Flylib.com

Books Software

 
 
 

Setting an Object s Properties


Setting an Object's Properties

After we have created an object, often we want to set some of the object's properties. To reference an object's properties, we use the following syntax:


objectVariable.PropertyName


Here, objectVariable is the object variable. That is, in the code

Dim myCommand as SqlCommand
myCommand = New SqlCommand()


the objectVariable is myCommand . The PropertyName is the name of the property that you want to access. Properties are used just like variables; they can be assigned values, they have types, and they can be used in expressions just like variables .

Typically, you will assign a value to a property just once and then will call one of the object's methods , which uses the value of the property in some manner.

For example, to send an email message from an ASP.NET page, we use the MailMessage class. When an instance of this class is created, a number of properties need to be set, such as From , To , Subject , and others. The following code snippet demonstrates how to create an instance of this class and set its properties:

'Create an instance of the MailMesage class
Dim myMailMessage As MailMessage = New MailMessage()

'Set the From, To, Subject, and Body properties
myMailMessage.From = "someone@example.com"
myMailMessage.To = "someone@example.com"
myMailMessage.Subject = "Email Subject"
myMailMessage.Body = "Hello!"


By the Way

Not all properties can necessarily be written to and read from. Some classes mark certain properties as read only or write only . The vast majority of properties, however, are both readable and writeable . When examining a new class and its properties, I'll point out if any of those properties are read only or write only.




Calling an Object's Methods

An object's methods are called just like other subroutines and functions, except that the name of the object whose method you want to call must precede the method being called. That is, the syntax for calling an object's method is as follows :


objectVariable.MethodName

(

param1, param2

, ...,

paramN

)


By the Way

Methods in classes are semantically just like subroutines and functions. That is, methods in classes can accept zero to many input parameters and can optionally provide a return value.


As we discussed earlier, the SqlCommand class is used for retrieving information from a database. In using the SqlCommand class, we must specify the database to retrieve the data from, as well as what data to retrieve. These two bits of information are specified via the Connection and CommandText properties. The SqlCommand class contains an ExecuteReader() method, which returns the data specified by the CommandText property from the database specified by the Connection property.

To call this method, we first must create an instance of the SqlCommand class and set its Connection and CommandText properties. After these two steps have been accomplished, we can call the ExecuteReader() method. The following code snippet demonstrates the syntax for calling a method:

'Create an instance of the SqlCommand class
Dim myCommand as SqlCommand = New SqlCommand()

'Set the Connection and CommandText properties
myCommand.Connection = ...
myCommand.CommandText = "..."

'Call the ExecuteReader() method
Dim myReader as SqlDataReader
myReader = myCommand.ExecuteReader()


As you can see in this code snippet, the ExecuteReader() method returns an object of type SqlDataReader .

By the Way

The SqlDataReader class is designed for holding data retrieved from a database.


Methods that return a value are similar to functions; some methods do not return a value, making them similar to subroutines. Also, methodsboth ones that do and ones that do not return a valuecan have zero to many input parameters.