20
BIM211 – Visual Programming Methods 1

BIM211 – Visual Programming

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

Page 1: BIM211 – Visual Programming

BIM211 – Visual Programming

Methods

1

Page 2: BIM211 – Visual Programming

Contents

• Methods

2

Page 3: BIM211 – Visual Programming

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

Page 4: BIM211 – Visual Programming

4

Methods should be placed in a class definition

Event handlers are methods too

Like classes, methods should be written between ‘{‘ and ‘}’

Page 5: BIM211 – Visual Programming

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

Page 6: BIM211 – Visual Programming

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

Page 7: BIM211 – Visual Programming

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

Page 8: BIM211 – Visual Programming

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

Page 9: BIM211 – Visual Programming

9

Page 10: BIM211 – Visual Programming

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

Page 11: BIM211 – Visual Programming

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

Page 12: BIM211 – Visual Programming

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

Page 13: BIM211 – Visual Programming

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

Page 14: BIM211 – Visual Programming

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

Page 15: BIM211 – Visual Programming

Pass by Reference Mechanism

private void Combine(ref string str, int num){ str = str.ToLower(); return str + num.ToString();}

15

Page 16: BIM211 – Visual Programming

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

Page 17: BIM211 – Visual Programming

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

Page 18: BIM211 – Visual Programming

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.

Page 19: BIM211 – Visual Programming

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

Page 20: BIM211 – Visual Programming

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