The Basics of VB.NET Through Text Game Development
(Page 1 of 4 )
This is the second article in a series covering how to learn VB.NET by developing a text-based game. The last article gave a basic introduction into the development of our application, and this one will pick up where it left off. So if you're ready to keep learning, keep reading!
More on WriteLine
Before we move on, I'd like to point out a few more things concerning WriteLine. First, although we passed a String (the expected type of a series of characters) as an argument earlier, we can pass just about anything as an argument, and WriteLine will print its textual representation. For example, here we print out an Integer (Visual Basic's version of a thirty-two bit signed integer, or the System.Int32 type—there is also a System.Int16, Short and a System.Int64, Long):
Console.WriteLine(4)
If you place the above line of code into our module and run it, the number four will be printed out. If we pass any type of object, a textual representation of it will be printed out as well. How is this representation determined? The object's ToString procedure is called on to return a String. This procedure is defined in the Object class, which is the ultimate base class of everything, though we do not need to explicitly inherit from it when constructing a new class. By default, the procedure will simply return the name of the class, but this procedure can be overridden to return any String. We can modify our program to call ToString:
Sub Main()
Console.WriteLine("Hello World.".ToString())
Console.ReadKey()
End Sub
The effect is the same.
The second thing I'd like to point out is WriteLine's ability to work with format strings. Let's say we have two objects. One is an Integer and one is a String. We need to print both of them out on the same line, along with some other text. One way to do this would be to append everything together using the concatenation operator, &:
Console.WriteLine("Two objects: " & 4 & ":-)")
This approach certainly works, but it's a bit ugly, and would be even uglier if we had more objects. A better way is to use a format string to specify what will go where. Here, we use a format string:
Console.WriteLine("Two objects: {0}{1}", 4, ":-)")
This example will produce the exact same output as the earlier example, but this version looks a lot neater. Notice how, above, we have two bracketed numbers, starting with 0. These bracketed numbers will be replaced with objects passed as arguments in the exact same order that the objects are passed. So, {0} is replaced by the first object passed, {1} is replaced by the second, and so forth.
Next: Working with the Console >>
More Visual Basic.NET Articles
More By Peyton McCullough