Visual C # 2005 Quick Start writing method

Source: Internet
Author: User
Tags readline stub visual studio

In the following exercise, you will create an application that contains a method that calculates the amount of a consultant's charges-assuming that the consultant charges a fixed fee each day, depending on how many days of work they will charge. First you develop the application's logic, and then use the Generate Method Stub wizard to write the method used by this logic. Next, we'll run the method in a console application to get the final impression of the program. Finally, we will use the visual Studio 2005 debugger to check the method invocation.

Developing application logic

1. In Visual Studio 2005, open the \microsoft press\visual CSharp Step under the My Documents folder by Step\chapter 3\ The dailyrate item in the Dailyrate subfolder.

2. In Solution Explorer, double-click the Program.cs file to display the program in the Code and Text Editor window.

3. Add the following statement to the Run method body:

double dailyRate = readDouble("Enter your daily rate: ");
int noOfDays = readInt("Enter the number of days: ");
writeFee(calculateFee(dailyRate, noOfDays));

When the application starts, the Run method is called by the Main method.

The code block that you just added in the Run method calls the Readdouble method (which is about to begin writing this method) so that the user can enter the consultant's daily rate. The next statement calls the Readint method (which we write immediately) to get the number of days. Finally, the Writefee method (waiting to be written) is called to display the results on the screen. Note that the value passed to Writefee is the value returned by the Calculatefee method (the last method to write), which obtains the day rate and the number of days, and calculates the total amount to be paid.

Note that because the Readdouble,readint,writefee or Calculatefee methods have not yet been written, IntelliSense cannot automatically list them when you enter the above code. In addition, do not attempt to build the program, because it is sure to fail.

To write a method using the Generate Method Stub wizard

1. In the Code and Text Editor window, click the Readdouble method call in the Run method.

A small underline icon is then displayed below the first letter ("R") of the readdouble. Moving the mouse pointer to the letter "R" automatically appears with an icon. Hovering over this icon displays a ToolTip: the option to generate the method stub (Shift + Alt + F10), and provides a drop-down menu. When you click the Drop-down menu, you see an option: Generate a method stub for "readdouble" in "Dailyrate.program".

2. Click the Readdouble method stub option in Generate Dailyrate.program.

The Build Method Stub wizard then examines the call to the Readdouble method, determines the parameter type and return value, and generates a method with the default implementation, as follows:

private double readDouble(string p)
{
  throw new Exception("The method or operation is not implemented.");
}

The new method is created using a private qualifier. The method body currently only throws an exception. We will replace the principal with our own statement in the next step.

3. Remove throw new Exception (...) from the Readdouble method. ; statement, replace it with the following line of code:

Console.Write(p);
string line = Console.ReadLine();
return double.Parse(line);

The above code block prints the string in the variable p to the screen. The variable is the string parameter that the calling method is passed, which contains a message that prompts the user to enter a daily rate. The user enters a value that is read in a string by the ReadLine method, and is passed double. The Parse method converts to a double value. The result is passed back as the return value of the method call.

Note The ReadLine method is a method that is associated with WriteLine, which reads the user's input from the keyboard until the ENTER key is pressed. The text entered by the user is returned as the return value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.