1 / 14

Packaging Code: Methods, Properties, Classes, Namespaces, and More!

Learn about the common ways of packaging code, properties, methods, classes, namespaces, and the prepackaged codes available in the .NET framework class library.

sdotson
Download Presentation

Packaging Code: Methods, Properties, Classes, Namespaces, and More!

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 4-Methods Dr. John P. Abraham Professor UTPA

  2. Common ways of packaging code • Properties • Methods • Classes • Namespaces (related classes are grouped into a namespace) • Prepackaged codes are available in .NET framework class library

  3. Class Math • From namespace System • Provides a collection of methods for mathematics Console.Writeline(Math.sqrt(900.0)) Abs(x) Ceiling(x) rounds x to the smallest integer, greater than x. Int32 numofGalons = Convert.ToInt16(Math.Ceiling(totalSqFt/450)); Floor(x) rounds x to the largest integer, less than or equal to x. Pow(x,y) x raised to the power of y

  4. Example of floor and ceiling • Floor: Floor(2.10) = 2 Floor(2.00) = 2 Floor(1.90) = 1 Floor(1.80) = 1 • Ceiling: Ceiling(0.00) = 0 Ceiling(0.10) = 1 Ceiling(0.20) = 1 Ceiling(0.30) = 1

  5. Main declared as static • Why must Main be declared static? • It is the applications entry point. • Declaring main as static allows the execution environment to invoke main without creating an instance of the class • Public static void Main (string args[]) • The args allows one to call the application and pass parameters in from command line.

  6. Declaring Methods with Multiple Parameters • Parameters are specified as a comma-separated list. Actual parameters appear in the call and Formal parameters appear in the method declaration. • Both actual and formal parameters must have exactly same number of parameters • Both actual and formal parameters must agree in their types. • Values are assigned according to the order of appearance rather than variable names.

  7. Method-call stack and activation records • When an application calls amethod the return address of the calling method is pushed on to the program-execution stack (method-call stack). • If series of methods are called, successive return addresses are pushed onto the stack (LIFO). • The program-execution stack also contains the memory for the local variables used in each invocation of a method during the application’s execution. This is known as the activation record (stack frame) of the method call. • When the method completes and return to its caller, the activation record for this method call is popped off the stack, and those local variables are no longer known to the application. • If more methods calls occur than available memory for program-execution stack, stack overflow error occurs.

  8. Argument Promotion and Casting • Math.Sqrt(4) • Expects a double argument. However, accepts an integer and promotes it to a double and returns the result of 2.0 • When a method expects an integer, if you pass a real number, you get an error, since the method can’t truncate numbers arbitrarily. Float can be promoted double, but double can’t be converted to float implicitly. • If you want to go the other way you have cast it.

  9. The .NET framework library • Some examples • System.Windows.Forms • System.Windows.Controls (for WPFs) • System.Linq (for language integrated querry) • System.IO (for files, keyboards, monitors, etc) • System.Web (creating web applications) • System.Text (manipulate characters and strings)

  10. Random Number generation • Needed for your card game assignment coming up later. • Objects of class Random can produce random byte, int and double values. • Random randomnumbers = new Random(); • int randomValue = randomnumbers.Next(); • Generates random values in the range 0 to +2,147,483,646 • This uses the current time as seed value to generate a psuedorandom number. • If you place number .NEXT(1,53), it will generate a random value 1 to 52. • This can lead to a repeating exact same sequence. To avoid that you can use a seed value • Random randomnumbers = new Random(seedvalue);

  11. Method overloading • Methods of the same name can be declared in the same class, as long as they have different set of parameters(number, type and order). Examples: Public int Square (int intValue) {Return intvalue*intvalue;} Public double Square (double doubleValue) {return doubleValue*doubleValue;}

  12. Recursion • Run some examples here.

  13. Recursion Method that calls itself. A recursive method solves only a simple problem (base case). Any thing other than the base case it calls itself with a slightly simpler problem. Eventually it becomes the base case for which it knows the answer. Run some examples here.

  14. Passing Arguments: value and reference Applying the ref keyword to a parameter declaration allows you pass a variable to a method by reference. The method now will be able to modify the original variable. The original variable must be initialized before passing to the method. If you do not want to initialize the variable, you can use the modifier out. This means that the called method will assign a value to the original variable. If not a compiler error will result.

More Related