One of the first things you need to learn in a new programming language is how to store data in memory. This video examines the principles behind variables, constants, and literals.
- [Instructor] One of the first things you need to learn in a new programming language is how to store data in memory. For a complete understanding of variables, we'll look at these three related topics in this chapter, variables, constants and literal values. A variable represents a storage location in memory. By definition, the value stored in the variable can change during the application execution. Constants, on the other hand, represent a never-changing value. Once set, the value will never change. In code, the constant and variable declarations look similar. Here is an example. The only difference is the first word on the declaration. Dim for our variable and C-O-N-S-T or const for a constant. A literal is an exact value expressed as itself. That statement makes more sense when looking at some literal examples. The colored text on the right side of the equal sign are examples of literal values. The variable value on the left is changed to the literal value on the right. On the top are the integer literal values of 21 and 98. They are literally that exact number. Then below that we have 8.375. That and the line below that is a double literal. Then we have c equals True. That is a Boolean literal. And d equal double quote hello double quote. That is a string literal. Literals are used whenever you want to assign a known value. They work everywhere in your VB code. Here on line six, I'm declaring a width variable for type integer and assigning it the literal value of 20. On line seven, I'm creating a constant and assigning it the literal value of C:\. Then I will use those and pass them into the functions on line 10 and 11. On line 10, I'm passing a literal integer and a variable to the function CalculateArea. On line 11, I'm passing a literal string and a constant string to the SaveToFile function. .NET and VB are a strong type system. Every variable, constant and literal has a specific type. For a variable, this means that it can only hold a value of its defined type. Well, that's true for simple variables, it gets more interesting when talking about object-oriented programming and class inheritance. For this discussion, let's stick with the simple definition. Here's an example of an integer variable. It's declared as an integer on line 35. On line 38, we can assign the literal value 10 because it matches the variable data type. On line 41, there's an error because a Boolean value is not an integer. In most cases, you wouldn't want to have a Boolean value in an integer. Visual Basic is flexible however, if you prefer, you can convert between types. You can do that explicitly with the conversion tools or configure your code to support automatic conversion. We'll look at conversion later. To be clear, you can never store a non-integer value in this variable but you can convert it before assigning it to the variable. The variable is always initialized to a value before it's use in code. This can be an explicit initialization where you provide the value or an implicit initialization where the variable is set by VB to the default value for that data type. On line 18, we are initializing the value at declaration time. On line 21, we do not set an initial value, so it is implicitly set to the integer default value of zero. Each data type has a default value that is sensible for that type. It could be zero, false, 0.0 or nothing which is called null in other programming language. A variable can be of any valid .NET type. Constants, on the other hand, must be typed by they have a limited set of types available. Here's a list of the types you can use in a constant. Let's look at how to declare a variable. A quick break down of the structure of a variable declaration. A common way to create local variables is with the dim keyword. Dim is an ancient word from the old Basic days. Depending on what documents you read, it either stands for dimension as in dimension an array or it is an acronym for declare in memory. In addition to the dim keyword, there are a set of access modifiers for variable declarations. Here are two common modifiers, private and public. When you use the access modifiers, you don't need to also have the dim keyword. Access modifiers are important when declaring variables within classes and modules. We won't look at them in this chapter. Next is the identifier. What most people would call the variable name. There are a few simple rules for names that we'll see in the code examples. Next is the as keyword and then the variable data type. And last on the line is the initializer. When you want to set your own value for the variable at the same time you declare the variable. That's enough details for now. Let's see some code in Visual Studio.
- Visual Basic syntax
- Using .NET classes in your application
- Declaring variables and data types
- Working with numbers and dates
- Using strings
- Repeating blocks of code with loops
- Evaluating conditions with if, then, and else
- Debugging and handling exceptions
- Working with lists