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 bDebuggerAttach ed As Boolean)
Dim sServer As String
Dim sDB As String
If bDebuggerAttach ed Then
sServer = "test"
Else
sServer = "prod"
End If
sDB = "appdb"
m_sConnect = "Data Source=" & sServer & ";Database= " & sDB & ";Integrate d
Security=true"
m_sConnect = m_sConnect & ";Applicati on Name = " &
My.Application. Info.AssemblyNa me
m_objSQLClient = New clsSQLClient
m_objSQLClient. connect(m_sConn ect)
m_objUsers = New clsUsers(m_objS QLClient)
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("u sers")
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.sqlClien t.tableForEdit( "users")
but it doesn't work- it raises an exception when clsUsers.markEn try
references mybase.sqlClien t.
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
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 bDebuggerAttach ed As Boolean)
Dim sServer As String
Dim sDB As String
If bDebuggerAttach ed Then
sServer = "test"
Else
sServer = "prod"
End If
sDB = "appdb"
m_sConnect = "Data Source=" & sServer & ";Database= " & sDB & ";Integrate d
Security=true"
m_sConnect = m_sConnect & ";Applicati on Name = " &
My.Application. Info.AssemblyNa me
m_objSQLClient = New clsSQLClient
m_objSQLClient. connect(m_sConn ect)
m_objUsers = New clsUsers(m_objS QLClient)
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("u sers")
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.sqlClien t.tableForEdit( "users")
but it doesn't work- it raises an exception when clsUsers.markEn try
references mybase.sqlClien t.
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
Comment