Methods

It is common for methods to have return values of their own. Any m
=__Graphics Class and Objects (pg 290-293)__=

Objects from the Graphics class come from the System::Drawing namespace.

__Common examples:__
 * Drawing::Brush** - used to fill with a solid color
 * Drawing::Rectangle** - can provide location and boundaries for actual drawing
 * Drawing::Graphics** - (usually declared as "g") uses brush to draw a picture

Objects are derived types - Delcared using ^ symbol (**Drawing::Graphics**^ g;)

__Process to create a circle inside a rectangle:__

1. Declare a **Drawing::Brush** object and **Drawing::Graphics** objects as instance objects 2. Initialize brush and graphics objects under **Form1_Load** (graphics object assigned to a pictureBox control) 3. Under the **btn_Click** event handler, declare **Drawing::Rectangle** object with dimensions as parameters 4. Use the **FillEllipse** method assigned to the graphics object, using the brush and rectangle as parameters

=__System-Defined Class Methods (pg 271-272)__=

System-defined class methods are built into the **System** namespace. Use the scope resolution operator

Syntax: System:://Class_name//:://Method_name//

Common examples of classes in the **System** namespace are **Int32**, **Convert**, and the **Math** classes.

=__The System::Math Class Library (pg 272)__=

Collection of methods related to mathematical functions

Examples:

Trigonometric funtions (**Math::Sin**, **Math::Cos**, **Math::Tan**)
 * Math::Sqrt** - Used to calculate the square root of a number
 * Math::Pow** - Used to raise a number to a power
 * Math::Log** - To find the logarithm of a number
 * Math::PI** - returns constant value of pi (3.14159265358979323846264338327950288...)

**__System Defined Instance Methods (pg 274)__** System Defined Instance Methods are methods predetermined by the programming system for an object. By simple means of a little extra code, you don’t have to program a whole other section of code for a tiny little thing.

Take the textbox object for example and the code **textbox1->Focus;**. This tells the program to reset the typing cursor back into the textbox. Focus is a built in method of the textbox accessible by the code **Focus** tagged on.

**__Programmer Defined Methods__** Programmer defined methods are methods specifically made by the programmer. Instead of double clicking on a button or the like, and C++ making the signature line for you, the programmer writes it themselves. The line is written like this: //Access_mode: return_type:: method_name (parameter list)// //{…}// The **return type** is the type of data the method will return back to the program. The method name is decided by the programmer, and is called earlier in the program code. The **method body** (signified by the {…}) is the list of statements that make up the actual method and coding that is executed when the program runs.
 * Access mode** refers to how much other parts of the program have access to the method. Private, public , and protected are the three access types. Private is the most common, and means that only members of the same class can access it.
 * Parameter list** refers to information the method receives from the unit that called it.

//Void// Data types such as **int** or **double** are used as the return type. However, commonly used is the **void** or **System::Void types.** Void tells the programmer and the program that the method is not returning any values. Often times, such as in a button_Click method, the data used inside the method body is also outputted from that same body. Take a math program. Your button does all the math inside it and still outputs your answer, but the signature line still says void. It just means that the method isn’t being used by another, so it doesn’t have to //return// any values.

= = =__**Programmer Defined Methods with Reference Parameters**__= Methods with Reference Parameters are used to change the value of a variable, and return the changed value when it is done. First you must call the method in an event such as button1_Click ReadData(num) Then write the programmer-defined method. private: void ReadData{} In order to say that a variable is a reference parameter, you must use the & symbol. private: void ReadData(int& ref_num){} This is called Pass-by-reference Now you can DO THINGS with the variable ref_num, which uses num as a reference. private: void ReadData(int& ref_num) { Int32::TryParse(textBox1->Text, ref_num); } Now, if ref_num’s value was changed, it will return the changed value. In this example it wasn’t. In THIS example it WILL be changed. private: blah blah timer1_Tick(blah blah){ int x, y; double angle_radians; stuff stuff stuff stuff private: void SetXY( int & x, int & y, double angle_radians) { x=xcenter+smajor*Math::Cos(angle_radians); y=ycenter-sminor*Math::Sin(angle_radians); }
 * SetXY(x, y, angle_radians);**

=**__Programmer Defined Methods with Return Value (p. 288-289)__**= When values need to be sent back, **reference parameters** are used. It is common for methods to have return values of their own, and any method may have a return value. Many system class methods have return values that may or may not be used. For example, condsider **TryParse**. It is used to read data, like such: //Int32::TryParse(textbox1->Text, num);// In this example, it tries to parse the text from textbox1 and store it into the variable num. However, if it is unable to find an integer, the variable num is not changed. TryParse always returns a Boolean value, indicating whether the parse attempt was successful (0 if it is not successful, 1 if it is). This could be used to alert the user to a potential problem, like in this example:

// **int** num; **bool** ok; ok=Int32::TryParse(txtWidth->Text, num); **if** (ok==true) MessageBox::Show(“Data read successfully”); **else** Message Box::Show(“ERROR reading data”);//

This could also be done with programmer-defined methods using a **return**, like shown below:

// **private** : **void** ConvertToYards( **int** yards){ **int** meters; meters=yards*1.0936; **return** meters; }//

// **private** : System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e) { **int** yards, meters; yards=Int32::TryParse(txtYards->, yards); meters=ConvertToYards(yards); }//

In the above example, yards is parsed out of txtYards, and then passed into ConvertToYards. From there, it is converted to yards and returned back. This is similar to in algebra where f(x)=something, only it is Method_Name(variables passed)=something.