Global type variables in VB.NET

J

JohnR

My situation is that I have a custom user control that I have built into a
DLL and put it on the "USER CONTROLS" part of the toolbox, so I can drag it
into a form whenever I need it.
The problem is that, for a variety of reasons, I need to "see" a variable in
all the SUBs that exist in my user control class that is defined in my
startup module of my program. Here's a code snippet:

Imports MyUserControlLibrary
Module Startup
public myGlobalArrayList as arraylist
public sub main()
.... Application.Run(Form1)
end sub
end module

I need to see myGlobalArrayList EVERYWHERE including the subs in
MyUserControlLibrary (which is a dll I built containing my custom stuff). I
tried making it a public variable (as in this example), I've tried a
separate class where I define a public shared property. But in all cases I
can see myGlobalArrayList in my application subs, but NOT in the subs within
the MyUserControlLibrary dll.
When I was testing the routines in MyUserControlLibrary I had them as the
*.VB files in my current application and everything worked fine. When I
broke them out into a separate module and compiled them into a dll they
could no longer see the variable.

Is there any way I can make myGlobalArrayList visible to the routines in the
dll library???

Thanks for any suggestions...
 
C

Cor Ligthert

John,

Would you not first start with a more simple approach.
A user control is for a self made control (what nobody does) or a from
Control derived class. A control is a class that acts to do UI handling.

In that can be a reason for an Arrayclass, however than probably not the
best approach, because there are standard when they are used collections in
a control.

You can make a "compenent" of course, however I would first try to make my
program with what I want to use. Than I can decide to make a compenent from
it for future use.

Just my thought when I readed your message.

Cor
 
J

JohnR

Hi Cor,

Thanks for the reply. You misread the post... I am, in fact, creating a
user control that is derived from (ie: inherits) the LABEL control. Within
that usercontrol I have an arraylist which must be accessable from anywhere
in the program.

I did find a solution to my problem and I want to post it here for others
who may be interested. I used a SINGLETON PATTERN class (search google for
that for more info). Basically, a singleton pattern class is a class with
only one instance. Essentially the constructor of the class is private (not
public like regular classes) so it can only be constructed from within
itself. But, you ask, how can you access the class if you can't access the
constructor? Good question. The answer is that you have a PUBLIC SHARED
function method that checks if the instance exists yet, and if it does it
returns that instance, if it does not yet exist, it creates an instance and
returns that. Public Shared procedured in a class can be accessed EVEN IF
THE CLASS INSTANCE DOES NOT YET EXIST. So that's how you build the
instance.

Using this technique, whenever you want the class you get the exact same
instance (properties and all). Soooo, this class is perfect for holding
properties that can be accessed globally. This is the solution I used and
it worked flawlessly.

John
 

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