.Net Programming - Financial independence

Exercise and Solution – Variables and Console in Visual Basic

Assignment

This is the first exercise of the series and comes after Visual Basic Tutorial 5. The solution will be posted in one day.

1. Create Three variables :
One of type Decimal assigned to: 5.4.
One of type String assigned to: I love programming.
A last one of type Integer assigned to: 190.

2. Output all three variables in the console.

3. (bonus) Try to output a line containing all the three variables in one String variable. If you can’t do it don’t worry we will look into this in the solution!

Solution

Here is the solution to the first exercise related to the post on the variable and console in Visual Basic.

Module Program
    Sub Main()
        '1. Variable declaration
        Dim DecimalVariable As Decimal
        Dim StringVariable As String
        Dim IntegerVariable As Integer

        '2. Variable initialization
        DecimalVariable = 5.4
        StringVariable = "I like programming"
        IntegerVariable = 190

        '3. Display in Console
        Console.WriteLine(DecimalVariable)
        Console.WriteLine(StringVariable)
        Console.WriteLine(IntegerVariable)
        Console.ReadKey()
    End Sub
End Module

I divided this exercise in three logical parts.
1. The first one (Variable declaration) is for asking the computer to create 3 variables of different types.
2. The second step is for filling up the variables with values. The initial values will ensure they won’t be null when we will want to use them.
3. For the third step, I use all the variables with Console.WriteLine(), each will write an individual line with the value on it.

Try to run that program, it should give you something like the Window below.

Exercise 1 Solution

As for the last question, I wanted you to create a String containing all of the variables and output everything at once.

For doing so, we will have to learn another important concept of Visual Basic that is called, String format.

Here is an example of the String.Format method.

String.Format("Here are the three values at once: {0}, {1}, {2}", DecimalVariable, StringVariable, IntegerVariable)

Keep in mind that the {0}, {1} and {2} will be replaced by the values listed after the String itself. Notice that Decimal Variable is the first one and will replace {0}, StringVariable is the second one and will replace {1} and so on.

Don’t forget that many things in programming start with 0 and not 1. Here is the final code.

Module Program
    Sub Main()
        '1. Variable declaration
        Dim DecimalVariable As Decimal
        Dim StringVariable As String
        Dim IntegerVariable As Integer
        Dim FinalString As String

        '2. Variable initialization
        DecimalVariable = 5.4
        StringVariable = "I like programming"
        IntegerVariable = 190
        FinalString = String.Format("Here are the three values at once: {0}, {1}, {2}.", DecimalVariable, StringVariable, IntegerVariable)

        '3. Display in Console
        Console.WriteLine(FinalString)
        Console.WriteLine(DecimalVariable)
        Console.WriteLine(StringVariable)
        Console.WriteLine(IntegerVariable)
        Console.ReadKey()
    End Sub
End Module

You can now click on Start to run the program!

Exercise 1 Solution 2

This exercise will gave you the basics of variable declaration and initialization as well as some knowledge of String formatting.

Good job everyone! If something is not clear, feel free to ask questions in the comments.

YouTube Video

Next article How to Use the IF Keyword in Visual Basic
Previous article How to Work with Variables in Visual Basic

Related posts

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Comment

Your data will be safe! Your e-mail address will not be published. Also other data will not be shared with third person. Required fields marked as *