Upload
jackson-acosta
View
33
Download
0
Embed Size (px)
DESCRIPTION
BIM211 – Visual Programming. Methods. Contents. Methods. Methods. A method is a discrete set of code that can be called from other code. - PowerPoint PPT Presentation
Citation preview
BIM211 – Visual Programming
Methods
1
Contents
• Methods
2
Methods
• A method is a discrete set of code that can be called from other code.
• Methods are much like events, but rather than being executed by a user interacting with a form or control, methods are executed when called by a code statement.
• Two types of methods are used in Visual C#:– Methods that return a value– Methods that do not return a value
3
4
Methods should be placed in a class definition
Event handlers are methods too
Like classes, methods should be written between ‘{‘ and ‘}’
Declaring methods that don’t return values
• Open the Picture Viewer program and write the following code inside the class definition in ViewerForm.cs:
private void OpenPicture()
{
}
5
Name of the method
The method returns nothing back
The method will be used only in the form
OpenPicture Method// Show the open file dialog box.if (ofdSelectPicture.ShowDialog() == DialogResult.OK){ // Load the picture into the picture box. picShowPicture.Image =
Image.FromFile(ofdSelectPicture.FileName); // Show the name of the file in the form’s caption. this.Text = string.Concat(“Picture Viewer(“ +ofdSelectPicture.FileName + “)”); // Show the name of the file in the status bar. sbrMyStatusStrip.Items[0].Text = ofdSelectPicture.FileName;}
6
About the method
• Last week, you wrote the same code three times which opens an OpenFileDialog and displays the selected picture.
• Instead of this duplication, you may prefer to write a single method and call it from the other three occasions.
7
Declaring a method that take one parameter
private void DrawBorder(PictureBox objPicturebox){ Graphics g = this.CreateGraphics(); g.Clear(SystemColors.Control); g.DrawRectangle(Pens.Blue, objPicturebox.Left - 1, objPicturebox.Top - 1, objPicturebox.Width + 1, objPicturebox.Height + 1); g.Dispose();}
8
9
A method that returns a value
private int ComputeLength(string strText){ return strText.Length;}
10
Name of the methodType of the return value
Name of the parameter
Type of the parameter
The return keyword,1) Terminates the method
2) Returns the specified value back
Calling Methods
• Open btnSelectPicture button’s Click event handler and delete its contents
• Write the following code:• this.OpenPicture();• or:• OpenPicture();• Go to other two locations that repeats the
same code and do the same.
11
Pass by Value Mechanism
private string Combine(string str, int num){ return str + num.ToString();}• Assume that this method is called by:string course = Combine("BIM", 211);
12
Pass by Value Mechanism
• Here, the string “BIM” and the number 211 are copied into the parameters of the function, str and num.
• This is called the pass by value mechanism.• Any change of parameters in the method does
not affect the original arguments in the caller method.
13
Pass by Reference Mechanism
• If you want a method to change the arguments, you have to use the pass by reference mechanism.
• This mechanism is accomplished by the keyword ref.
• Just put the keyword ref before the parameter that you want the method to change.
• Don’t forget to use the ref keyword when calling the method.
14
Pass by Reference Mechanism
private void Combine(ref string str, int num){ str = str.ToLower(); return str + num.ToString();}
15
Pass by Reference Mechanism
private void Form_Load(){ string dept = "BIM"; int code = 211; string course = Combine(ref dept, code); Label1.Text = "dept: "+dept+"course:"+course;}
16
About the Controls…
• The controls on a form are all references because they are created with the new constructor (Remember the pointers in C).
• It means that if you pass a control to a method, it is passed by reference automatically and any change made by the method affects the control.
17
Exampleprivate void ChangeText(TextBox tb){ tb.Text = "The text is changed.";}
private void Form_Load(){ TextBox1.Text = "Original text"; ChangeText(TextBox1);}
18
TextBox1 is passed to the method by reference and its text
is changed by the method.
Instance Methods
• The OpenPicture() method belongs to an instance of a class (or object). This type of methods are called instance methods.
• An instance method requires an object.• But creating an object takes resources and
time.• For some methods like mathematical
functions you can use static methods.
19
Static Methods
• Static methods belong to a class and they can be called without an instance of the class.
• All methods in Math class are static methods.– double y = Math.Sin(x);– double pi = Math.Atan(1.0) * 4;– double pi = Math.PI;
• You can define static methods in your programs too but you will learn it next.
20