Programming | | Notes on Visual Basic .NET Console Application: |
Visual Basic .NET: As we start programming, we will design, write code, and run programs at first in the Console environment. This allows us to focus on the code first. After Module 3, the class will then apply forms for object-oriented programming. In addition, you will see newer methods (not seen before) and you must be prepared to rethink and apply these new methods.
A review of the Visual Basic window components: When working with this IDE, you will be using and interacting with four different windows set around and below this environment (solution explorer, properties, output, and the tool box). Instead of the Minimize button, these windows have Pin buttons that allow you to pin and minimize the window to the side of the window environment. For the most part, try to keep all windows open and pinned-up. Building and running your Visual Basic Console Application: Through the Console application, before you run your program you should select Build from the menu and then Build “file name”. This process will read each line and test the program. If you have errors, it will find and locate the lines which contain the errors. To run the program you must select Debug from the menu and then Start or just press F5.
Remarks - a single quote ('): Better used with and known as Internal documentation. This is used to include explanatory remarks in a program. As you can see in the above program, they are used in the FlowerBox as well as within the program. It is good programming practice to begin all procedures and functions with a brief comment describing the functional characteristics of the procedure (what it does), for your own benefit and the benefit of anyone else who examines the code.
' ***************INPUT*****************
Variable: Again to review a variable, it is simply defined as a name that can contain a value. Programming involves giving values to these names and presenting them in some form to the user. A variable has a type, which is defined by the kind of value it holds. If the variable holds a number, it may be an integer, floating decimal, or long integer.
Declaring Variables: Before variables are used, all variables have to be declared. Declaring a variable means defining its type, and optionally, setting an initial value (initializing the variable). Variables do not have to be initialized (assigned a value) when they are declared, but it is often useful. For example: Bill, TipPercent, Tip, TotalBill are four variables used as shown below.
The Dim statement: This statement is used at module, class, structure, procedure, or block level to declare and allocate storage space for variables. Below is an example of the DIM statement - More information is to come with the DIM statement.
Dim Name As String
As String: The String class of the .NET framework provides many built-in methods to facilitate the comparison and manipulation of strings. It is now a trivial matter to get data about a string, or to create new strings by manipulating current strings. The Visual Basic .NET language also has inherent methods that duplicate many of these functionalities. For example:
Dim Name As String
As Integer: Integer variables are stored as stringed 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647. The Integer data type provides optimal performance on a 32-bit processor, as the smaller integer types are slower to load and store from and to memory. Note: the Integer data type can be converted to the Long, Single, Double, or Decimal data type without encountering a System.Overflow Exception error. For example:
Dim Age As Integer
As Decimal: Decimal data type provides the greatest number of significant digits for a number. It supports up to 29 significant digits and can represent values in excess of 7.9228 x 10^28. It is particularly suitable for calculations, such as financial, that require a large number of digits but cannot tolerate rounding errors. You can use Decimal variables for money values. The advantage is the precision of the values. The Double data type is faster and requires less memory, but it is subject to rounding errors. The Decimal data type retains complete accuracy to 28 decimal places.
Dim Cost As Decimal
Console.Write() Functions: Write function inserts commas between items and quotation marks around strings as they are written to the file. When Write is used to write data to a file, only the following data formats are supported. Several universal assumptions are followed so the data can always be read and correctly interpreted using Input, regardless of locale:
Console.Write("What is your Name?" & Name)
Console.WriteLine() Functions: WriteLine inserts a newline character (that is, a carriage return–linefeed, or Chr(13) + Chr(10)), after it has written the final character in Output to the file. Console.ReadLine() Functions: In short, the Console.Read holds the console application window until the user presses a keyboard button. This is used to hold the console window for the user to read and apply input to the program. Interacting With The Visual Basic program: VB is a very simple language to pick up, and yet it can accomplish a great deal. The best aspect of the language is its close resemblance to English. Now let’s look at a very simple code to review some of the Visual Basic keywords that you must know and will use as you start building programs. The below example program will read your name and print your name. Programming - Example Problem #2: The Visual Basic Console Application (Tip Calculator):
Example Problem #2: The Visual Basic Console Application (Tip Calculator): Below is a simple example of a Visual Basic console program where you created the Program Flowchart of the Tip Calculator. Notice and review the DIM statement, WriteLine and ReadLines, comment lines, and the VB structure. This shows some of the basic keywords that you will use to create and build programs in the VB console applications. Try to copy this code to the Visual Basic Console Application and run the program. When saving this program please review the file name and location of the program as shown below.
Where do you paste The Flower Box?
The following link will load up the Flowerbox page so you can copy the flowerbox template structure:
Example Problem #2A: Belore getting start look at your flowchart of this program in example 1 and review the inputs and Variables. Key the following code and run the program. During run time you will need to type your the amount and tip precentage in the console window. Also review the asterik comment lines shown below.
The IntelliSense window:
Is a great device that is user friendly. As shown above (to the right of the correct code), this is a context-sensitive programming feature that aids and reduces programming errors. Most of the time it will show up after a period. Press the Tab to accept highlighted selection, press ESC to close the IntelliSense window. Please explore this method when writing code. Also note, as you are coding, if you do not follow the rules and mistype keywords or variables, you will see blue and/or red lines that indicate coding errors.
Example Problem #2B: You are to modifiy the present program by typing in the information below the input lines as shown below.
Example Problem #2C: You will now add the output lines:
Example Problem #2D: Note - we need to enhance the output with the Tip Amount. Can you discuss with the class how would this look like and where would it be located (above the Total Bill)? Also can you create some spaces in between the input and output lines and try to ehance the output a little?
Printing in Visual Basic with the Console Applications: Using the console application, you must print the code through the code window by selecting File and then Print. To print the output during run time you will use the paint program. So you will have two different print outs that should be turned in (code and output).
To print the output during runtime you will first need to run the console application and use the “Snipping Tool Application“ to copy the console screen into the paint program. You will then need to open the print program and paste the information from the click border by selecting Edit menu and Paste. You should then see the output as an image/picture. Change the attributes through the Image menu (this will make the letters black and the background white) and resize if needed. Then just select File and Print to print the run time information.
Directions for turning in Example problem- Virtual:
Virtual No Printing just upload the assignment files to Canvas Module 1 Example Problem 2. You will NOT need to print out any documents just submit this example problem in the Canvas drop box. Note you should have two screen shots: example 2 code and the console output screen. Put all screen shots (images) in a Word Document and send me the file. Save the Word document as the example problem: Example Problem 2 to Canvas Programming Module 1 drop box. Please note, that you will need to re-save this file as a pdf file before uploading. Here is an example video on how to do that: See Video > > >
Teacher's Comments: You are asked to complete the example problem above and print out both the program code and the console application screen during run time, to be turned in together. Try to save all you work. Understanding all of the code lines is important as we move forward with Visual Basic.
|