Variables in Visual Basic 2008 are more than just names or placeholders for values. They’re intelligent entities that can not only store but also process their values. I don’t mean to confuse you all, but I think you must be told: Visual Basic 2008 variables are objects. That’s because a variable that holds dates is declared as such with the following statement:
Dim expiration As Date
Then you can assign a date value to the expiration variable with a statement like this:
expiration = #1/1/2003#
So far, nothing out of the ordinary; this is how you use variables with any other language. In addition to holding a date, however, the expiration variable can manipulate dates. The following expression will return a new date that’s three years ahead of the date stored in the expiration variable:
The new date can be assigned to another date variable:
Dim newExpiration As Date
newExpiration = expiration.AddYears(3)
AddYears is a method that knows how to add a number of years to a Date variable. There are similarly named methods for adding months, days, and so on. In addition to methods, the Date type exposes properties, such as the Month and Day properties, which return the date’s month and day number, respectively. The keywords following the period after the variable’s name are called methods and properties, just like the properties and methods of the controls you place on a form to create your application’s visual interface. The methods and properties (or the members) of a variable expose the functionality that’s built into the class representing the variable itself.
Without this built-in functionality, you’d have to write some serious code to extract the month from a date variable, to add a number of days to a given date, to figure out whether a character is a letter, a digit, or a punctuation symbol, and so on. Much of the functionality that you’ll need in an application that manipulates dates, numbers, or text has already been built into the variables themselves.
Don’t let the terminology scare you. Think of variables as placeholders for values and access their functionality with expressions like the ones shown earlier. Start using variables to store values and, if you need to process them, enter a variable’s name followed by a period to see a list of the members it exposes. In most cases, you’ll be able to figure out what these members do by just reading their names. I’ll come back to the concept of variables as objects, but I wanted to hit it right off the bat. A more detailed discussion of the notion of variables as objects can be found in Chapter, “Working with Objects,” which discusses objects in detail.
Programming languages can treat simple variables much more efficiently than objects. An integer takes two bytes in memory, and the compiler will generate very efficient code to manipulate an integer variable (add it to another numeric value, compare it to another integer, and so on). If you declare an integer variable and use it in your code as such, Visual Studio doesn’t create an object to represent this value. It creates a new variable for storing integers, like good old BASIC. After you call one of the variable’s methods, the compiler emits code to create the actual object. This process is called boxing and it introduces a small delay, which is truly insignificant compared to the convenience of manipulating a variable through its methods.
As you’ve seen by now, variables are objects. This shouldn’t come as a surprise, but it’s an odd concept for programmers with no experience in object-oriented programming.We haven’t covered objects and classes formally yet, but you have a good idea of what an object is. It’s an entity that exposes some functionality by means of properties and methods. The TextBox control is an object and it exposes the Text property, which allows you to read or set the text on the control. Any name followed by a period and another name signifies an object. The “other name” is a property or method of the object.