SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

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 ...Warren0989
 
9781285852744 ppt ch02
9781285852744 ppt ch029781285852744 ppt ch02
9781285852744 ppt ch02Terry Yoast
 
Computer Programming
Computer ProgrammingComputer Programming
Computer ProgrammingBurhan Fakhar
 
Functions ppt ch06
Functions ppt ch06Functions ppt ch06
Functions ppt ch06Terry Yoast
 
9781285852744 ppt ch12
9781285852744 ppt ch129781285852744 ppt ch12
9781285852744 ppt ch12Terry Yoast
 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
 
9781285852744 ppt ch13
9781285852744 ppt ch139781285852744 ppt ch13
9781285852744 ppt ch13Terry Yoast
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
Testing Model Transformations
Testing Model TransformationsTesting Model Transformations
Testing Model Transformationsmiso_uam
 
CIS110 Computer Programming Design Chapter (4)
CIS110 Computer Programming Design Chapter  (4)CIS110 Computer Programming Design Chapter  (4)
CIS110 Computer Programming Design Chapter (4)Dr. Ahmed Al Zaidy
 
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 TESTINGJournal For Research
 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
 

What's hot (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
 

Viewers also liked

Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Visula C# Programming Lecture 6
Visula C# Programming Lecture 6Abou Bakr Ashraf
 
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 overridingPinky Anaya
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
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 TypesDudy Ali
 
C# Tutorial
C# Tutorial C# Tutorial
C# Tutorial Jm Ramos
 
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 LearningeVideoTuition
 
Slideshare.Com Powerpoint
Slideshare.Com PowerpointSlideshare.Com Powerpoint
Slideshare.Com Powerpointguested929b
 

Viewers also liked (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
 

Similar to Csc153 chapter 07

CIS110 Computer Programming Design Chapter (9)
CIS110 Computer Programming Design Chapter  (9)CIS110 Computer Programming Design Chapter  (9)
CIS110 Computer Programming Design Chapter (9)Dr. Ahmed Al Zaidy
 
Intro to Programming: Modularity
Intro to Programming: ModularityIntro to Programming: Modularity
Intro to Programming: ModularityNicole Ryan
 
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
 
test.pptx
test.pptxtest.pptx
test.pptxStacia7
 
9781285852744 ppt ch10
9781285852744 ppt ch109781285852744 ppt ch10
9781285852744 ppt ch10Terry Yoast
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
Java method present by showrov ahamed
Java method present by showrov ahamedJava method present by showrov ahamed
Java method present by showrov ahamedMd Showrov Ahmed
 
Learn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsLearn C# Programming - Encapsulation & Methods
Learn C# Programming - Encapsulation & MethodsEng Teong Cheah
 
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.pptxArjunKhanal8
 
CIS110 Computer Programming Design Chapter (2)
CIS110 Computer Programming Design Chapter  (2)CIS110 Computer Programming Design Chapter  (2)
CIS110 Computer Programming Design Chapter (2)Dr. Ahmed Al Zaidy
 
Logic Formulation 2
Logic Formulation 2Logic Formulation 2
Logic Formulation 2deathful
 
ASP.NET Session 3
ASP.NET Session 3ASP.NET Session 3
ASP.NET Session 3Sisir Ghosh
 

Similar to 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
 

Recently uploaded

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 

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