Learning VB.NET: Working with Variables, Conditionals, and Console Input
(Page 1 of 4 )
This is the third article in a series covering how to learn VB.NET by developing a text-based game. The last article covered working with the console and how to change color schemes. This one will discuss defining variables, conditionals, and Console input. So if you're ready to keep learning, then keep reading!
Defining Variables
We need to cover a few more things before we can begin work on the game itself. The first is very important, and I'm afraid that perhaps I've already put it off too long. In order to make any sort of useful program, we need to be able to work with variables. Languages differ greatly in how they treat variables, and Visual Basic is no exception here. So, let's get started.
Let's say that we want an Integer object called number. There are two things that are important here: the name of the variable (number) and the type of the variable (Integer). We need to explicitly define both when we create a new variable because Visual Basic is both strongly- and statically-typed. Here's how it's done:
Dim number As Integer
The above declaration is fairly straightforward, as it should be. The thing to note here is the Dim keyword. The Dim keyword is designed to declare and allocate space for a variable. Also, note how the variable name goes before the type name, which is different from many other languages. The variable name and the type name are separated by As (after all, we're declaring number as an Integer ).
Now that we've declared a variable, we need to assign something to it. Naturally, we'll have to assign an Integer to our variable. Assignment is done with the assignment operator and resembles assignment in other languages:
number = 3
Above, there are no surprises. We put the variable, the assignment operator, and then the value to be assigned to the variable.
We can also define a variable and assign it a value in the same step. Let's combine these processes using the above example:
Dim number As Integer = 3
As you can see, we simply combine the declaration step with part of the assignment step. This looks neat with the example we've chosen because we're only working with an Integer, and we're not instantiating a new object. However, let's consider a more complex object that we must explicitly instantiate. Let's create another variable containing a Date object:
Dim anotherVariable As Date = New Date ()
Aside from declaring and assigning to a variable, the above line of code does something else very important: it instantiates a Date object. The Date type is used to represent a date and time. Instantiation in Visual Basic is not wildly different from instantiation in other languages, as you can see above. We simply use the New keyword and call the appropriate constructor.
Back to declaration and assignment, though. The above code does look a bit redundant. The type is essentially specified twice, and very close together, too. It looks a bit ugly, but, thankfully, there's another way to do this. In fact, the above method is discouraged, and I only brought it up so that you may recognize it if you ever encounter it. The other method is more concise:
Dim anotherVariable As New Date ()
This looks much neater and more elegant, and it's the style you should always use if you need to declare a variable and assign a newly instantiated object to it in one step.
Next: Reading Console Input >>
More Visual Basic.NET Articles
More By Peyton McCullough