Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
Problem
You want to develop a Console application that converts between the Fahrenheit, Celsius, and kelvin temperature systems. Solution
Sample code folder: Chapter 01\ Console Version Create a Windows Console application, and add logic to perform all the calculations based on the user's input. First, read through Recipe 1.1 for background information on using Visual Studio and on converting between the various temperature systems. Discussion
Start Visual Studio 2005, and then use the File Module Module1 Sub Main( ) End Sub End Module
There are a few different ways to rename the module. If you only want to change the name in the code, just replace the word "Module1" with something like "Convert-Temperature": Module ConvertTemperature
Unfortunately, this requires you to make a change to the project's properties. Before the change, Visual Studio planned to start the program from the Sub Main routine in the Module1 module. But since you changed the name, there is no longer a Module1 for Visual Studio to use. To modify the properties, select the Project If you want to avoid all of this unpleasantness, rename the module's filename instead of its name in the code. To do this, right-click the Module1.vb file in the Solution Explorer, choose the Rename command from the shortcut menu that appears, and give it a new name such as ConvertTemperature.vb. (Don't forget the .vb extension.) Visual Studio will change the module name as well and fix up all the other loose connections. All of the conversion code will go in the Sub Main routine: Module ConvertTemperature Sub Main( ) ' ----- The program starts here. Dim userInput As String Dim sourceType As String On Error Resume Next ' ----- Display general instructions. Console.WriteLine("Instructions:" & vbCrLf & _ "To convert temperature, enter a starting " & _ "temperature, followed" & vbCrLf & _ "by one of the following letters:" & vbCrLf & _ " F = Fahrenheit" & vbCrLf & _ " C = Celsius" & vbCrLf & _ " K = kelvin" & vbCrLf & _ "Enter a blank line to exit." & vbCrLf) ' ----- The program continues until the user ' enters a blank line. Do While True ' ----- Prompt the user. Console.WriteLine("Enter a source temperature.") Console.Write("> ") userInput = Console.ReadLine( ) ' ----- A blank line exits the application. If (Trim(userInput) = "") Then Exit Do ' ----- Determine the source type. userInput = UCase(userInput) If (InStr(userInput, "F") > 0) Then ' ----- Start with Fahrenheit. sourceType = "F" userInput = Replace(userInput, "F", "") ElseIf (InStr(userInput, "C") > 0) Then ' ----- Start with Celsius. sourceType = "C" userInput = Replace(userInput, "C", "") ElseIf (InStr(userInput, "K") > 0) Then ' ----- Start with kelvin. sourceType = "K" userInput = Replace(userInput, "K", "") Else ' ----- Invalid entry. Console.WriteLine("Invalid input: " & _ userInput & vbCrLf) Continue Do End If ' ----- Check for a valid temperature. userInput = Trim(userInput) If (IsNumeric(userInput) = False) Then Console.WriteLine("Invalid number: " & _ userInput & vbCrLf) Continue Do End If ' ----- Time to convert. If (sourceType = "F") Then ' ----- Convert from Fahrenheit to other types. Console.WriteLine(" Fahrenheit: " & userInput) Console.WriteLine(" Celsius: " & _ (Val(userInput) - 32) / 1.8) Console.WriteLine(" kelvin: " & _ ((Val(userInput) - 32) / 1.8) + 273.15) ElseIf (sourceType = "C") Then ' ----- Convert from Celsius to other types. Console.WriteLine(" Fahrenheit: " & _ (Val(userInput) * 1.8) + 32) Console.WriteLine(" Celsius: " & userInput) Console.WriteLine(" kelvin: " & _ Val(userInput) + 273.15) Else ' ----- Convert from kelvin to other types. Console.WriteLine(" Fahrenheit: " & _ ((Val(userInput) - 273.15) * 1.8) + 32) Console.WriteLine(" Celsius: " & _ Val(userInput) - 273.15) Console.WriteLine(" kelvin: " & userInput) End If Loop End End Sub End Module
Running the program opens up a command window. You will immediately be prompted to enter a source temperature. The program continues to convert values until it detects a blank line for input. Here is a typical short session: Instructions: To convert temperature, enter a starting temperature, followed by one of the following letters: F = Fahrenheit C = Celsius K = kelvin Enter a blank line to exit. Enter a source temperature. > 37c Fahrenheit: 98.6 Celsius: 37 kelvin: 310.15 Enter a source temperature. >
Console applications bring back memories of those pre-Windows days when the 80-by-24-character console display was the primary user interface mechanism on the IBM PC platform. Text input and output, and maybe some simple character-based graphics and color, were all the visual glitz that a programmer could use. Console applications in .NET use that same basic text-presentation system as their primary interface, but they also include the full power of the .NET libraries. For the actual user interaction, the Console object takes center stage. It includes features that let you display text on the console (Write(), WriteLine()), retrieve user input (Read(), ReadKey(), ReadLine()), and manipulate the console window in other useful ways. The temperature conversion program uses the Console object and some basic temperature formulas within its core processing loop. First, it gets a line of input from the user and stores it as a string: userInput = Console.ReadLine( )
The input must be a valid number, plus the letter F, C, or K. The letter can appear anywhere in the number: 37C is the same as C37 is the same as 3C7. Once the program has extracted the numeric temperature and its source system, it performs the conversion; it then outputs the results using the Console.WriteLine() method. See Also
The recipes in this chapter should be read together to gain a full understanding of general .NET application development concepts. |
Категории