Search code examples
vb.netobjectrecordset

why make an instance of a recordset object?


In this chunk of a code, the writer makes a reference to a recordset object and I just wanted to make sure I understood why before moving on.

Private Sub LoadDataBound()
  Set rs = GetData
If Not rs.EOF Then
    Set dgPend.DataSource = rs '     Bind the Datagrid to the recordset
    dgPend.Refresh
    dgPend.AllowUpdate = True
Else
    MsgBox "No Records Found in DataGrid!"
End If

'Call Bind_Contols
'Call End_Time
End Sub



Private Function GetData() As ADODB.Recordset
sWhichDataType = UCase(Trim(sWhichDataType))

Select Case sWhichDataType
Case "ADS"
    sSource = "\\development\brakechecktables\speedwrench.add"
    Set GetData = OpenADS
Case "SQL"
    sSource = "development"
    Set GetData = OpenSQL
Case "FOX"
    sSource = "E:\Yes30\Data\speedwrench\"
    Set GetData = OpenFOX
Case Else
    MsgBox "Invalid DataSet Selected"
End Select
End Function

In LoadDataBound, it says set rs = GetData(). This calls to the Function below, but why does it require to be set as an object of the rs recordset. Correct me here, because i'm most likely wrong, but does it require this so that you're able to make a function out of the recordset without having to use the same name for both (recordset and function) ? Or is there another process going on here that i'm unaware of.


Solution

  • I'd be happy to help you understand the code, but I'm really having trouble understanding your question and what your confusion is. You seem to be confused by the meanings, differences, and purposes of variables, objects, and methods (functions and sub routines). The code is fairly straightforward and easy to comprehend, so I'm guessing by your question that you are new to programming. The code does not look like it's very well written, which may be part of your confusion. It does not look like it is very well encapsulated, but it's not completely terrible. I've certainly seen much worse.

    So, assuming that my assumptions are correct, let me start by explaining variables, objects and methods, then I'll explain briefly what the code is doing. If that doesn't answer your question, please clarify.

    Variables

    A variable is the simplest concept. It's a named reference to a value that is stored in memory. In the code, rs is a variable. Each variable has a type, so they can only be set to values that are compatible with that type. You didn't show the code where the rs variable is declared, so I'm just assuming that it is declared as a type of ADODB.Recordset, such as:

    Private rs As ADODB.Recordset
    

    Objects

    An object, on the other hand is subtly different. An object is an instance of a type. RecordSet is a type of object, but it's not an object itself. There may be many objects created that are all that same type. Each object is typically independent of all the other objects of the same type (although they can, in some cases, share data). The values that variables reference are always objects of some type. Many variables can reference the same object, but every object is referenced by at least one variable. If an object is no longer referenced by any variable, it may live in memory for a little while, but it will be destroyed eventually by the garbage collector. A variable does not have to reference any object. It can instead reference Nothing. For instance:

    rs = New ADODB.RecordSet()  'rs now references a RecordSet object
    rs = Nothing  'rs now references no object at all
    

    The New operator is what creates a new object. Objects do not have names (unless they somehow implement that themselves), so even though the above variable is called rs, that's not the name of the object. For instance, I could do this:

    rs = New ADODB.RecordSet()
    temp = rs
    

    In this example, temp and rs now both reference the same object and changes made in one will be reflected automatically in the other. Therefore, rs and temp are not the names of the object itself. They are just the names of the variables that reference it.

    Method

    A method is a logical group of, hopefully encapsulated, code, that is assigned a name and can be invoked at any time using that name. A method can optionally take a list of parameters, and it can optionally return a single value. In VB, if the method returns a value, it is called a Function, and if it does not return a value, it is called a Sub routine.

    Value Types vs. Reference Types

    I won't dwell on it, because I don't want to confuse you, but everything I've said so far has been true of reference types, but not value types. Basically, reference types are typically more complex object types that store more data and are often mutable. Value types are usually simpler, smaller, and immutable. Two variables can reference the same reference type object, but they cannot do so with value type objects (such as Integer).

    Explanation of Your Code

    Your code contains a method called GetData. The reason this was made as a method may be two-fold. First, the author may have made it as a method because he intended to call it from multiple places in the code. When that is the case, it is always a good idea to make a method that does the work so that you don't duplicate the same code in multiple places. Second, the author may have just wanted to make it a method to make the code easier to read. Rather than putting all that code directly into the LoadDataBound method and needing to add a comment in the code saying that that section of code is getting the data, it's easier to read to just break it out into a separate method called GetData, then it's self-documenting.

    The GetData method takes no parameters and returns a new ADBDB.RecordSet object. Inside the GetData method, it calls either OpenADS, OpenSQL, or OpenFOX, and it is these methods that must create the new RecordSet object and fill it with values from the database.

    The LoadDataBound method gets data from the database and then loads a control on the form with that data using data binding. The first thing it does is it calls the GetData method which creates a new RecordSet object, fills it with data, and then returns that new object back to the LoadDataBound method. The LoadDataBound method sets the rs variable to reference the new RecordSet object that was returned by GetData.

    The LoadDataBound method then checks to see if the RecordSet object that was returned by GetData contains any data. If it does, it loads that data into the control, otherwise it shows a message box.