473,805 Members | 2,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Globally accessible variables and functions?

I'm writing a VB.NET application, and I have a lot of general-purpose
utility functions that need to be available throughout the program
(like functions that perform standard date conversions peculiar to my
app, etc.) and some global data values (i.e. the start and end dates
of a billing period).

I have gathered them all into an object and would like to make that
object a global variable - instantiate it once and then be able to
reference it from anywhere in the code that I need to. I don't want
to pay the overhead of recreating the object every time I need it.

Can somebody suggest the best way to accomplish this? I don't see an
equivalent to a global variable in a .BAS module as in VB 6. Is there
a better way to do this in .NET?
Thanks in advance...

Nov 20 '05 #1
8 2010
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.

However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

http://msdn.microsoft.com/library/de...redmembers.asp
I hope this helps?

Cor
Nov 20 '05 #2
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.

However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

http://msdn.microsoft.com/library/de...redmembers.asp
I hope this helps?

Cor
Nov 20 '05 #3
On Thu, 8 Apr 2004 19:33:48 +0200, "Cor Ligthert"
<no**********@p lanet.nl> wrote:
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.
I fill in some values of a few data members in the new() function - so
there's that overhead, performance-wise.


However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

But that doesn't create the member variables and initialize them, etc.

I think I found the answer in making a "singleton" class - the class
contains an instance of itself, and I use a function I'll call
"Create", which will do:
Public Shared Function Create() As clsMyApplicatio n
If oApp Is Nothing Then
oApp = New clsMyApplicatio n
End If

Return oMyApp
End Function
And then, wherever I need this object, I just "create" it:

dim oMyApp As clsMyApplicatio n = oMyApp.Create()

Which will create the object if necessary, but otherwise will use the
one that is already created. It seems to work well.

http://msdn.microsoft.com/library/de...redmembers.asp
I hope this helps?

Cor


Nov 20 '05 #4
On Thu, 8 Apr 2004 19:33:48 +0200, "Cor Ligthert"
<no**********@p lanet.nl> wrote:
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.
I fill in some values of a few data members in the new() function - so
there's that overhead, performance-wise.


However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

But that doesn't create the member variables and initialize them, etc.

I think I found the answer in making a "singleton" class - the class
contains an instance of itself, and I use a function I'll call
"Create", which will do:
Public Shared Function Create() As clsMyApplicatio n
If oApp Is Nothing Then
oApp = New clsMyApplicatio n
End If

Return oMyApp
End Function
And then, wherever I need this object, I just "create" it:

dim oMyApp As clsMyApplicatio n = oMyApp.Create()

Which will create the object if necessary, but otherwise will use the
one that is already created. It seems to work well.

http://msdn.microsoft.com/library/de...redmembers.asp
I hope this helps?

Cor


Nov 20 '05 #5
So then what's the difference between a singleton class, and a class with
all shared members? I mean as far as the end result of what you are trying
to accomplish.

"Dougal Fair" <do****@dougalf air.com> wrote in message
news:40******** ********@news.g oldengate.net.. .
On Thu, 8 Apr 2004 19:33:48 +0200, "Cor Ligthert"
<no**********@p lanet.nl> wrote:
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.


I fill in some values of a few data members in the new() function - so
there's that overhead, performance-wise.


However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

But that doesn't create the member variables and initialize them, etc.

I think I found the answer in making a "singleton" class - the class
contains an instance of itself, and I use a function I'll call
"Create", which will do:
Public Shared Function Create() As clsMyApplicatio n
If oApp Is Nothing Then
oApp = New clsMyApplicatio n
End If

Return oMyApp
End Function
And then, wherever I need this object, I just "create" it:

dim oMyApp As clsMyApplicatio n = oMyApp.Create()

Which will create the object if necessary, but otherwise will use the
one that is already created. It seems to work well.


http://msdn.microsoft.com/library/de...-us/vbcn7/html

/vaconsharedmemb ers.asp


I hope this helps?

Cor

Nov 20 '05 #6
So then what's the difference between a singleton class, and a class with
all shared members? I mean as far as the end result of what you are trying
to accomplish.

"Dougal Fair" <do****@dougalf air.com> wrote in message
news:40******** ********@news.g oldengate.net.. .
On Thu, 8 Apr 2004 19:33:48 +0200, "Cor Ligthert"
<no**********@p lanet.nl> wrote:
Hi Douglas,

I do not understand what you mean, with pay for that overhead, recreating
gives the posibility to free resources.


I fill in some values of a few data members in the new() function - so
there's that overhead, performance-wise.


However what you ask is called in VB.net a Shared class, you make that by
setting all the methods and properties shared.

But that doesn't create the member variables and initialize them, etc.

I think I found the answer in making a "singleton" class - the class
contains an instance of itself, and I use a function I'll call
"Create", which will do:
Public Shared Function Create() As clsMyApplicatio n
If oApp Is Nothing Then
oApp = New clsMyApplicatio n
End If

Return oMyApp
End Function
And then, wherever I need this object, I just "create" it:

dim oMyApp As clsMyApplicatio n = oMyApp.Create()

Which will create the object if necessary, but otherwise will use the
one that is already created. It seems to work well.


http://msdn.microsoft.com/library/de...-us/vbcn7/html

/vaconsharedmemb ers.asp


I hope this helps?

Cor

Nov 20 '05 #7
"Dougal Fair" <do****@dougalf air.com> schrieb
I'm writing a VB.NET application, and I have a lot of
general-purpose utility functions that need to be available
throughout the program (like functions that perform standard date
conversions peculiar to my app, etc.) and some global data values
(i.e. the start and end dates of a billing period).

I have gathered them all into an object and would like to make
that object a global variable - instantiate it once and then be able
to reference it from anywhere in the code that I need to. I don't
want to pay the overhead of recreating the object every time I need
it.

Can somebody suggest the best way to accomplish this? I don't see
an equivalent to a global variable in a .BAS module as in VB 6. Is
there a better way to do this in .NET?


Declare them Shared in a Class.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #8
"Dougal Fair" <do****@dougalf air.com> schrieb
I'm writing a VB.NET application, and I have a lot of
general-purpose utility functions that need to be available
throughout the program (like functions that perform standard date
conversions peculiar to my app, etc.) and some global data values
(i.e. the start and end dates of a billing period).

I have gathered them all into an object and would like to make
that object a global variable - instantiate it once and then be able
to reference it from anywhere in the code that I need to. I don't
want to pay the overhead of recreating the object every time I need
it.

Can somebody suggest the best way to accomplish this? I don't see
an equivalent to a global variable in a .BAS module as in VB 6. Is
there a better way to do this in .NET?


Declare them Shared in a Class.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #9

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

Similar topics

15
2350
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global $arr=array(); (syntax err) 2) global $arr; in "main", the var isn't global 3) global $arr; in function, the array is cleared each time
21
3397
by: steve | last post by:
Dont’ make my mistake. It is costly. Say you have defined a variable $var in your main script. Now in a function you access it using: global $var; But you want to set it to null inside the function. DO: $var = null; DONT DO: unset($var);
13
1719
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out which one. if I do:
4
3615
by: Daniel | last post by:
Hullo =) Inspired by another guy's questions here I've created an isset function that works (almost) like the one in native PHP: function isset(variablename) { return(typeof(eval("window."+variablename))!='undefined'); } I use it like if(isset('myVar') { alert(myVar); }
6
2692
by: Gunnar Beushausen | last post by:
Hi! I need a class to store the users data (ID, name etc.) that is accessible from anywhere. At application startup the class gets filled with its data about the user. But how can i access this data from all other classes? Normally to get access, i would have to say something like UserData *UD = new UserData; But this way a new class gets instantieted. What can i do to
0
257
by: Dougal Fair | last post by:
I'm writing a VB.NET application, and I have a lot of general-purpose utility functions that need to be available throughout the program (like functions that perform standard date conversions peculiar to my app, etc.) and some global data values (i.e. the start and end dates of a billing period). I have gathered them all into an object and would like to make that object a global variable - instantiate it once and then be able to...
2
1719
by: robin.bruce | last post by:
Hi guys, I just tried to give advice to a colleague who has a problem program in C, and I was interested to hear the thoughts of this forum on the subject. I've been an occasional programmer in C for about a year now, so by no means an expert, so it would be good to know if I'm leading my colleague down the wrong path.
9
2790
by: userblue | last post by:
Hi Does anyone know if there is a way to define what is effectively a single globally visible, enumerated list whilst actually defining the entries across several different modules? or somehow do a similar thing with macros. Details: I have a c project to fit into a small microprocessor and need to save some ram. I have a significant number of flags all over the place that currently use whole byte storage. I thought if I had a way to...
4
10901
by: adodotnet20 | last post by:
I'm developing a Windows application and I'm having some problems in making variable accessible to the whole application. I use VS.NET 2005 and I create a database connection object in the method private void connect_Click(object sender, EventArgs e). That object I create seems to be accessible only within that method, how do I make it accessible to the whole application, just like I have access to mytextbox.text throughout my tabbed...
0
9596
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
10613
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
10363
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
6876
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
5544
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...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
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
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
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.