SlideShare une entreprise Scribd logo
1  sur  35
Microsoft Visual C# 2010
       Fourth Edition



        Chapter 7
      Using Methods
Objectives
• Learn about methods and implementation hiding
• Write methods with no parameters and no return
  value
• Write methods that require a single argument
• Write methods that require multiple arguments
• Write a method that returns a value




Microsoft Visual C# 2010, Fourth Edition           2
Objectives (cont'd.)
• Pass an array to a method
• Learn some alternate ways to write a Main()
  method header
• Learn about issues using methods in GUI programs




Microsoft Visual C# 2010, Fourth Edition         3
Understanding Methods and
               Implementation Hiding
• Method
    – Encapsulated series of statements that perform a task
    – Using a method is called invoking or calling




Microsoft Visual C# 2010, Fourth Edition                 4
Understanding Methods and
         Implementation Hiding (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   5
Understanding Implementation Hiding

• Implementation hiding
    – An important principle of object-oriented programming
    – Keeps the details of a method’s operations hidden
• Only concern is the way you interface or interact
  with the method
    – Program does not need to know how method works
• Multifile assembly
    – Program that uses methods and classes stored in
      other files


Microsoft Visual C# 2010, Fourth Edition                 6
Writing Methods with No Parameters
          and No Return Value
• Major reasons to create a method
    – Code will remain short and easy to follow
    – A method is easily reusable
• Code bloat
    – Unnecessarily long or repetitive statements
• A method must include:
    – Method declaration
          • Also called a method header or method definition
    – Opening curly brace
    – Method body
    – Closing curly brace
Microsoft Visual C# 2010, Fourth Edition                       7
Writing Methods with No Parameters
     and No Return Value (cont'd.)
• Method declaration
    – Defines the rules for using the method and contains:
          •   Optional declared accessibility
          •   Optional static modifier
          •   Return type for the method
          •   Method name, or identifier
          •   Opening parenthesis
          •   Optional list of method parameters
          •   Closing parenthesis



Microsoft Visual C# 2010, Fourth Edition                     8
Writing Methods with No Parameters
     and No Return Value (cont'd.)
• Optional declared accessibility
    – Limits how other methods can use your method
    – Possible access values
          •   Public
          •   Protected internal
          •   Protected
          •   Internal
          •   Private
• You can declare a method to be static or nonstatic
    – Methods are nonstatic by default
Microsoft Visual C# 2010, Fourth Edition             9
Writing Methods with No Parameters
     and No Return Value (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   10
Writing Methods with No Parameters
     and No Return Value (cont'd.)
• You can declare a method to be static or nonstatic
    – Methods are nonstatic by default
• static method
    – Can be called without referring to an object
          • Instead, you refer to the class
    – Cannot call nonstatic methods
          • Nonstatic methods can call static ones




Microsoft Visual C# 2010, Fourth Edition             11
Writing Methods with No Parameters
     and No Return Value (cont'd.)
• Every method has a return type
    – Indicates what kind of value the method will return to
      any other method that calls it
    – If a method does not return a value, its return type is
      void
• Method name must be a legal C# identifier
• Scope
    – Area in which a variable is known
• Parameter to a method
    – Variable that holds data passed to a method when it is
      called
Microsoft Visual C# 2010, Fourth Edition                    12
Writing Methods with No Parameters
     and No Return Value (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   13
Writing Methods with No Parameters
     and No Return Value (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   14
Writing Methods That Require a Single
             Argument
• You need:
    – Type of the parameter
    – A local identifier (name) for the parameter
• Local variable
    – Declared within a method
• Formal parameter
    – Parameter in a method header that accepts a value
• Actual parameters
    – Arguments within a method call


Microsoft Visual C# 2010, Fourth Edition                  15
Writing Methods That Require a Single
          Argument (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   16
Writing Methods That Require a Single
          Argument (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   17
Writing Methods That Require Multiple
             Arguments
• Methods can take any number of parameters
• When you call the method, arguments must match
     – In both number and type




Microsoft Visual C# 2010, Fourth Edition       18
Writing Methods That Require Multiple
         Arguments (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   19
Writing a Method That Returns a
                     Value
• A method can return, at most, one value to a method
  that calls it
• Method’s type
    – Method’s return type
• return statement
    – Causes a value to be sent back to the calling method
• You are not required to use a returned value




Microsoft Visual C# 2010, Fourth Edition                 20
Writing a Method That Returns a
                 Value (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   21
Writing a Method That Returns a
                 Value (cont'd.)
• A returned value can be:
     – Stored in a variable
     – Used directly
• Nested method calls
     – Method calls placed inside other method calls
• Writing a method that returns a Boolean value
     – When a method returns a value that is type bool
           • Method call can be used anywhere you can use a
             Boolean expression


Microsoft Visual C# 2010, Fourth Edition                      22
Writing a Method That Returns a
                 Value (cont'd.)




Microsoft Visual C# 2010, Fourth Edition   23
Passing an Array to a Method

• You can pass a single array element to a method
    – In same manner as you would pass a variable
• Variables are passed by value
    – Local variables store a local copy of the value
• You can pass an entire array as a parameter
• Arrays, like all objects but unlike built-in types, are
  passed by reference
    – Method receives actual memory address of the array
          • Has access to the actual values in the array elements


Microsoft Visual C# 2010, Fourth Edition                            24
Microsoft Visual C# 2010, Fourth Edition   25
Microsoft Visual C# 2010, Fourth Edition   26
Passing an Array to a Method (cont'd.)

• You can pass a multidimensional array to a method
    – By indicating the appropriate number of dimensions
      after the data type in the method header
    – Example
           public static void
        displayScores(int[,]scoresArray)
• Jagged arrays
    – Insert the appropriate number of square brackets after
      the data type in the method header
    – Example
             public static void displayIDs(int[][] idArray)

Microsoft Visual C# 2010, Fourth Edition                      27
Alternate Ways to Write a Main()
               Method Header
• Conventional way
        public static void Main()
• Passing command-line arguments
        public static void Main(string[] args)
• Some programmers prefer to write Main() method
  headers with a return type of int instead of void
    – Last statement in the Main() method must be a
      return statement



Microsoft Visual C# 2010, Fourth Edition              28
Issues Using Methods in GUI
                     Programs
• Some special considerations when creating GUI
  applications
    – Understanding methods that are automatically
      generated in the visual environment
    – Appreciating scope in a GUI program
    – Creating methods to be nonstatic when associated
      with a Form




Microsoft Visual C# 2010, Fourth Edition                 29
Understanding Methods that are
 Automatically Generated in the Visual
             Environment
• When you create GUI applications using the IDE
    – Many methods are generated automatically




Microsoft Visual C# 2010, Fourth Edition           30
Appreciating Scope in a GUI Program
• When you declare a variable or constant within a
  method, it is local to that method
• If a variable or constant is needed by multiple
  event-handling methods
    – Variables or constants in question must be defined
      outside the methods (but within the Form class)
• Automatically generated event-handling methods
  have predefined sets of parameters



Microsoft Visual C# 2010, Fourth Edition                   31
Creating Methods to be Nonstatic
       when Associated with a Form
• When you create a GUI application and generate a
  method, the keyword static does not appear in the
  method header
    – Because the method is associated with an object from
      which the method-invoking events are sent




Microsoft Visual C# 2010, Fourth Edition                32
You Do It
• Activities to explore
    – Calling a Method
    – Writing a Method that Receives Parameters and
      Returns a Value




Microsoft Visual C# 2010, Fourth Edition              33
Summary
• Method is a series of statements that perform a
  task
• Some methods require passed-in information
  called arguments or parameters
• You write methods to make programs easier to
  understand and so that you can easily reuse them
• Object-oriented programs hide their methods’
  implementation
• You can pass multiple arguments to a method


Microsoft Visual C# 2010, Fourth Edition             34
Summary (cont'd.)
• The return type for a method can be any type used
  in the C# programming language
• You can pass an array as a parameter to a method
• You might see different Main() method headers in
  other books or in programs written by others
• Special considerations exist for methods when you
  create GUI applications




Microsoft Visual C# 2010, Fourth Edition          35

Contenu connexe

Tendances

Tendances (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Chapter 4 5
Chapter 4 5Chapter 4 5
Chapter 4 5
 
Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...Solutions manual for c++ programming from problem analysis to program design ...
Solutions manual for c++ programming from problem analysis to program design ...
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02
 
Chap02
Chap02Chap02
Chap02
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06
 
JavaScript functions
JavaScript functionsJavaScript functions
JavaScript functions
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
 
Chapter2
Chapter2Chapter2
Chapter2
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13
 
Lesson 13 object and class
Lesson 13 object and classLesson 13 object and class
Lesson 13 object and class
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformations
 
CIS110 Computer Programming Design Chapter (4)
CIS110 Computer Programming Design Chapter  (4)CIS110 Computer Programming Design Chapter  (4)
CIS110 Computer Programming Design Chapter (4)
 
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTINGA WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
A WHITE BOX TESTING TECHNIQUE IN SOFTWARE TESTING : BASIS PATH TESTING
 
DISE - Programming Concepts
DISE - Programming ConceptsDISE - Programming Concepts
DISE - Programming Concepts
 
Coding
CodingCoding
Coding
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
 

En vedette (12)

Programming in c#
Programming in c#Programming in c#
Programming in c#
 
Csharp_Chap03
Csharp_Chap03Csharp_Chap03
Csharp_Chap03
 
Lo2
Lo2Lo2
Lo2
 
Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6
 
Differences between method overloading and method overriding
Differences between method overloading and method overridingDifferences between method overloading and method overriding
Differences between method overloading and method overriding
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Object Oriented Programming - Value Types & Reference Types
Object Oriented Programming - Value Types & Reference TypesObject Oriented Programming - Value Types & Reference Types
Object Oriented Programming - Value Types & Reference Types
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick LearningOracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
Oracle/SQL For Beginners - DDL | DML | DCL | TCL - Quick Learning
 
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with ExamplesDML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
DML, DDL, DCL ,DRL/DQL and TCL Statements in SQL with Examples
 
Slideshare.Com Powerpoint
Slideshare.Com PowerpointSlideshare.Com Powerpoint
Slideshare.Com Powerpoint
 

Similaire à Csc153 chapter 07

Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Simplilearn
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
LokeshK66
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
ArjunKhanal8
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
Sisir Ghosh
 

Similaire à Csc153 chapter 07 (20)

CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: Modularity
 
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
Methods in C# | C# Methods Tutorial | C# Tutorial for Beginners | Learn C# Pr...
 
7494605
74946057494605
7494605
 
test.pptx
test.pptxtest.pptx
test.pptx
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
Ch07 cmpt110
Ch07 cmpt110Ch07 cmpt110
Ch07 cmpt110
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamed
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & Methods
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdf
 
First draft programming c++
First draft programming c++First draft programming c++
First draft programming c++
 
METHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptxMETHODS OR FUNCTIONS IN C for dotnet.pptx
METHODS OR FUNCTIONS IN C for dotnet.pptx
 
CIS110 Computer Programming Design Chapter (2)
CIS110 Computer Programming Design Chapter  (2)CIS110 Computer Programming Design Chapter  (2)
CIS110 Computer Programming Design Chapter (2)
 
Logic Formulation 2
Logic Formulation 2Logic Formulation 2
Logic Formulation 2
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3
 
C++.ppt
C++.pptC++.ppt
C++.ppt
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Dernier (20)

Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

Csc153 chapter 07

  • 1. Microsoft Visual C# 2010 Fourth Edition Chapter 7 Using Methods
  • 2. Objectives • Learn about methods and implementation hiding • Write methods with no parameters and no return value • Write methods that require a single argument • Write methods that require multiple arguments • Write a method that returns a value Microsoft Visual C# 2010, Fourth Edition 2
  • 3. Objectives (cont'd.) • Pass an array to a method • Learn some alternate ways to write a Main() method header • Learn about issues using methods in GUI programs Microsoft Visual C# 2010, Fourth Edition 3
  • 4. Understanding Methods and Implementation Hiding • Method – Encapsulated series of statements that perform a task – Using a method is called invoking or calling Microsoft Visual C# 2010, Fourth Edition 4
  • 5. Understanding Methods and Implementation Hiding (cont'd.) Microsoft Visual C# 2010, Fourth Edition 5
  • 6. Understanding Implementation Hiding • Implementation hiding – An important principle of object-oriented programming – Keeps the details of a method’s operations hidden • Only concern is the way you interface or interact with the method – Program does not need to know how method works • Multifile assembly – Program that uses methods and classes stored in other files Microsoft Visual C# 2010, Fourth Edition 6
  • 7. Writing Methods with No Parameters and No Return Value • Major reasons to create a method – Code will remain short and easy to follow – A method is easily reusable • Code bloat – Unnecessarily long or repetitive statements • A method must include: – Method declaration • Also called a method header or method definition – Opening curly brace – Method body – Closing curly brace Microsoft Visual C# 2010, Fourth Edition 7
  • 8. Writing Methods with No Parameters and No Return Value (cont'd.) • Method declaration – Defines the rules for using the method and contains: • Optional declared accessibility • Optional static modifier • Return type for the method • Method name, or identifier • Opening parenthesis • Optional list of method parameters • Closing parenthesis Microsoft Visual C# 2010, Fourth Edition 8
  • 9. Writing Methods with No Parameters and No Return Value (cont'd.) • Optional declared accessibility – Limits how other methods can use your method – Possible access values • Public • Protected internal • Protected • Internal • Private • You can declare a method to be static or nonstatic – Methods are nonstatic by default Microsoft Visual C# 2010, Fourth Edition 9
  • 10. Writing Methods with No Parameters and No Return Value (cont'd.) Microsoft Visual C# 2010, Fourth Edition 10
  • 11. Writing Methods with No Parameters and No Return Value (cont'd.) • You can declare a method to be static or nonstatic – Methods are nonstatic by default • static method – Can be called without referring to an object • Instead, you refer to the class – Cannot call nonstatic methods • Nonstatic methods can call static ones Microsoft Visual C# 2010, Fourth Edition 11
  • 12. Writing Methods with No Parameters and No Return Value (cont'd.) • Every method has a return type – Indicates what kind of value the method will return to any other method that calls it – If a method does not return a value, its return type is void • Method name must be a legal C# identifier • Scope – Area in which a variable is known • Parameter to a method – Variable that holds data passed to a method when it is called Microsoft Visual C# 2010, Fourth Edition 12
  • 13. Writing Methods with No Parameters and No Return Value (cont'd.) Microsoft Visual C# 2010, Fourth Edition 13
  • 14. Writing Methods with No Parameters and No Return Value (cont'd.) Microsoft Visual C# 2010, Fourth Edition 14
  • 15. Writing Methods That Require a Single Argument • You need: – Type of the parameter – A local identifier (name) for the parameter • Local variable – Declared within a method • Formal parameter – Parameter in a method header that accepts a value • Actual parameters – Arguments within a method call Microsoft Visual C# 2010, Fourth Edition 15
  • 16. Writing Methods That Require a Single Argument (cont'd.) Microsoft Visual C# 2010, Fourth Edition 16
  • 17. Writing Methods That Require a Single Argument (cont'd.) Microsoft Visual C# 2010, Fourth Edition 17
  • 18. Writing Methods That Require Multiple Arguments • Methods can take any number of parameters • When you call the method, arguments must match – In both number and type Microsoft Visual C# 2010, Fourth Edition 18
  • 19. Writing Methods That Require Multiple Arguments (cont'd.) Microsoft Visual C# 2010, Fourth Edition 19
  • 20. Writing a Method That Returns a Value • A method can return, at most, one value to a method that calls it • Method’s type – Method’s return type • return statement – Causes a value to be sent back to the calling method • You are not required to use a returned value Microsoft Visual C# 2010, Fourth Edition 20
  • 21. Writing a Method That Returns a Value (cont'd.) Microsoft Visual C# 2010, Fourth Edition 21
  • 22. Writing a Method That Returns a Value (cont'd.) • A returned value can be: – Stored in a variable – Used directly • Nested method calls – Method calls placed inside other method calls • Writing a method that returns a Boolean value – When a method returns a value that is type bool • Method call can be used anywhere you can use a Boolean expression Microsoft Visual C# 2010, Fourth Edition 22
  • 23. Writing a Method That Returns a Value (cont'd.) Microsoft Visual C# 2010, Fourth Edition 23
  • 24. Passing an Array to a Method • You can pass a single array element to a method – In same manner as you would pass a variable • Variables are passed by value – Local variables store a local copy of the value • You can pass an entire array as a parameter • Arrays, like all objects but unlike built-in types, are passed by reference – Method receives actual memory address of the array • Has access to the actual values in the array elements Microsoft Visual C# 2010, Fourth Edition 24
  • 25. Microsoft Visual C# 2010, Fourth Edition 25
  • 26. Microsoft Visual C# 2010, Fourth Edition 26
  • 27. Passing an Array to a Method (cont'd.) • You can pass a multidimensional array to a method – By indicating the appropriate number of dimensions after the data type in the method header – Example public static void displayScores(int[,]scoresArray) • Jagged arrays – Insert the appropriate number of square brackets after the data type in the method header – Example public static void displayIDs(int[][] idArray) Microsoft Visual C# 2010, Fourth Edition 27
  • 28. Alternate Ways to Write a Main() Method Header • Conventional way public static void Main() • Passing command-line arguments public static void Main(string[] args) • Some programmers prefer to write Main() method headers with a return type of int instead of void – Last statement in the Main() method must be a return statement Microsoft Visual C# 2010, Fourth Edition 28
  • 29. Issues Using Methods in GUI Programs • Some special considerations when creating GUI applications – Understanding methods that are automatically generated in the visual environment – Appreciating scope in a GUI program – Creating methods to be nonstatic when associated with a Form Microsoft Visual C# 2010, Fourth Edition 29
  • 30. Understanding Methods that are Automatically Generated in the Visual Environment • When you create GUI applications using the IDE – Many methods are generated automatically Microsoft Visual C# 2010, Fourth Edition 30
  • 31. Appreciating Scope in a GUI Program • When you declare a variable or constant within a method, it is local to that method • If a variable or constant is needed by multiple event-handling methods – Variables or constants in question must be defined outside the methods (but within the Form class) • Automatically generated event-handling methods have predefined sets of parameters Microsoft Visual C# 2010, Fourth Edition 31
  • 32. Creating Methods to be Nonstatic when Associated with a Form • When you create a GUI application and generate a method, the keyword static does not appear in the method header – Because the method is associated with an object from which the method-invoking events are sent Microsoft Visual C# 2010, Fourth Edition 32
  • 33. You Do It • Activities to explore – Calling a Method – Writing a Method that Receives Parameters and Returns a Value Microsoft Visual C# 2010, Fourth Edition 33
  • 34. Summary • Method is a series of statements that perform a task • Some methods require passed-in information called arguments or parameters • You write methods to make programs easier to understand and so that you can easily reuse them • Object-oriented programs hide their methods’ implementation • You can pass multiple arguments to a method Microsoft Visual C# 2010, Fourth Edition 34
  • 35. Summary (cont'd.) • The return type for a method can be any type used in the C# programming language • You can pass an array as a parameter to a method • You might see different Main() method headers in other books or in programs written by others • Special considerations exist for methods when you create GUI applications Microsoft Visual C# 2010, Fourth Edition 35