Global variables in a project.

  • Thread starter Thread starter UJ
  • Start date Start date
U

UJ

How do I create an instance of a class that is accessible to all
classes/forms in my project? I already have a startup module and I was
thinking putting stuff in there and then accessing it through there.

I need it for things like db connections, security manager, general db
functions, .....

Any help would be appreciated.....

UJ.
 
You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Chanmm,

Can you point me in the correct direction? This is a general architecture
page. I don't see anything specific to globals.

UJ.
 
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
OK I'm completely lost. I think I didn't explain myself correctly. I want to
be able define somewhere a user object called SecurityManager (which I
wrote). I want to be able to define it somewhere so I can reference it from
all kinds of objects. I have found so far that what I can do is create a
startup module (let's call it StartItUp), and create the instance of the
class as public in there. But then I always need to reference my Security
Manager as StartItUp.SecManager.

Is there an easier way?

TIA - UJ

Samuel R. Neff said:
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
In your start-up module, you can just create a public instance and use it
throughout your applciation:

PUblic myClassInstance as myClass = new myclass

You can then referr to myClassInstance throughout your applicaiton. Hope I
understood your question correctly.
 
Thats rubbish. This gives you a new instance of the class every time you ask
for it. Not a single persistent instance that can store instance data.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Samuel R. Neff said:
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
What's easier than....

public class SecurityManager

private _sm As new SecurityManager

public readonly shared TheSecurityManager() as SecurityManager
get
return _sm
end get
end property



end class

Now whenever you need to access your security manager you use...

SecurityManager.TheSecurityManager.SomeMethorOrOther()

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





UJ said:
OK I'm completely lost. I think I didn't explain myself correctly. I want
to be able define somewhere a user object called SecurityManager (which I
wrote). I want to be able to define it somewhere so I can reference it
from all kinds of objects. I have found so far that what I can do is
create a startup module (let's call it StartItUp), and create the instance
of the class as public in there. But then I always need to reference my
Security Manager as StartItUp.SecManager.

Is there an easier way?

TIA - UJ

Samuel R. Neff said:
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
Global variable usage is a very poor architectural choice. If you use them,
and global methods then you go back to the dark-ages of unstructured non
object oriented programming and create horrible spaghetti code. That's why
there is no mention of them in the architecture guidelines.

Of course, if you're moving over from VB6 you will be used to the concept of
spaghetti and feel quite at home with it but VB6 wasn't ever a real
programming language ;-).

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Bob,

Good morning,
Of course, if you're moving over from VB6 you will be used to the concept
of spaghetti and feel quite at home with it but VB6 wasn't ever a real
programming language ;-).
You need some coffee

:-)

Cor
 
Bob,
It's all fine and good to say you don't need globals. But then how do you do
something like a security manager class where every class is going to need
to access it? Do you spend you life passing references around for it?

Currently I am going to put references in my startup module and then make
references to it by startup.securitymanager

I can't think of any other way. Remember - the whole point is to have only
one instance of the variable around. I don't want to have many instances.

I supposed in a really good world, I would make it a property of the startup
(and I may do that).

Thanks for the help.

UJ.
 
UJ,

The simplest you can do is to pass the variables in the constructor.

Cor
 
Bob,
Thats rubbish. This gives you a new instance of the class every time you
ask for it. Not a single persistent instance that can store instance data.
Huh?

Looking at Samual's code, he has a readonly shared field. That shared field
will be initialized when the class itself is initialized (when the Shared
Sub New runs).

How does it give a new instance each time you ask for it?

Very curious
Jay

Bob Powell said:
Thats rubbish. This gives you a new instance of the class every time you
ask for it. Not a single persistent instance that can store instance data.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Samuel R. Neff said:
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
Huh? Rubbish? Try it. You'll see. It only instantiates the class
one time and it's even thread safe.


imports System

public module MyModule
sub Main

For i As Integer = 1 To 10
Dim o As Object = Singleton.Instance
Next

end sub

end module

Public Class Singleton
Public Shared ReadOnly Instance As New Singleton

Private Sub New()
Console.WriteLine("Instantiating Singleton...")
End Sub
End Class



The technique you posted earlier is not thread safe. A double-check
lock would be closer..

If Not _instance Is Nothing
SyncLock syncroot
If Not _instance Is Nothing
_instance = New Singleton()
End If
End SyncLock
End If

Return _instance

But even that code can become non-thread safe after the JIT
"optimizes" the code.

Except for situations where custom code needs to be run prior to
instantiation the single line approach is recommended.

http://msdn.microsoft.com/library/d...n-us/dnpatterns/html/ImpSingletonInCsharp.asp

Sam


Thats rubbish. This gives you a new instance of the class every time you ask
for it. Not a single persistent instance that can store instance data.

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
Global variables as they existed in VB6 are discouraged but shared
properties/fields of classes and singleton instances of classes are
perfectly acceptable, recomended and produce the exact same result.

Saying global variables shouldn't be used is really just pointing out
bad terminology--the concept is fine (in moderation) but we now use
different techniques to implement the same functionality.

Best regards,

Sam

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 
UJ,
There have been numerous articles about the good & bad about using "Globals"
or Singletons in OO programming verses passing the instance to numerous
methods. If you like I can try to track them down for you, only it may not
be until Monday...

One of the major benefits of a Singleton is you do not need to couple (add a
parameter) the class to every method that may eventually need the Singleton,
nor do you need to have instance fields holding a reference. In the same
token one of the downsides is that any class at any time can access the
Singleton (this may not be a good idea in all cases)...

I normally try to minimize the use of Singletons & Globals. However! There
are needs, such as a SecurityManager where a Singleton makes sense. Note I
would making SecurityManager a Singleton, rather then making it a property
of StartUp.

I would consider implementing the SecurityManager Singleton as either only
shared methods of the class or a Single instance as Samuel & Bob showed
earlier.

I think the important thing is to keep tight control of who & when the
Singleton is instantiated. Notice in both Samuel's & Bob's example that only
the class itself creates an instance of it. (Encapsulation). Rather then let
Startup create an instance, if Startup can create an instance then other
classes might be able to create their own instances also...

Normally when you are defining a Singleton you will want to make the class
NotInheritable & add a private constructor, as this prevents others from
inheriting from your class & also prevents creating an instance of the
class. Then I would either have a single shared Instance member (either
Readonly Field or Readonly Property) that returns the instance of the Class
or I would have all Shared members...

Hope this helps
Jay
 
This is due to me working till 4 am...

my mistake :-(

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Jay B. Harlow said:
Bob,
Thats rubbish. This gives you a new instance of the class every time you
ask for it. Not a single persistent instance that can store instance
data.
Huh?

Looking at Samual's code, he has a readonly shared field. That shared
field will be initialized when the class itself is initialized (when the
Shared Sub New runs).

How does it give a new instance each time you ask for it?

Very curious
Jay

Bob Powell said:
Thats rubbish. This gives you a new instance of the class every time you
ask for it. Not a single persistent instance that can store instance
data.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





Samuel R. Neff said:
The .NET recommended practice for creating singletons is to use a
single line..

Public Shared ReadOnly Instance As New [ClassName]

For example..

Public NotInheritable Class DBCentral

Public Shared ReadOnly Instance As New DBCentral

Private Sub New()
End Sub

End Class


Sam


On Thu, 17 Feb 2005 17:09:03 +0100, "Bob Powell [MVP]"

You can create a class that uses the Singleton Pattern.
Something like...

public class DBCentral

private _theInstance as DBCentral

public readonly shared property TheInstance() as DBCentral
get()
if _theInstance is nothing then
_theInstance=new DBCentral()
end if
return _theInstance
end get
end property

end class

I didn't check the exact syntax but you get the picture....

B-Line is now hiring one VB.NET developer for
WinForms + WebServices position with ASPX in future.
Seaking mid to senior level developer. For
information or to apply e-mail sam_blinex_com.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top