Home All Groups Group Topic Archive Search About

Reference to a non-shared member requires an object reference

Author
19 Sep 2006 8:20 AM
Przemek
Hi,

I'm creating small aspnet application. The main purpose of application
is to manage list of contracts (adding, updating, deleting) and
creating reports based on database of contracts. So I've created a
class Contract, which have all contract's properties and method like
InsertContract etc. In my InsertContract method I want to pass Contract
object to my database provider class Insert method. I'm receiving an
error "Reference to a non-shared member requires an object reference".
It's understandable, because I didn't instantiate database provider
object before. I can workaround this error using "shared" keyword in my
database provider method definition. My question is, if it's correct
solution and if not, what shall I do. Here is my code:

Public Class Contract

        Public Property Trademark() As String
            Get
                Return _trademark
            End Get
            Set(ByVal value As String)
                _trademark = value
            End Set
        End Property

' other properties

Public Sub New()

End Sub

'other methods

Public Sub InsertContract ()
DatabaseProvider.InsertContract (contract)
End Sub

End Class


Public Class DatabaseProvider

Public Shared Sub InsertContract (record as Contract)

'adding contract to database

End Class

Przemek

Author
19 Sep 2006 11:16 AM
Robinson
Show quote Hide quote
>
> I'm creating small aspnet application. The main purpose of application
> is to manage list of contracts (adding, updating, deleting) and
> creating reports based on database of contracts. So I've created a
> class Contract, which have all contract's properties and method like
> InsertContract etc. In my InsertContract method I want to pass Contract
> object to my database provider class Insert method. I'm receiving an
> error "Reference to a non-shared member requires an object reference".
> It's understandable, because I didn't instantiate database provider
> object before. I can workaround this error using "shared" keyword in my
> database provider method definition. My question is, if it's correct
> solution and if not, what shall I do. Here is my code:
>
> Public Class Contract
>
>        Public Property Trademark() As String
>            Get
>                Return _trademark
>            End Get
>            Set(ByVal value As String)
>                _trademark = value
>            End Set
>        End Property
>
> ' other properties
>

Several things here, mainly design issues.  I would make "InsertContract" a
method on your Database provider.  So you pass in a Contract object to the
provider, rather than try to make the contract itself "know" about the
provider ( ie. "myProvider.InsertContract ( myContract ).  If you want to do
the latter, you need to store an reference to the provider in the contract,
which I'm sure you will agree is not quite so easy to do.  You could of
course make your provider a global singleton, and then reference it with
your contract object, but I don't think that would be a very good idea.