Visual C Sharp Methods and Classes 1 - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Visual C Sharp Methods and Classes 1

Description:

The Math class. Does common math calculations. Use: ClassName.MethodName (param 1, param2... How do we override Math.Sqrt double return value? Use explicit (float) ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 23
Provided by: johnal1
Category:

less

Transcript and Presenter's Notes

Title: Visual C Sharp Methods and Classes 1


1
Visual C Sharp Methods and Classes 1
  • Modular code
  • Break program into small defined routines
  • Each with a defined and testable function
  • Easier to debug - re-usable
  • Called a Method
  • e.g. Button_click
  • AKA function, procedure or subroutine
  • Classes are part of Object oriented code
  • Combined method and data
  • e.g. We have seen the Math class and methods
    (Cos)
  • And DateTime class, methods Now, AddHours
  • (remember naming convention Upper case)

2
Visual C Sharp Methods and Classes 2
  • The Math class
  • Does common math calculations
  • Use ClassName.MethodName (param 1, param2 )
  • e.g. Math.Sin(x), Math.Exp(y)
  • Also constants
  • Math.PI 3.1415926535
  • Math.E 2.7182818285
  • Use dot notation

3
Visual C Sharp Methods and Classes 3
  • Method declaration
  • Return data type (or void if none)
  • Name
  • Parameters
  • ltdata typegt ltnamegt ltparametersgt
  • Example Square root return double, pass
    integer
  • double SquareRoot ( int sq )
  • // code goes here
  • Called using its name
  • double SqRoot SquareRoot(3)

4
Visual C Sharp Methods and Classes 4
  • Code
  • double SquareRoot ( int sq )
  • return Math.Sqrt(sq) //Math.Sqrt returns a
    double
  • private void button1_Click(object sender,
    EventArgs e)
  • double SqRoot SquareRoot(3)
  • MessageBox.Show(SqRoot.ToString())
  • Display 1.73205080756888
  • Note button click is method too!

5
Visual C Sharp Methods and Classes 5
  • Suppose we only wanted float precision?
  • How do we override Math.Sqrt double return value?
  • Use explicit (float)
  • float SquareRoot ( int sq )
  • return (float)Math.Sqrt(sq)
  • Call using float SqRoot SquareRoot(3)
  • Display 1.732051

6
Visual C Sharp Methods and Classes 6
  • Reserved word return used to return value
  • sq used in method may be another name in
    calling routine
  • int two 2
  • int three 3
  • int root2 SqRoot(two)
  • int root3 SqRoot(three)

7
Visual C Sharp Methods and Classes 7
  • Method use
  • Nothing returned? - use void
  • Nothing passed? - empty brackets
  • Four cases
  • Pass nothing, return nothing,
  • Pass something, return nothing,
  • Pass nothing, return something,
  • Pass something, return something.
  • void delay( )
  • void delay(int delayTime)
  • boolean done( ) // return true when done
  • double SqRoot(int sq) // already seen

8
Visual C Sharp Methods and Classes 8
  • Example 1 A delay Wait pass/return nothing
  • void Wait( )
  • int i 0
  • while (i lt 10) i i 1
  • Call using
  • private void button1_Click(object sender,
    EventArgs e)
  • Wait( )

9
Visual C Sharp Methods and Classes 9
  • Example 2a Pass delay time, return nothing
  • void PassWaitTime(int delayTime)
  • int i 0
  • while (i lt delayTime) i i 1
  • Call using
  • private void button1_Click(object sender,
    EventArgs e)
  • PassWaitTime(10)

10
Visual C Sharp Methods and Classes 10
  • Example 2b - Can pass variable
  • int longTime 10000
  • int shortTime 10
  • Wait(longTime)
  • Wait(shortTime)
  • Note variable name is longTime or shortTime in
    main program, but delayTime in method.
  • Can we declare delayTime in main program?
  • Yes but is a different version variable
    scope

11
Visual C Sharp Methods and Classes 11
  • Example 3 Pass nothing, return something
  • Wait routine returns string done
  • string ReturnWait( )
  • int i 0
  • while (i lt 10) i i 1
  • return done
  • Have do to something with returned value
  • private void button1_Click(object sender,
    EventArgs e)
  • MessageBox.Show(ReturnWait( ))

12
Visual C Sharp Methods and Classes 12
  • Example 4 Pass and return value
  • string PassReturnWait(int delayTime)
  • int i 0
  • while (i lt delayTime) i i 1
  • return done
  • Call
  • int shortTime 10
  • MessageBox.Show(ReturnWait( shortTime))

13
Visual C Sharp Methods and Classes 13
  • Variables
  • Declared in a method local variables
  • Declared outside a method global variables
  • AKA scope where it can be referenced in the
    program
  • Only the method that defines them know they exist
  • Send parameters to communicate with other methods

14
Visual C Sharp Methods and Classes 14
  • What if we want to pass or return more values?
  • Add to parameter list, example button click
  • private void button1_Click(object sender,
    EventArgs e)
  • Note passing an object and event details here
  • What if we want to pass hundreds of items?
  • Pass pointer to array or structure
  • Pass by value. Copy made seen this
  • Pass by reference. Address of variable passed
  • faster but insecure
  • Keywords ref and out

15
Visual C Sharp Methods and Classes 15
  • Methods can call themselves - Recursive
  • Breaks problem down to simpler forms
  • Must be a way out!
  • Example Factorial
  • N! N(N-1)! N(N-1)(N-2)!
  • 1! 1

16
Visual C Sharp Methods and Classes 16
Final value 120
5! 5 24 120 is returned
4! 4 6 24 is returned
3! 3 2 6 is returned
2! 2 1 2 is returned
1 returned
(b) Values returned from each recursive call.
17
Visual C Sharp Methods and Classes 17
  • int Factorial(int factorialNo)
  • if (factorialNo 0)
  • return 1
  • else
  • return factorialNo Factorial(factorialNo
    - 1)
  • Call
  • int fac5 Factorial(5) // soon gets a large
    number !

18
Visual C Sharp Methods and Classes 18
  • Method overloading
  • Methods can have the same name but different
    number (or type) of arguments
  • Usually perform the same task on different data
    types
  • Example
  • MessageBox.Show(text )
  • MessageBox.Show("text,"caption")
  • MessageBox.Show(text", caption, buttons,
    icon)
  • See how to do this later

19
Visual C Sharp Methods and Classes 19
  • Classes and Objects
  • Consider a button object
  • Each button has its own data (properties) and
    methods (buttonClick)
  • All created from same code (method and data) a
    class
  • A class is a method with its own data (e.g.
    .top)
  • An object is a unique case (called an instance)
    of a class
  • How do we define a class? - Later

20
Visual C Sharp Methods and Classes 20
  • Suppose we want our method to have its own
    methods?
  • No problem
  • BUT do we want it to be seen by main program?
  • Private, public, protected methods
  • Private local to method/class
  • Public seen everywhere
  • Protected intermediate accessed by class or
    classes derived from it - OOP

21
Visual C Sharp Methods and Classes 21
  • Namespace
  • A group of classes and their methods
  • .NET library (FCL) is composed of namespaces
  • Namespaces are stored in .dll files called
    assemblies
  • Some FLC namespaces
  • System, System.IO, System.Windows.Forms
  • Included in a program with the using keyword

22
Visual C Sharp Methods and Classes 22
  • Summary
  • Methods break up program
  • Reusable
  • Declare return and passed variables and types
  • Pass data to and from methods.
  • OOP
  • Classes combine data (properties) and code
    (method)
  • Objects individual case (instance) of a class
Write a Comment
User Comments (0)
About PowerShow.com