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:
Here, objectVariable is the object variable. That is, in the code
Dim myCommand as SqlCommand myCommand = New SqlCommand()
Typically, you will assign a value to a property just once and then will call one of the object's
For example, to send an email message from an ASP.NET page, we use the
class. When an instance of this class is created, a number of properties need to be set, such as
, and others. The following code snippet
'Create an instance of the MailMesage class Dim myMailMessage As MailMessage = New MailMessage() 'Set the From, To, Subject, and Body properties myMailMessage.From = "email@example.com" myMailMessage.To = "firstname.lastname@example.org" myMailMessage.Subject = "Email Subject" myMailMessage.Body = "Hello!"
Calling an Object's
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
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
class and set its
properties. After these two steps have been accomplished, we can call the
method. The following code snippet
'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.