Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Pgina 1 de 4
NOTE Solution file names have the .sln suffix, such as PrimitiveDataTypes.sln. A solution can contain one or more projects. Project files have the .csproj suffix. If you open a project rather than a solution, Visual Studio 2005 will automatically create a new solution file for it. If you build the solution, Visual Studio 2005 automatically saves any updated or new files, and you will be prompted to provide a name and location for the new solution file. 4. On the Debug menu, click Start Without Debugging. The following application window appears:
5.
In the Choose A Data type list, click the string type. The value 42 appears in the Sample value box.
6.
Click the int type in the list. The value to do appears in the Sample value box, indicating that the statements to display an int value still need to be written.
7.
Click each data type in the list. Confirm that the code for the double and bool types also needs to be completed.
mk:@MSITStore:C:\Users\lduran\Desktop\Microsoft%20Visual%20C%20Sharp%20200... 22/11/2010
Pgina 2 de 4
8.
Click Quit to close the window and stop the program. Control returns to the Visual Studio 2005 programming environment.
The showFloatValue method runs when you click the float type in the list box. This method contains three statements: The first statement declares a variable named var of type float. The second statement assigns var the value 0.42F. (The F is a type suffix specifying that 0.42 should be treated as a float value. If you forget the F, the value 0.42 will be treated as a double, and your program will not compile because you cannot assign a value of one type to a variable of a different type in this way.) The third statement displays the value of this variable in the value TextBox on the form. This statement requires a little bit of your attention. The way in which you display an item in a TextBox is to set its Text property. You did this at design time in Chapter 1 using the Properties window. This statement shows you how to perform the same task programmatically, using the expression value.Text. The data that you put in the Text property must be a string (a sequence of characters), and not a number. If you try and assign a number to the Text property your program will not compile. For this reason, the statement simply displays the text 0.42F in the TextBox (anything in double-quotes is text, otherwise known as a string). In a real-world application, you would add statements that convert the value of the variable var into a string and then put this into the Text property, but you need to know a little bit more about C# and the .NET Framework before we can do that (we will cover data type conversions in Chapter 11, Understanding Parameter Arrays, and Chapter 19, Operator Overloading).
mk:@MSITStore:C:\Users\lduran\Desktop\Microsoft%20Visual%20C%20Sharp%20200... 22/11/2010
Pgina 3 de 4
3.
In the Code and Text Editor window, locate the showIntValue method listed here:
TIP Another way to find a method in the Code and Text Editor window is to click the Members list that appears above the window, to the right. This window displays a list of all the methods (and other items). You can click the name of a member, and you will be taken directly to it in the Code and Text Editor window. 4. Type the following two statements at the start of the showIntValue method, after the open curly brace:
private void showIntValue() { int var; var = 42; value.Text = "to do"; }
5. On the Build menu, click Build Solution. The build will display some warnings, but no errors. You can ignore the warnings for now. 6. In the original statement, change the string to do to 42. The method should now look exactly like this:
mk:@MSITStore:C:\Users\lduran\Desktop\Microsoft%20Visual%20C%20Sharp%20200... 22/11/2010
Pgina 4 de 4
TIP If you have edited the source code since the last build, the Start Without Debugging command automatically rebuilds the program before starting the application. 8. 9. 10. 11. Select the int type in the list box. Confirm that the value 42 is displayed in the Sample value text box. Click Quit to close the window and stop the program. In the Code and Text Editor window, find the showDoubleValue method. Edit the showDoubleValue method exactly as follows:
16.
mk:@MSITStore:C:\Users\lduran\Desktop\Microsoft%20Visual%20C%20Sharp%20200... 22/11/2010