Re: Sharing an instance of an object across classes




Hello Beth,

There is nothing wrong with passing an initialized class as a parameter to
another class even if you pass it as byvalue ( wich i would recomend )
object instances will only transfer the memory pointer , so no new
instances are created , you still hold one instance of your class .

You can easiliy proove and confirm this yourself changes in the passed class
will be reflected in the original class , so in terms of memory footprint
and perfomance this is even a bether aproach as using inheritance as this
would create a bigger footprint in the resulting code output ,


about your coding style ,,,, it would be a good idea for you to read this
book
http://astore.amazon.com/vbdotnetcoder-20/detail/0735621721/175-0134366-2373937

Although i must say i have seen worse coding styles from people honestly
believing that they comply to MSF
i am just mentioning above link because of the comments in this thread wich
are partially valid after reading and conforming to the styles in above book
you can slap 90% of the coders around for not complying to the official MSF
, Practical guidelines and best practices rules .

But ,,,, honestly said it wil sure make you a bether and more productive
programmer if you comply to the basic rules of naming conventions just for
the simple fact that a lot of tools ( devpartner profiler to just call one )
comply to these rules so in my code i just simply declare a private field
and with just a short keyborard shortcut i can convert it to a correctly
named property


Regards

Michel Posseth [MCP]




"Beth" <Beth@xxxxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht
news:8D42B0F4-6D5C-4A5F-B32A-330F97A7B8C0@xxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: Sharing an instance of an object across classes
    ... You're tied your clsUsers to always coming from the database. ... Private m_objSQLClient As clsSQLClient ... Private m_sConnect As String ... Friend Sub connect ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Sharing an instance of an object across classes
    ... You're tied your clsUsers to always coming from the database. ... Private m_objSQLClient As clsSQLClient ... Private m_sConnect As String ... Friend Sub connect ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Sharing an instance of an object across classes
    ... You're tied your clsUsers to always coming from the database. ... Private m_objSQLClient As clsSQLClient ... Private m_sConnect As String ... Friend Sub connect ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Sharing an instance of an object across classes
    ... Private m_objSQLClient As clsSQLClient ... Private m_sConnect As String ... Friend Sub connect ... Friend Class clsUsers ...
    (microsoft.public.dotnet.languages.vb)
  • ByRef doesnt make it across invoke?
    ... I just found that passing arguments by reference to a function across ... string 'Failed' ... Private Shared StaticString As String ... Private Sub Form1_Load(ByVal sender As System.Object, ...
    (microsoft.public.dotnet.languages.vb)

Loading