Calling an Object s Methods


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.




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