Creating an Object


Recall that classes are an abstract definition, listing functionality that is provided. In addition to properties, methods, and events, classes contain constructors. A constructor is a special method that is used to create an instance of the class.

By the Way

In a later section, "Calling an Object's Methods," we'll discuss what, exactly, methods are. For now, you can think of a method as a function or subroutine. Like functions and subroutines, methods are a means of encapsulating a number of program instructions, can have zero or more parameters, and may return a value.


Constructors always have the same name as the class. For example, one of the classes used to programmatically work with database data is the SqlCommand class. The constructor for this class is a method named SqlCommand().

To create an instance of an object, we use the following syntax:

Variable = New Constructor() 


The constructor Constructor returns an object of the class Constructor. Because Visual Basic is a strongly typed language, the type of Variable must be of the class whose constructor is being called. For example, to create an instance of the SqlCommand class, we would first create a variable whose type was of SqlCommand as follows:

Dim myCommand as SqlCommand 


And then we would assign to this variable the object returned by the constructor:

myCommand = New SqlCommand() 


The first line of code creates a variable named myCommand of type SqlCommand; the second line of code assigns to myCommand the object returned by the constructor SqlCommand().

Did you Know?

Recall that to use an object, we must first create an instance of the object. This process is commonly called instantiation. We can instantiate an object like this:

Dim variable as type variable = New Constructor() 


Or we can also instantiate an object with the following line of code:

Dim variableName as type = New Constructor() 


With the SqlCommand example we examined, we could have rewritten it as follows:

Dim myCommand as SqlCommand = New SqlCommand() 



Constructors with Parameters

Constructors, like functions and subroutines, can have zero or more parameters. Additionally, classes may have more than one constructor. When constructors accept one or more parameters, typically the parameters are for initial values of various properties. For example, the SqlCommand class has a constructor that accepts zero parameters, as well as one that accepts a string parameter. The constructor that accepts zero parameters does not assign any initial value to any of its properties. The constructor that accepts a string parameter, however, assigns the passed-in parameter value to the object's CommandText property.

By the Way

In general, any class method (such as the constructor) can have multiple versions, each that accepts a different number of input parameters. Class methods that have versions that accept a different number of parameters are referred to as overloaded.


Constructors that accept more than one parameter are used frequently for reducing the amount of code that needs to be written. For example, to create an SqlCommand object and set its CommandText property, we would need to use the following two lines of code:

Dim myCommand as SqlCommand = New SqlCommand() myCommand.CommandText = "some value" 


However, by using the SqlCommand() constructor that accepts a string parameter, we can condense these two lines into one as follows:

Dim myCommand as SqlCommand = New SqlCommand("some value") 


By the Way

Most classes have more than one constructorone that accepts zero parameters and also a myriad of others that accept one, two, three, four, or even more parameters. The constructor that accepts zero parameters is typically referred to as the default constructor.





Sams Teach Yourself ASP. NET 2.0 in 24 Hours, Complete Starter Kit
Sams Teach Yourself ASP.NET 2.0 in 24 Hours, Complete Starter Kit
ISBN: 0672327384
EAN: 2147483647
Year: 2004
Pages: 233

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net