Calling an Object's MethodsAn 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)
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.
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. |