Learning VB.NET Through Text Game Development - Getting Started continued
(Page 4 of 4 )
So, now that we know what a module is, what's the “sub” thing? It stands for subroutine and is equivalent to a function with a void return type—that is, it returns no value and instead performs some sort of operation with a desirable side effect. The parenthesis, of course, can be filled with parameters, but we'll get to that later. It's part of a broader group of procedures, which includes functions as well. This is why you'll hear this particular procedure be called a Sub procedure.
This particular procedure, Main, is special. It's what is called when the program executes, so it's the heart of the program. In Visual Studio, press the green arrow in the toolbar, or press F5. This will start debugging, a process we're not interested in right now, but we are interested in running the program, which is what will happen. You may notice a console appear real fast and then disappear. This is our program executing. The console disappears so fast because, of course, our program does nothing at the moment. Let's fix this. Let's print out “Hello World” and then wait for a key to be pressed before exiting:
Sub Main()
System.Console.WriteLine("Hello World!")
System.Console.ReadKey()
End Sub
Now start the program up. The program will print “Hello World” in the console and then wait for a key to be pressed before terminating, as expected.
The above example shows two procedures being called. Nothing is strikingly odd about the two calls—they are done in the same manner as in other languages. What is worth nothing, however, is our use of the Framework Class Library here. In the example, we access the System namespace, which is where the base classes are stored. We can actually omit the namespace here, but I just wanted to point out its existence. The following code is equivalent:
Sub Main()
Console.WriteLine("Hello World!")
Console.ReadKey()
End Sub
In the System namespace, we access the Console type. This type provides shared (static) procedures that we can use to control the console, displaying information to the user and reading information from the user. We'll be using this type a lot since our application is console-based.
Finally, we access the procedures WriteLine and ReadKey. WriteLine does what its name implies: it simply writes a line of text out to the console. ReadKey reads a key press and returns information about the key pressed (which we have no use for and therefore do not store).
I'm afraid that's all I have room for in this article. Be sure to check back next week though. I'll be picking up where I left off, with more information about WriteLine, and move on to more complex matters. See you then!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |