Basics

Extra

A method has four possible signatures. Example:

using System;

public class DeclareUseMethodsExtra

// Public operation that takes no parameters and has no return public static void MySubRoutine() {

// code goes here

// Public operation that takes parameters and has no return public static void MySubRoutine(int intParam) {

// code goes here

// Public operation that takes no parameters and has a return public static string MySimpleFunction() {

// code goes here return "MyReturn";

// Public operation that takes in parameters and has a return public static int MyAddFunction(int intParam1, int intParam2) {

return intParam1 + intParam2;

switch (cuhoice.ToStringO) {

—™ Create the different cases for the user input, formatting the return variable appropriately.

case "0":

sReturn break;

= DateTime.Now.ToShortDateStringO;

case "1":

sReturn break;

= DateTime.Ncw.ToShortTimeStringO;

case "2":

sReturn break;

- DateTime.Now.DayOfvVeekToStrinç

case "3":

sReturn break;

- DateTime.Now.ToStringO;

default:

sReturn break;

= "{net supported}";

Note: See the section "Write Your First C# Application, " earlier in this chapter, if you need more information on saving a file.

CAWINNT\System32\cmd.exe _

^HHË Microsoft Windows 2600 [Uersion 5.00.2195]

^^ <G> Copyright 1985 2000 Microsoft Corp. My Doc um

C:\DocuPients and Settings\Administrator>cd C:\CSharp rai C:SCsharp>csc DeclareUseMethods.cs ■Ml Microsoft <R> Uisual CS Compiler Uersion 7.00.9148 EC ^KMttJ &15 ]

My Comp J Copyright CO Microsoft Corp 2000. nil rights reseruei

C:NCsharp>DeclareUseMethods.exe CHOOSE A FORMAT FOR THE CURRENT DATE:

0 - Short Date

1 - Short Time

The current date on the system in short date format is '5/2/2001

C:\Csharp>DeclareUseMethods.exe CHOOSE A FORMAT FOR THE CURRENT DATE:

0 - Short Date

1 - Short Time

The current date on the system in short date format is '9:10 AM'

C:SCsharp>DeclareUseMethods.exe CHOOSE A FORMAT FOR THE CURRENT DATE:

0 - Short Date

1 - Short Time

3 - Date and Time

—E Compile and run the program.

■ The program displays the options for different dates and times and displays results for the current date and time.

IMPLEMENT EXCEPTION ANDLING

When programmatically working with errors, you need to understand exception handling. No matter how good a developer you are, you cannot avoid runtime errors. For example, suppose your program tries to read a file that does not exist. How would you handle this? You would first try to access the file and then, if there is a failure, you would want to have code that runs if the error occurs. If the error occurred, you may want to ask the user to pick a new path for that file and then try again.

If you do not code for handling errors, your code would either stop executing or would move on to the next executable statement. Moving on to the next statement is acceptable in some cases, but not all.

The exception handling in C# is performed by using the keywords try and catch. The code that you want to "try" goes in a block of code after the try statement. After that try code block, you would put in a catch statement. The code that you want to execute in the event that an error occurs would go into a block of code that is after the catch statement.

IMPLEMENT EXCEPTION HANDLING

% Untitled Notepad

IMPLEMENT EXCEPTION HANDLING

% Untitled Notepad

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

0 Type the name of the class you want to create and press Enter.

0 Between the curly braces, create the Main function.

'—0 Create a new variable of type Implement ExceptionHandling.

Q Use the try statement to create a new string variable set to null, and use this variable to call EvaluateString.

D Open your text editor.

—0 Type using System; to import the System namespace and press Enter.

0 Type the name of the class you want to create and press Enter.

—Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

0 Between the curly braces, create the Main function.

'—0 Create a new variable of type Implement ExceptionHandling.

Q Use the try statement to create a new string variable set to null, and use this variable to call EvaluateString.

L-Q Use the catch statement to write out the exception.

Was this article helpful?

0 0

Post a comment