m m e e
play

M M E E in C# in C# I I K K 204111 Computer & - PowerPoint PPT Presentation

Writing Writing Methods Methods M M E E in C# in C# I I K K 204111 Computer & Programming Dr.Arnon Rungsawang http://mike.cpe.ku.ac.th/204111 2 K K 204111 Computer & Programming - Writing Methods I I E M M E


  1. Writing Writing Methods Methods M M E E in C# in C# I I K K 204111 Computer & Programming Dr.Arnon Rungsawang http://mike.cpe.ku.ac.th/204111

  2. 2 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  3. Classes & Methods Classes & Methods class HelloWorld { public static void Main () { myHello(); } static void myHello() { // method body Console.WriteLine( “ Hello World! ” ) } … } 3 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  4. Standard Classes Standard Classes • C# Framework Class Library (FCL) defines many classes, e.g., – Console – MessageBox – Int32 – Math • The definition of a class includes both methods and data properties: – methods, e.g., • Console.WriteLine( ) • Int32.Parse( ) • Math.Cos( ) • MessageBox.Show( ) – data properties, e.g., • Int32.MinValue • Int32.MaxValue • Math.PI • Math.E 4 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  5. Methods Methods • A method method’ ’s name s name should provide a well-defined, easy-to- understand functionality. – A method takes input (parameters) takes input (parameters), performs some actions performs some actions, and (sometime) returns a value returns a value. • Example: invoking the WriteLine method of the Console class: Console.WriteLine ( “ The ultimate conflict begins... ” ); class method Information provided to the method dot (parameters or called “arguments”) 5 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  6. Example: Math Math class methods class methods Example: Me tho d De sc r iptio n E xample Abs( x ) absolute value of x Abs( 23.7 ) is 23.7 Abs( 0 ) is 0 Abs( -23.7 ) is 23.7 Ceiling( x ) rounds x to the smallest Ceiling( 9.2 ) is 10.0 integer not less than x Ceiling( -9.8 ) is -9.0 Cos( x ) trigonometric cosine of x Cos( 0.0 ) is 1.0 ( x in radians) Exp( x ) exponential method ex Exp( 1.0 ) is approximately 2.7182818284590451 Exp( 2.0 ) is approximately 7.3890560989306504 Floor( x ) rounds x to the largest integer Floor( 9.2 ) is 9.0 not greater than x Floor( -9.8 ) is -10.0 Log( x ) natural logarithm of x (base e ) Log( 2.7182818284590451 ) is approximately 1.0 Log( 7.3890560989306504 ) is approximately 2.0 Max( x, y ) larger value of x and y Max( 2.3, 12.7 ) is 12.7 (also has versions for float , Max( -2.3, -12.7 ) is -2.3 int and long values) Min( x, y ) smaller value of x and y Min( 2.3, 12.7 ) is 2.3 (also has versions for float , Min( -2.3, -12.7 ) is -12.7 int and long values) Pow( x, y ) x raised to power y (xy) Pow( 2.0, 7.0 ) is 128.0 Pow( 9.0, .5 ) is 3.0 Sin( x ) trigonometric sine of x Sin( 0.0 ) is 0.0 ( x in radians) Sqrt( x ) square root of x Sqrt( 900.0 ) is 30.0 Sqrt( 9.0 ) is 3.0 Tan( x ) trigonometric tangent of x Tan( 0.0 ) is 0.0 ( x in radians) Co mmo nly use d Math c lass me tho ds. 6 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  7. Methods provide abstraction Methods provide abstraction • An abstraction abstraction hides (or ignores) the right details at the right time. – A method is abstract in that we don't really have to think about its internal details in order to use it. • e.g., we don't need to know how the WriteLine method works in order to invoke it. • Why abstraction? – Easier to reuse – Easier to understand – Divide and conquer Divide and conquer – 7 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  8. Method’ ’s header s header Method • A method declaration begins with a method header. class MyClass { … method static int SquareSum( int num1, int num2 ) header parameter list parameter list method method name name The parameter list specifies the type The parameter list specifies the type and name of each each parameter. parameter. return and name of return type type The name of a parameter in the method The name of a parameter in the method declaration is called a formal argument, formal argument, declaration is called a properties properties or a formal parameter. formal parameter. or a 8 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  9. Method’ ’s body s body Method • The method header is followed by the method body. class MyClass { … static int SquareSum(int num1, int num2) { // method body method int sum = num1 + num2; body return sum * sum; } … } 9 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  10. return Statement Statement return • The return return type of a method indicates the type type of value that the method sends back sends back to the calling calling location. location • A method that does not return a value has a void return type. • The return return statement statement specifies the value that will be returned. – Its expression must conform to the return type. class MyClass { … static int SquareSum(int num1, int num2) { // method body int sum = num1 + num2; return return (sum * sum); int } … } 10 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  11. Calling a method Calling a method • Each time a method is called, the actual arguments actual arguments in the invocation are copied copied into the formal arguments formal arguments . Caller in Caller in int num = SquareSum (2, 3); Main( ) Main( ) static int SquareSum (int num1, int num2) { int sum = num1 + num2; return sum * sum; } See SquareSum.cs 11 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  12. Methods in standard classes Methods in standard classes • The Console Console class – The Write and WriteLine methods • The S tring class String – The Format method: e.g., String.Format( “{0:C}”, 2.0 ) See TestMethods.cs 12 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  13. Methods in standard classes (2) Methods in standard classes (2) • The MessageBox MessageBox class – The Show Show method we will use has four parameters • text to be displayed, caption, buttons, icon. Argument 4: MessageBox Icon Argument 2: Title bar (Optional) string (Optional) Argument 1: Message Argument 3: OK dialog to display button. (Optional) 13 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  14. 1 // Sum.cs 2 // Summation with the for structure. 3 4 using System; 5 using System.Windows.Forms; 6 7 class Sum 8 { 9 static void Main( string[] args ) 10 { 11 int sum = 0; 12 13 for ( int number = 2; number <= 100; number += 2 ) 14 sum += number; 15 16 MessageBox.Show( "The sum is " + sum, 17 "Sum Even Integers from 2 to 100", 18 MessageBoxButtons.OK, 19 MessageBoxIcon.Information ); 20 21 } // end method Main 22 23 } // end class Sum Argument 4: MessageBox Icon Argument 2: Title bar (Optional) string (Optional) Argument 1: Message Argument 3: OK dialog to display button. (Optional) 14 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  15. MessageBox Buttons Buttons MessageBox Me ssageBo x B uttons Desc riptio n MessageBoxButton.OK Specifies that the dialog should include an OK button. MessageBoxButton.OKCancel Specifies that the dialog should include OK and Cancel buttons. Warns the user about some condition and allows the user to either continue or cancel an operation. MessageBoxButton.YesNo Specifies that the dialog should contain Yes and No buttons. Used to ask the user a question. MessageBoxButton.YesNoCancel Specifies that the dialog should contain Yes , No and Cancel buttons. Typically used to ask the user a question but still allows the user to cancel the operation. MessageBoxButton.RetryCancel Specifies that the dialog should contain Retry and Cancel buttons. Typically used to inform a user about a failed operation and allow the user to retry or cancel the operation. MessageBoxButton.AbortRetryIgnore Specifies that the dialog should contain Abort , Retry and Ignore buttons. Typically used to inform the user that one of a series of operations has failed and allow the user to abort the series of operations, retry the failed operation or ignore the failed operation and continue. Buttons fo r message dialo gs. 15 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

  16. MessageBox Icons Icons MessageBox Me ssage Bo x I c o ns I c o n De sc riptio n MessageBoxIcon.Exclamation Displays a dialog with an exclamation point. Typically used to caution the user against potential problems. MessageBoxIcon.Information Displays a dialog with an informational message to the user. MessageBoxIcon.Question Displays a dialog with a question mark. Typically used to ask the user a question. MessageBoxIcon.Error Displays a dialog with an x in a red circle. Helps alert user of errors or important messages. I c o ns fo r me ssage dialo gs. 16 K K 204111 Computer & Programming - Writing Methods I I E M M E Version 2008/1

Recommend


More recommend