To start up Visual Studio Express for the first time, go to the Start screen on Windows 8 or the Start menu on Windows 7 and look for the Menu Choice or tile for Visual Studio Express for desktop. You'll see that it's named VS Express. If this is the first time you've started up Visual Studio on your system, you'll be asked to register and provide a serial number. Follow the prompts and check your email for the serial number that will be emailed to you by Microsoft. Registration is free, but required.
Once you've started it up you'll be able to create your first project. To create a project, go to the Menu and choose File > New Project. Visual Studio Express for Windows Desktop lets you create applications using three programming languages, Visual Basic, Visual C#, and Visual C++. Within the Visual Basic section, choose the Windows category and you'll find a number of options. You can build three different types of applications, a Windows Forms application, a WPF or Windows Presentation Foundation application, or a Console application.
Create a Console Application. After selecting that option, name the application Console First App. I'm placing the application and project in my exercisefiles02gettingstarted folder, but you can place it anywhere you like. A Console App starts off as a simple Visual Basic file called a module. And it has a subroutine or a method called Main. This Main method will be executed automatically as the application starts up.
So place the cursor between the sub declaration and the end sub declaration. And any code you put there will be executed. Our first step is to execute a simple, hello world, outputting a simple string. And I'll use an expression that looks like this. Console.WriteLine. In Visual Studio, you can auto complete code by pressing the Tab key. So I started typing write and then I moved the cursor to WriteLine and pressed Tab.
Now I'll put in an opening parenthesis and then a simple string of, hello world. In Visual Basic you don't need to finish the line with a semi colon or any other punctuation. All you need is the end of the statement and in this case that's the closing parenthesis. I'll save my changes with Ctrl+s, and then I'll run the application. When you're working with a console application, if you run with debugging, you'll see that the console window flies by very, very fast, but doesn't stay open.
If all you're doing is outputting content into the console, run without debugging, and you can do that by going to the Menu and choosing Debug > Start Without Debugging, or pressin Ctrl+F5. And there is the result. I see the string, Hello World, and a prompt, press any key to continue. And when I press any key the console window closes. Now I'll add a few more lines of code. I'm going to declare a couple of simple variables.
In Visual Basic you declare a variable using the key word dim. I'll talk about what that keyword does and what it means later. But for now, all you need to know is that every variable should start with dim. Then, assign the variable name. I'll set it as value one and I'll give it a value of five. If you already know c style languages, such as C, C++, or Java, you might be wondering at this point whether you need to declare data types. That is, whether a variable is an integer, a double, a string, or some other type.
And the answer is, in Visual Basic, you don't have to. You can follow a pattern known as typeless programming, where you let the Visual Studio compiler infer the data types. In this case, I'm saying that the value is 5 and the compiler will respond by saying that the value 1 is an integer. Next I'll declare a second variable. I will say Dim value2 equals 20. You may have noticed that I was prompted for a keyword called as, but I'm not going to use that right now.
Finally I will declare a third variable named total. And I'll get its value by adding the first two variable together with this statement. Dim total equals value one plus value two. Just as I did earlier, with Autocompleting Command, you can also autocomplete variable names. For example, if I type in just val. I'll see a list of all available identifiers that have that string and I can choose the one I want and press tab. Now, I'll output to the console again.
I'll once again use Console.WriteLine and I'll output a concatenated string. That's two strings put together. I'll start with the result is and then I'll append to that by putting in a plus operator and then I'll output total.ToString. That's how you convert a number to a string. I'll save my changes, once again with Ctrl S. And I'll run with Control F5, and there's the result.
25. So that's a look at how to build a very simple console application in Visual Studio using the visual basic programming language. We'll talk a a lot more about this code and the concept of typeless programming later on in the course. And for most of this course I won't be building console applications. I'll be building Windows Presentation Foundation or WPF-based desktop applications but if you need to use a console application, this is how you get started.
- What is Visual Basic?
- Learning Visual Basic syntax
- Declaring variables and data types
- Working with numbers, Boolean values, and dates
- Using strings, words, and characters
- Repeating blocks of code with loops
- Evaluating conditions with if, then, and else
- Debugging and handling exceptions
- Managing ordered data with arrays
- Managing application logic with modules
- Defining custom classes
- Storing data with instance fields