BIL528 – Bilgisayar Programlama II Methods 1. Contents Methods 2.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
The Web Warrior Guide to Web Design Technologies
Programming Based on Events
COMP 14 Introduction to Programming Mr. Joshua Stough February 28, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
OOP Languages: Java vs C++
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
BIL528 – Bilgisayar Programlama II Introduction 1.
BIM211 – Visual Programming Database Operations II 1.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Visual C++ Programming: Concepts and Projects Chapter 6A: Methods (Concepts)
BİL528 – Bilgisayar Programlama II Database Operations II 1.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 1B Introduction (Tutorial)
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Copyright  Hannu Laine C++-programming Part 3 Hannu Laine.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
BIL528 – Bilgisayar Programlama II Introduction 1.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Applications Development
Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis Chapter 5: Methods.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
Dialog Boxes.
1 Methods Introduction to Methods Passing Arguments to a Method More About Local Variables Returning a Value from a Method Problem Solving with Methods.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 15 – Fund Raiser Application Introducing.
BİL527 – Bilgisayar Programlama I Functions 1. Contents Functions Delegates 2.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-1 Why Write Methods? Methods are commonly used to break a problem down.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 6A Methods (Concepts)
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 02 Dr. Eng. Ibrahim El-Nahry Methods. 2 Learning Objectives Class Definition includes both methods and data properties Method Definition and Declaration.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Client-side (JavaScript) Validation. Associating a function with a click event – Part 1 Use the input tag’s onclick attribute to associate a function.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Introduction to Object-oriented Programming
Java Programming: Guided Learning with Early Objects
INF230 Basics in C# Programming
Classes and Objects.
BİL527 – Bilgisayar Programlama I
Class Operations Pointer and References with class types
Class: Special Topics Copy Constructors Static members Friends this
Using Procedures and Exception Handling
More Object Oriented Programming
Method Mark and Lyubo.
METHODS AND BEHAVIORS AKEEL AHMED.
CSC 113 Tutorial QUIZ I.
Variables and Their scope
Classes and Objects 5th Lecture
CHAPTER 6 GENERAL-PURPOSE METHODS
CIS16 Application Development and Programming using Visual Basic.net
Visual Basic: Week 5 Review User defined functions
Tonga Institute of Higher Education
Constructors, GUI’s(Using Swing) and ActionListner
February , 2009 CSE 113 B.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
References Revisted (Ch 5)
Presentation transcript:

BIL528 – Bilgisayar Programlama II Methods 1

Contents Methods 2

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 method Type 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

Example private 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