Download
bil528 bilgisayar programlama ii n.
Skip this Video
Loading SlideShow in 5 Seconds..
BIL528 – Bilgisayar Programlama II PowerPoint Presentation
Download Presentation
BIL528 – Bilgisayar Programlama II

BIL528 – Bilgisayar Programlama II

110 Views Download Presentation
Download Presentation

BIL528 – Bilgisayar Programlama II

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. BIL528 – Bilgisayar Programlama II Methods

  2. Contents • Methods

  3. 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

  4. Methods should be placed in a class definition Event handlers are methods too Like classes, methods should be written between ‘{‘ and ‘}’

  5. 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() { } The method will be used only in the form Name of the method The method returns nothing back

  6. 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; }

  7. About the method • Last week, you wrote the same code three times which opens anOpenFileDialog 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.

  8. Declaring a method that take one parameter privatevoidDrawBorder(PictureBoxobjPicturebox) { Graphicsg = this.CreateGraphics(); g.Clear(SystemColors.Control); g.DrawRectangle(Pens.Blue, objPicturebox.Left - 1, objPicturebox.Top - 1, objPicturebox.Width + 1, objPicturebox.Height + 1); g.Dispose(); }

  9. A method that returns a value private int ComputeLength(string strText) { return strText.Length; } Type of the return value Name of the method Type of the parameter Name of the parameter The return keyword, 1) Terminates the method 2) Returns the specified value back

  10. 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. Example private void ChangeText(TextBoxtb) { tb.Text = "The text is changed."; } private void Form_Load() { TextBox1.Text = "Original text"; ChangeText(TextBox1); } TextBox1 is passed to the method by reference and its text is changed by the method.

  18. 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.