Domanda

I'm new to visual basic so forgive me in advance. I'm trying to reference a data table from a module and I get “reference not a shared member requires object” error. I can’t figure out why.

Public Module myFuncs

Public Function get_name()

    Dim lotto As Integer
    Dim counter As Integer
    Dim first As String
    Dim last As String
    Dim fullname As String

    counter = Database11DataSet.Tbl_names.Rows.Count
    lotto = Int((counter - 1 + 1) * Rnd() + 1)

    Dim nameq = From n In Database11DataSet.Tbl_names.AsEnumerable()
                Where n.ID = lotto
                Select n

    For Each n In nameq
        first = n.first
    Next
    lotto = Int((counter - 1 + 1) * Rnd() + 1)

… 'does the same for the last name

Return fullname
End function
È stato utile?

Soluzione

As you mentioned, your problems were on the lines that used:

Database11DataSet.Tbl_names

Within your code, Database11DataSet appears to be a type rather than an object; the only methods that can be called on a type are shared (or, depending upon the language, static). From http://en.wikipedia.org/wiki/Method_(computer_programming)#Static_methods:

Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. A static method is distinguished in some programming languages with the static keyword placed somewhere in the method's signature.

In your case, you should be able to resolve this by creating an instance of Database11DataSet at the top of your code, and then using that in place of the type:

Public Function get_name()

    ...

    Dim data as new Database11DataSet()

    counter = data.Tbl_names.Rows.Count
    lotto = Int((counter - 1 + 1) * Rnd() + 1)

    Dim nameq = From n In data.Tbl_names.AsEnumerable()
                Where n.ID = lotto
                Select n

    ...

End Function
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top