Share Data Between Classes

  • Thread starter Thread starter cksj
  • Start date Start date
C

cksj

I'm working on a VB.Net DLL project. It has 6 classes. One of the classes
has the properties for the database path, database name and server name. The
purpose of this class is so that the DLL can be tested on different servers.
Without using the keyword "Shared", how can I share these data to the other
5 classes?

Thanks for any ideas!

Cesar
 
Cesar,
Without Shared, each consumer of the class will need to create an instance
of the class then access the properties. Which for this sort of thing the
Singleton Pattern is better, you do however need to use Shared to implement
a Singleton.

You could make the class itself a Singleton, when means the properties will
not be marked shared, however a read only shared Instance field will be
created in the class.

Public Class DbSettings

Public Shared Readonly Instance As New DbSettings()

Private Sub New()
End Sub

Public Property DatabasePath...

End Class

Then whenever you need to use any of the properties of the class you use
DbSettings.Instance.DatabasePath.

Hope this helps
Jay
 
Cesar,
Oh! out of curiosity, why do you not want to use "Shared"?

Hope this helps
Jay
 
Cesar,

You would have to make the other classes create your
settings class internally. That would make it fairly
loosely coupled, so you can test it on various backends.
I'd say though, that if that is a helper type class, you
might at least want consider why you wouldn't want to
make it shared (or static as it is in C#)
Somewhere or other you need to make the database settings
class instance available within the memory spaces of the
other 5 classes. To me, those are your two choices. If
you're creating internally, then you have plenty of
choices on how you might then go about doing this. If
the 5 classes need the settings directly, then you could
create a private property that controls the creation of
the settings object for the user of the other 5 classes,
or you could create it when the class in created, or you
could create and destroy it only when it is needed to
name but a few ways....

Hope that's of help....
 
If you have a dbconnection object that you want the other 5 classes to use
the classic approach would be to just dim the db class object and pass it
along to each method that requires in in the other classes

so you would just have a method like

public function getItem(ses as dbconnection, itemCode as string) as class1
 
Jay,

Thanks for your help.
I read that using Shared in DLL is not the best practice to use.

Cesar
 
Cesar,
I read that using Shared in DLL is not the best practice to use.
Correct Shared should not be your first choice, however there are times when
it is appropriate. It depends on the type of app. Like any construct
available in .NET its usage has its pros & cons.

As I said in my other email, you need to use it for the Singleton Pattern,
what you asked sounds like a good application of the Singleton Pattern,
assuming this is not an ASP.NET app. ASP.NET apps you need to use session &
context aware methods of implementing a singleton. Which is where public
shared methods (properties, subs & functions) are better than public shared
fields. You can change the code of the method to better anticipate the
environment you are running it.

The Singleton Pattern is a common OOP Design Pattern to allow global access
to an object. For details on the Singleton Patten and other Design Patterns
see:
1. "Design Patterns - Elements of Reusable Object-Oriented Software" by the
GOF (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides)
2. "Patterns of Enterprise Application Architecture" by Martin Fowler
3. "Visual Basic Design Patterns - VB 6.0 and VB.NET" by James W. Cooper.

#3 is a good companion for #1, #2 shows some advanced patterns. All three
are from Addison Wesley.

Hope this helps
Jay
 
Back
Top