X hits on this document

PDF document

“Beginning Visual C# 2005 Express Edition Video Series” - page 19 / 73





19 / 73

of Scope. I'll have more to say about it in later in this chapter when we look at Properties, Fields and Methods.

Before we can interact with the Make property or the Drive function, we must create an instance of the Car class; or rather, we must instantiate the Car object.

Visual Basic

Dim myCar as Car myCar = New Car Dim location as string myCar.Make = "Oldsmobile" myLocation = myCar.Drive()

The most important part of this example is the first two lines of code. First of all we dimension a variable myCar that is of type Car. If you remember back to video lesson 4 we worked with Primitive Types like strings and integers. Now we are working with a Complex Type, one that we created ourselves. Its “complex” because it can contain multiple values (Make and Model) and perform actions (like the Drive method). So when we talk about “types” we are talking about “classes”… as we learn more about .NET, we’ll see that even Primitive Types are defined as classes which have methods and properties.

The second line of code is where the magic actually happens. In line 1 we created a variable of type Car, but as of yet it is not actually a Car. At the moment we use the “New” keyword, a Car object is created in memory and we store a reference to that new Car in the myCar variable. Let me clarify this. All the information about the newly created Car object is stored in your computer’s memory. The variable myCar is given the address of that memory space, which is called a “reference”. Now, whenever you use the word “myCar” in code, it just refers the compiler to the memory address that represents that instance of the Car object.

There's a subtle difference between dimensioning a variable as a type (or class) and actually creating an instance of that type (or class). This will become more apparent as we talk about Class Constructors near the end of this chapter.

Almost everything you work with in VB, whether you know it or not, is a Class. Just take a look back at all the forms we’ve created in the previous video lessons.

Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET


Document info
Document views120
Page views120
Page last viewedWed Oct 26 13:48:15 UTC 2016