Re: Sharing an instance of an object across classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi again, sloan.

When I was in high school, we coded on Commodore Pets with a cassete tape
for storage. I had a computer at home my dad bought for $99. I think it was
a TRS-A.
Not sure, that was a long time ago.

Oddly enough, my first job was for the Apple//e platform in AppleSoft BASIC
for the public schools market. When the GS came out, we coded in 6502
assembly. It was a big deal because all of a sudden we had a lot more memory
and we didn't have to use the unsupported language card to do anything useful.

Of course, no one else was developing for that platform, so I switched to MS
and I've been there ever sense.

I guess I just haven't kept up as well as you have over the years, but I
haven't had a lot of support, either.

I figured maybe someone dumped on you for something similar 10 years ago and
you were paying it forward.

"sloan" wrote:


//which pains you young
whipper-snappers,//

Huh?
I've been developing code for 10 years also (as in being paid ... for a
living).
I started with VB4, 16Bit for Win3.1.

So I don't think age has anything to do with it.


If you count the space invader knock-off game I wrote in the 6th grade on a
TRS80 using ascii characters, then I've been writing code for 24 years on
and off.
Wait, I took a Apple 2e course during my 5th grade summer. Make that 25
years.





"Beth" <Beth@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:513683F2-1DEE-4565-9C4A-8642B4CD024F@xxxxxxxxxxxxxxxx
Thanks, sloan.
I think.

Yes, I've been developing over 10 years now. I started with VB3, believe
it
or not.
And yes, I have taken a couple of .NET courses. Didn't get through to me,
I
guess.

Actually, I used to think I could support anybody else's VB code, until I
saw some written by a guy experienced in COBOL. Then I saw some code
written
by engineers. Then I saw some code written by a FoxPro developer.

I usually rewrite the old stuff instead of trying to figure out what they
were doing.

So I gather...
You don't like my naming convention. Personally, I like being able to
tell
the data type from the variable name.

You were thinking I was working on components that didn't access the
database directly? That I was implementing some middle-tier components?
I'm
doing the whole thing, soup to nuts (actually, I've done the whole thing
and
I'm revisiting it.)
I could have introduced additional layers of abstraction, but for this
project, it didn't make sense. It would be introducing abstraction for
abstraction's sake, not reuse.

You want to see connection strings in a config file instead of code. I'm
not a huge fan of config files, but I have seen them promoted. It's an
internal application using integrated security, so all I'm concerned about
is
keeping people from accessing the wrong back end. I need to keep people
in
prod from connecting to test, even though prod started out in the test
environment and they're structurally the same. I'm even exposing the
connection in Help/About so we're all clear on where we're connecting at
runtime.

You want to see the use of lists instead of collections. I think I can
accommodate you, there.

It's certainly not my intention to write code which pains you young
whipper-snappers, so if there's more I was supposed to pick up on, feel
free
to share.

I need to take my nap now.

-Beth

"sloan" wrote:


I think that whole bit of code is ... .. hurting.....

You're tied your clsUsers to always coming from the database.
Based on the notation (hungarian), this looks like leftover VB6 code, and
no
one at your workplace has taken a VB.NET course yet.

Take a look here:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!140.entry

This is a layered application.

The business objects and collections are seperate from the code that
creates
them.
The code that creates them can use database related calls, OR (better)
the
business objects and collections can be created totally independant of a
database.
(One reason? UnitTesting)

Your connection strings should be kept in a config file. You've got
hardcoding all over the place.


I'm not trying to be mean...but youch....... I have no idea what that
code
is doing. I might have in 1999.



................



"Beth" <Beth@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
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