Sharing an instance of an object across classes - Bet

29-Oct-08 01:29:02
Hello.

I'm trying to find another way to share an instance of an object with other
classes.

I started by passing the instance to the other class's constructor, like this:

Friend Class clsData
Private m_objSQLClient As clsSQLClient
Private m_objUsers As clsUsers
Private m_sConnect As String

Friend Sub connect(ByVal bDebuggerAttached As Boolean)
Dim sServer As String
Dim sDB As String

If bDebuggerAttached Then
sServer = "test"
Else
sServer = "prod"
End If
sDB = "appdb"

m_sConnect = "Data Source=" & sServer & ";Database=" & sDB & ";Integrated
Security=true"
m_sConnect = m_sConnect & ";Application Name = " &
My.Application.Info.AssemblyName

m_objSQLClient = New clsSQLClient
m_objSQLClient.connect(m_sConnect)

m_objUsers = New clsUsers(m_objSQLClient)
End Sub
End Class

Friend Class clsUsers
Private m_objSQLClient As clsSQLClient
Private m_dtUser As DataTable

Friend Sub New(ByVal objSQLClient As clsSQLClient)
m_objSQLClient = objSQLClient
End Sub

Friend Function markEntry() As String
Try
m_dtUser = m_objSQLClient.tableForEdit("users")

This works, but I don't like it.  I want clsUsers to refer directly to the
instance variable in clsData instead of passing a parameter.

Then I saw an example using inheritance that I liked, so I added:

Protected Friend Function sqlClient() As clsSQLClient
Return m_objSQLClient
End Function

To clsData and I deleted the constructor in clsUsers and changed the code to:

Friend Class clsUsers
Inherits clsData
Private m_dtUser As DataTable

Friend Function markEntry() As String
Try
m_dtUser = MyBase.sqlClient.tableForEdit("users")

but it doesn't work- it raises an exception when clsUsers.markEntry
references mybase.sqlClient.

Is there another way I can do this, or do I need to stick with passing the
object instance as a parameter?

Thanks for any help,

-Beth
reply
Sharing an instance of an object across classes - Bet 29-Oct-08
        Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                Re: Sharing an instance of an object across classes - Bet 29-Oct-08
                        Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                                Re: Sharing an instance of an object across classes - Bet 29-Oct-08
                                        Re: Sharing an instance of an object across classes - Bet 29-Oct-08
                                                Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                                        Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                                                Re: Sharing an instance of an object across classes - Bet 29-Oct-08
                                                        Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                                                        Re: Sharing an instance of an object across classes - sloan 29-Oct-08
                                                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                                                                        Re: Sharing an instance of an object across classes - sloan 30-Oct-08
                                                                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                                                                                        Re: Sharing an instance of an object across classes - sloan 30-Oct-08
                                                                                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                                                                                                        Re: Sharing an instance of an object across classes - sloan 30-Oct-08
                                                                                                                Re: Sharing an instance of an object across classes - Bet 31-Oct-08
                                                                                                                Re: Sharing an instance of an object across classes - Andrew Morton 31-Oct-08
                                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
        Re: Sharing an instance of an object across classes - sloan 29-Oct-08
                Re: Sharing an instance of an object across classes - Tom Shelton 29-Oct-08
                        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 30-Oct-08
                                Re: Sharing an instance of an object across classes - Tom Shelton 01-Nov-08
                Re: Sharing an instance of an object across classes - Bet 29-Oct-08
                        Re: Sharing an instance of an object across classes - sloan 29-Oct-08
                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                                        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 31-Oct-08
                                                Re: Sharing an instance of an object across classes - Bet 31-Oct-08
                Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 30-Oct-08
        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 30-Oct-08
                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 30-Oct-08
                                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
        Re: Sharing an instance of an object across classes - Michel Posseth [MCP] 30-Oct-08
                Re: Sharing an instance of an object across classes - Bet 30-Oct-08
                Re: Sharing an instance of an object across classes - Bet 31-Oct-08
                        Re: Sharing an instance of an object across classes - Michel Posseth 01-Nov-08
                                Re: Sharing an instance of an object across classes - Bet 03-Nov-08
                        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 01-Nov-08
                                Re: Sharing an instance of an object across classes - Bet 03-Nov-08
                                        Re: Sharing an instance of an object across classes - Cor Ligthert[MVP] 03-Nov-08
                                                Re: Sharing an instance of an object across classes - Bet 03-Nov-08

If statement not catching on conditions
  

Search

search




Purchase