By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

Global variables in a project.

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
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.

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 21 '05 #2

P: n/a
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 21 '05 #3

P: n/a
UJ
Chanmm,

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

UJ.

"chanmmn" <ch*****@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 21 '05 #4

P: n/a

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]"
<bob@_spamkiller_bobpowell.net> wrote:
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.
Nov 21 '05 #5

P: n/a
UJ
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" <bl****@newsgroup.nospam> wrote in message
news:b4********************************@4ax.com...

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]"
<bob@_spamkiller_bobpowell.net> wrote:
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.

Nov 21 '05 #6

P: n/a
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.

"UJ" wrote:
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.

Nov 21 '05 #7

P: n/a
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" <bl****@newsgroup.nospam> wrote in message
news:b4********************************@4ax.com...

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]"
<bob@_spamkiller_bobpowell.net> wrote:
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.

Nov 21 '05 #8

P: n/a
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.SomeMethorOrOth er()

--
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" <UJ@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <bl****@newsgroup.nospam> wrote in message
news:b4********************************@4ax.com...

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]"
<bob@_spamkiller_bobpowell.net> wrote:
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.


Nov 21 '05 #9

P: n/a
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.

"UJ" <UJ@nowhere.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
Chanmm,

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

UJ.

"chanmmn" <ch*****@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 21 '05 #10

P: n/a
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
Nov 21 '05 #11

P: n/a
Nah, never drink the stuff. It makes me all hyper :-))

--
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.

"Cor Ligthert" <no************@planet.nl> wrote in message
news:O$*************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #12

P: n/a
UJ
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.
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OY*************@TK2MSFTNGP09.phx.gbl...
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.

"UJ" <UJ@nowhere.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
Chanmm,

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

UJ.

"chanmmn" <ch*****@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 21 '05 #13

P: n/a
UJ,

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

Cor
Nov 21 '05 #14

P: n/a
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).
Public Shared ReadOnly Instance As New DBCentral


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

Very curious
Jay

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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" <bl****@newsgroup.nospam> wrote in message
news:b4********************************@4ax.com...

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]"
<bob@_spamkiller_bobpowell.net> wrote:
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.


Nov 21 '05 #15

P: n/a

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/de...onInCsharp.asp

Sam
On Fri, 18 Feb 2005 12:44:51 +0100, "Bob Powell [MVP]"
<bob@_spamkiller_bobpowell.net> wrote:
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.
Nov 21 '05 #16

P: n/a

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.
Nov 21 '05 #17

P: n/a
Samuel,
The technique you posted earlier is not thread safe. A double-check
lock would be closer..
The following article also provides a plethora of information on the
singletons:

http://www.yoda.arachsys.com/csharp/singleton.html

Hope this helps
Jay

"Samuel R. Neff" <bl****@newsgroup.nospam> wrote in message
news:ii********************************@4ax.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/de...onInCsharp.asp

Sam
On Fri, 18 Feb 2005 12:44:51 +0100, "Bob Powell [MVP]"
<bob@_spamkiller_bobpowell.net> wrote:
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.

Nov 21 '05 #18

P: n/a
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

"UJ" <UJ@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
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.
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OY*************@TK2MSFTNGP09.phx.gbl...
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.

"UJ" <UJ@nowhere.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
Chanmm,

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

UJ.

"chanmmn" <ch*****@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com> wrote in message
news:O9**************@TK2MSFTNGP12.phx.gbl...
> 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.
>
>



Nov 21 '05 #19

P: n/a
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 [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OE**************@TK2MSFTNGP09.phx.gbl...
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).
Public Shared ReadOnly Instance As New DBCentral


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

Very curious
Jay

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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" <bl****@newsgroup.nospam> wrote in message
news:b4********************************@4ax.com...

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]"
<bob@_spamkiller_bobpowell.net> wrote:

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.



Nov 21 '05 #20

P: n/a
You're right... sorry.. Major brain fade on my part.

--
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" <bl****@newsgroup.nospam> wrote in message
news:ii********************************@4ax.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/de...onInCsharp.asp

Sam
On Fri, 18 Feb 2005 12:44:51 +0100, "Bob Powell [MVP]"
<bob@_spamkiller_bobpowell.net> wrote:
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.

Nov 21 '05 #21

P: n/a
Bob,
Yes I have the same problem with answering questions too late also. ;-)

Jay

"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
This is due to me working till 4 am...

my mistake :-(

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

<<snip>>
Nov 21 '05 #22

P: n/a
UJ
Dennis,
That works exactly the way I wanted it. And I had already found out that
anyway.

But thanks for the great help!

UJ.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:9B**********************************@microsof t.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.

"UJ" wrote:
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.

Nov 21 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.