Join David Gassner for an in-depth discussion in this video Working with data types, part of Visual Basic Essential Training.
For this exercise, I'm going to start by showing you a few different ways of declaring variables in your Visual Basic applications. I'll work in a project called data types in the 03 variables chapter. Within the data types folder, I'll double-click the data types solution file and open it in Visual Studio Express for Windows desktop. Then, in the Solution Explorer panel, I'll open the project tree, open MainWindow.xml, and double-click to open the Visual Basic file.
And I'll place all the code here in the RunCode subroutine or method. I'll start by declaring a simple string. I'll put in the keyword dim for dimension. I'll assign a variable name of myString, and I'll set an explicit data type of As String. Then I'll assign an initial value of Hello World. This declaration includes all four available parts of a variable declaration. The dim keyword, the name of the variable, the data type and the value.
Next I'll output that value to the string by calling my output method and passing in myString. I'll save the change and I'll run the application. When the application runs, I'll click the Go button. And there's the result. And I can click that button as many times as I like. Next, I'll declare variables of a couple of different data types. The next variable will be called myNumber, and I'll set its data type to Integer. And I'll give it a value of 42. Next I'll create a date, and I'll set its data type to Date. And for this one, I'll use a function called DateValue. The DateValue function receives a string, and it converts it internally to a DateTime value.
I'll pass in a value of November 28th, 2013. So now I have a string, a number, and a date. And I'll take this output command, I'll copy it to the clipboard, and I'll paste it in a couple of times, and I'll change the values that I'm outputting to myNumber and to myDate. I'll run the application and test the code, and I'll see that the literal values that I assigned to the string and the number, our output exactly as I set them.
But that the date is output in a different form. This is a clue that a date value is being handled a little bit differently than strings and numbers. So now, let's change these references a little bit. I'm going to make a copy of these three declarations, and then paste them in above the output commands. And then I'll change the names of these versions to myString2, myNumber2, and myDate2. Notice that until I did that, I was seeing a little squiggly line, telling me there was an error. You can only declare each identifier once within a single scope. Because I had already used myString, I couldn't use it again. Now I'm going to remove the data type declarations from these second versions. Programming languages are typically categorized as static or dynamic languages.
The data types of the variables are determined by the values that are being assigned. But internally, what's really happening, is that the compiler is examining the literal values that I'm assigning and setting the data type without my having to say it explicitly. This is called inferred data typing. Now, you can tell this is happening by saving your code and then moving the cursor over the variable name. And you'll see a little message telling you that this variable is a string. I'll do the same thing with the number and I'm told that it's an integer and the same thing with the date, and I'm told that it's a date. Next, to show how powerful data type inference is, I'll change the numeric value from 42 to 42.5.
I'll save the change and move the cursor over the variable name again, and now I'm told that it's a double value. This sort of programming is perfectly fine in Visual Basic. As long as you're assigning a literal value to a variable, you don't have to put in the ASCII word and the explicit data type. But here's how you can get into a little bit of trouble. Let's say that I declared a variable without a data type up at the top of the code. I'll place the cursor up here, and I'll declare a new variable that I'll call unknownType.
When I save my changes, I get a little squiggly line telling me that there's an error, and when I move the cursor over that, I'm told that this is an unused local variable. Visual Studio wants me use it, so now I'll move the cursor down here. After my explicit variable declarations, and I'll say unknownType equals myString. When I save the change, the squiggly line goes away. But now, it tells me that the unknownType variable is declared as something called an object.
It won't become a string until the application is running. And when that happens, the underlying runtime will have to deallocate the original memory and allocate new memory. For this reason, it's recommended in Visual Basic that you use explicit data typing whenever possible. You'll get better performance and better memory usage. And in fact, there are options that you can set in your Visual Basic programs that turn some of these features off and make Visual Basic a purely static language.
We'll talk about some of those other options later in the course.
- 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