Globally accessible variables and functions?

D

Dougal Fair

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

Dougal Fair

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 clsMyApplication
If oApp Is Nothing Then
oApp = New clsMyApplication
End If

Return oMyApp
End Function


And then, wherever I need this object, I just "create" it:

dim oMyApp As clsMyApplication = oMyApp.Create()

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

Armin Zingler

Dougal Fair said:
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
 
M

Marina

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.
 

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

Top