Re: Sharing an instance of an object across classes
- From: Beth <Beth@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 30 Oct 2008 09:26:05 -0700
Thanks for your response, Michael.
I know you can create 'bad' code in any language reguardless of the amount
of reading you've done or training you've had, and dealing with other
people's code you don't like, agree with, or support is part of being a
developer.
I don't like C-style languages because they're too symbolic and can combine
too many operations in one line of code. It's for a different level of geek
than me.
What I've noticed about the newer naming convention is it doesn't include
the data type of the variable and the module-level scope prefix (m_) was
dropped in favor of just the _.
I can see, though, that if you're using automated tools to generate your
code, you need to follow the tool's expected naming convention. Obviously, I
haven't used any. I write everything from scratch.
I'm assuming the .NET naming convention is published on MS's web site
somewhere, like the VB6 convention was, so I should be able to find it.
Thanks again for your response. I usually work by myself or with people
with less experience than me so the rest of the world can just fly by me and
I'll never even notice.
-Beth
"Michel Posseth [MCP]" wrote:
.
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
- References:
- Sharing an instance of an object across classes
- From: Beth
- Re: Sharing an instance of an object across classes
- From: Michel Posseth [MCP]
- Sharing an instance of an object across classes
- Prev by Date: Re: Multiple event Args in VB 2005
- Next by Date: Re: Multiple event Args in VB 2005
- Previous by thread: Re: Sharing an instance of an object across classes
- Next by thread: Re: Sharing an instance of an object across classes
- Index(es):
Relevant Pages
|