473,608 Members | 2,565 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variables in a project.

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
22 1655
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=ne w 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******** ******@TK2MSFTN GP12.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
http://msdn.microsoft.com/architecture

chanmm

"UJ" <UJ@nowhere.com > wrote in message
news:O9******** ******@TK2MSFTN GP12.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
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*****@hotmai l.com> wrote in message
news:Og******** ******@TK2MSFTN GP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

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

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@_spamkille r_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=ne w 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
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.SecMa nager.

Is there an easier way?

TIA - UJ

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:b4******** *************** *********@4ax.c om...

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@_spamkille r_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=ne w 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
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
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****@newsgro up.nospam> wrote in message
news:b4******** *************** *********@4ax.c om...

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@_spamkille r_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=ne w 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
What's easier than....

public class SecurityManager

private _sm As new SecurityManager

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

end class

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

SecurityManager .TheSecurityMan ager.SomeMethor OrOther()

--
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******** ********@TK2MSF TNGP10.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.SecMa nager.

Is there an easier way?

TIA - UJ

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:b4******** *************** *********@4ax.c om...

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@_spamkille r_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=ne w 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
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$******** ******@TK2MSFTN GP12.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*****@hotmai l.com> wrote in message
news:Og******** ******@TK2MSFTN GP12.phx.gbl...
http://msdn.microsoft.com/architecture

chanmm

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
4848
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On form B there are several text boxes where the array values can be changed, however when I close B and go back to A, those values are lost. I thought Public meant Public? Larry L
10
2628
by: Kleenex | last post by:
Reason: I am working on an embedded project which has very limited memory (under 512 bytes, 60 or so of which is stack space), which translates into limited stack space. In order to save on stack space, I tried to only use parameters and stack space for things which are truely temporary. Instead of passing a pointer to a data structure which should always be populated with data, I have the data structure declared as a global variable and...
8
5671
by: Thomas Coleman | last post by:
Ok, I've obviously discovered that Global.aspx has been completely changed in ..NET 2.0. However, I haven't figured out how to declare a constant that's available to any page in my application without having to jump through a bunch of hoops. First, let me layout how it worked in 1.1. In the Global.asax, within the Global class construct, I would simply add something like: public const string FOO = "foo";
6
2421
by: calcop | last post by:
Hello everyone, I hope I can write this question clearly. Anyway, it is about global variables. I have several files in my C++ project. One of which is main.cpp. This file comtains my main() function. I have a few other files in my project. They are server.cpp, server.h, strings.h, includes.h and functions.h. My main.cpp function #includes the includes.h file which includes strings.h and functions.h. My main() function fills a...
37
2727
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
10
2640
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
5
11809
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
2
2055
by: TARUN | last post by:
I have a question about declaring the Global.ascx: In VS.NET I create a Solution, and there are 4 projects under it. They are put under 4 folders, but web pages would be called across the 4 projects. For example, web page Page1 in Project1 will redirect to web page Page2 in Project2 which will further redirect to Page3 in project3 and Page4 in Project4 and finally back to Page1. In each web page, some session variables, such as...
8
13187
by: Rob T | last post by:
When I was using VS2003, I was able to compile my asp.net project locally on my machine and copy it to the production server and it would run just fine. I've now converted to VS2005. The project compiles & runs fine locally, but when I copy to the production machine, I get this error: Parser Error Message: Could not load type 'Global'. Source Error: Line 1: <%@ Application Codebehind="Global.asax.vb" Inherits="Global" %> Source...
15
2545
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the Global class. Note: I use these shared variables for read only values that are set at application start. It would appear the 2.0 doesn't like you to use shared variables in the global class. How do I convert my 1.1 applications to 2.0 without...
0
8057
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7998
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8491
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3959
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2472
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1580
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1327
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.