Visual Basic/VB >> Global Variables over three projects

by Ken Halter » Wed, 08 Oct 2003 10:21:32 GMT

One problem with GlobalMultiUse is that instances are created automatically. Which means
that an entirely new instance may created when you're not expecting it. I never use
GlobalMultiUse because I want to control when and where instances are created. If you
switch to MultiUse, you'll probably find your bug.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..


"Tim" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
> Hello All,
>
> I am developing an App using VB6. I have:
>
> 1 exe file
> 1 Active X OCX
> 1 ActiveX dll
>
> Actually it is bigger than this, but for explinations sake...
>
> The Active X dll is GlobalMultiUse (GMU) and I want it to hold global
> variables that can be set and read from the other two files.
>
> I have used no code to create instances of the GMU object. I have just
> ticked it as a reference in Project References.
>
> This all work fine on my development machine (Win 2000), but when I build
> the project group and install the app onto another machine (XP Prof) Some
> (and only some) of the variables get lost between the two other files.
>
> Any ideas would be appreciated.
>
> TIA
>
> Tim
>
>




Visual Basic/VB >> Global Variables over three projects

by Ken Halter » Wed, 08 Oct 2003 12:23:15 GMT


Here's one way... basically, you create a property that accepts the object as an argument.
'==================Form Code
Option Explicit

Private mobjClass1Instance As Class1
Private mobjClass2Instance As Class2

Private Sub Command1_Click()
Call mobjClass2Instance.ShowClass1Value
End Sub

Private Sub Form_Load()
Set mobjClass1Instance = New Class1
Set mobjClass2Instance = New Class2

'Set a value
mobjClass1Instance.TestProp = "This is a test"

'Pass the instance of Class1 to Class2
Set mobjClass2Instance.Class1Obj = mobjClass1Instance
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Make sure to clear the extra reference
Set mobjClass2Instance.Class1Obj = Nothing
End Sub
'==================Class1 Code (this represents the "global" data)
Option Explicit

Public TestProp As String
'==================Class2 Code
Option Explicit

Private mobjClass1Instance As Class1

Public Property Set Class1Obj(ByRef Setting As Class1)
Set mobjClass1Instance = Setting
End Property

Public Sub ShowClass1Value()
MsgBox "mobjClass1Instance.TestProp" _
= " & mobjClass1Instance.TestProp"
End Sub
'==================



--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..













Visual Basic/VB >> Global Variables over three projects

by Tim » Wed, 08 Oct 2003 20:22:22 GMT

Hello All,

I am developing an App using VB6. I have:

1 exe file
1 Active X OCX
1 ActiveX dll

Actually it is bigger than this, but for explinations sake...

The Active X dll is GlobalMultiUse (GMU) and I want it to hold global
variables that can be set and read from the other two files.

I have used no code to create instances of the GMU object. I have just
ticked it as a reference in Project References.

This all work fine on my development machine (Win 2000), but when I build
the project group and install the app onto another machine (XP Prof) Some
(and only some) of the variables get lost between the two other files.

Any ideas would be appreciated.

TIA

Tim




Global Variables over three projects

by Tim » Wed, 08 Oct 2003 23:54:42 GMT

Thanks for this Ken.

If I use MultiUse, how can I make the same instance be recognised by my
other two files at the same time.

Any help or pointers would be greatly appreciated.

Thanks

Tim



automatically. Which means
never use
created. If you


build
Some




Global Variables over three projects

by Frampton Firesword » Thu, 09 Oct 2003 02:17:19 GMT


automatically.

They're not! I agree, they're *supposed* to, but I've experienced projects
with GlobalMultiUse things in and they are 'nothing' in the form_load event.
I think it just shows that the rigmarole and the hoops VB has to go through
to implement different types of interfaces weighs it down sometimes - and
you just have to steer clear of those and know what it's best at.
My advice is to keep it as stateless as possible - create the object when
you need it, and destroy it when you don't. If it's constant data that needs
to be in the program all the time, then have it in a .bas.


Which means
never use
created. If you


build
Some




Global Variables over three projects

by Ken Halter » Thu, 09 Oct 2003 02:30:06 GMT

an't share "Globals" in a .bas file between multiple projects. There must've been
something else going on with your code. The reason I don't like GlobalMultiUse is that it
takes the creation/destruction stuff away from you. We had a project here that ended up
with 20 instances of a GlobalMultiUse object when they only expected one. I know it was
"programmer error" that caused the problem but still... I removed all GlobalMultiUse
classes (changed them to MultiUse) to force everyone to explicitly create instances.

The thing that attracts people to GlobalMultiUse is that they don't need to declare and
use an object variable to access the properties and methods. They basically become "part
of the language" and instantiate objects when needed. Puke on that. Declare an object
variable, instantiate the object, call it's methods, set it's properties and get rid of it
when you're done.

GlobalMultiUse is not such a good name for the behaviour anyway. There's really nothing
"Global" about it.

--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..


"Frampton Firesword" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...




Global Variables over three projects

by Tim » Thu, 09 Oct 2003 03:48:49 GMT

hanks for your reponses.

What is the eaisest way of declaring global variables over seperate
projects. ie. So the variables can be used from any project.

Cheers

"Ken Halter" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...
must've been
GlobalMultiUse is that it
that ended up
I know it was
GlobalMultiUse
instances.
to declare and
basically become "part
Declare an object
and get rid of it
really nothing
projects
event.
through
and
when
needs
it. I
global
just
files.




Global Variables over three projects

by Ken Halter » Thu, 09 Oct 2003 04:27:50 GMT

They'll need to be in a class somewhere (something like the sample I posted 10/7/2003 9:23
PM)... Note that the post date is wrong... I was messing around with my PC clock to see
what happens to some code when it hits midnight and I forgot to set it back before
posting.... here... I'll repost in case you can't find the original.

Here's one way... basically, you create a property that accepts the object as an argument.
'==================Form Code
Option Explicit

Private mobjClass1Instance As Class1
Private mobjClass2Instance As Class2

Private Sub Command1_Click()
Call mobjClass2Instance.ShowClass1Value
End Sub

Private Sub Form_Load()
Set mobjClass1Instance = New Class1
Set mobjClass2Instance = New Class2

'Set a value
mobjClass1Instance.TestProp = "This is a test"

'Pass the instance of Class1 to Class2
Set mobjClass2Instance.Class1Obj = mobjClass1Instance
End Sub

Private Sub Form_Unload(Cancel As Integer)
'Make sure to clear the extra reference
Set mobjClass2Instance.Class1Obj = Nothing
End Sub
'==================Class1 Code (this represents the "global" data)
Option Explicit

Public TestProp As String
'==================Class2 Code
Option Explicit

Private mobjClass1Instance As Class1

Public Property Set Class1Obj(ByRef Setting As Class1)
Set mobjClass1Instance = Setting
End Property

Public Sub ShowClass1Value()
MsgBox "mobjClass1Instance.TestProp" _
= " & mobjClass1Instance.TestProp"
End Sub
'==================


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..








Global Variables over three projects

by dnagel » Thu, 09 Oct 2003 04:32:04 GMT

think Ken already mentioned it...

Use a Class, initialize it in Sub Main, and then pass it around to
forms or other objects that need it by hanging properties off of
those objects to receive the reference to that class object...

Any other method, albeit there might be slightly less actual typing
involved, is not going to be as robust or consistently available.

You also gain the ability to use property Let/Set/Get routines,
providing read only values as well as read/write and you can
add logic to validate those values in the property procedures.

It really is the only method you should consider. My recommendation
comes from experience through trial and error as well as reading
the brain dumps from guys like Ken... He's rarely incorrect.

D.


Tim wrote:




Global Variables over three projects

by Tim » Thu, 09 Oct 2003 20:21:51 GMT

hanks for all your advice. I will use this method, which I now fully
understand, but didn't at first.

"dnagel" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...
here
one.
create




Global Variables over three projects

by Frampton Firesword » Sat, 11 Oct 2003 02:16:03 GMT

think I discovered that the problem was that although they didn't have to
create instances created of them, they did at least have to be dimmed. Which
is what I wasn't doing, I was expecting them to be like the 'Printer'
object.

But yes I agree, they are rubbish.


"Ken Halter" < XXXX@XXXXX.COM > wrote in message
news:% XXXX@XXXXX.COM ...
must've been
GlobalMultiUse is that it
that ended up
I know it was
GlobalMultiUse
instances.
to declare and
basically become "part
Declare an object
and get rid of it
really nothing
projects
event.
through
and
when
needs
it. I
global
just
files.




Similar Threads

1. Global Variables over three projects

2. Global variables in a project.

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.


3. global variables for whole project not just when user form is runn - Word VBA

4. global variables for whole project not just when user form is

Thanks Jezebel for your quick response, but somehow I don't think I 
understand enough to interpret all your suggestions.  What I've learnt in 
programming I've taught myself so can you please be more specific about how 
to make reference to the form object and making the data values available as 
properties of the form.  Does that mean the input from User Form 1 will still 
be available once the user re-starts the programme and is using User Form 2?

"Jezebel" wrote:

> 'Global variables within modules' suggests you haven't quite understood 
> variable scoping. Variables declared at the top of a module are either 
> global -- meaning they are available to the entire project, or private --  
> meaning they are available only within that module. Be aware that global 
> variables lose their value if you do any editing in VBA.
> 
> Global variables have a bad reputation in programming: they are major cause 
> of bugs; and there are nearly always better ways to move data between 
> objects in the application. In your case, instead of using globals for each 
> data value, you could use a single global with a reference to form object, 
> then make the data values available as properties of the form. The 
> programming advantage is that the form 'owns' the data and can ensure that 
> it is valid.
> 
> 
> 
> 
> "Bev" < XXXX@XXXXX.COM > wrote in message 
> news: XXXX@XXXXX.COM ...
> > Can anyone help me.  I've got a template project with 2 user forms.  Code 
> > has
> > to stop running between using the two user forms to enable the user to add
> > unique information and check statements created to that point, before
> > calculating of each statement occurs.  Then the user re-starts the 
> > programme,
> > so I'm trying to find a way to retain the variable information contained 
> > in
> > the first user form, to be utilised in code for the 2nd user form.  I 
> > already
> > know about declaring global variables within modules, but I'm talking 
> > about
> > retaining variable input for the whole project, over all modules. 
> 
> 
> 

5. Global Variables nott Global?

6. Global Variables on a VB.NET Web Page

Hi:

I'm a relative newby so hopefully this is a simple question!

I have found that I can create global variables easily on a web page by
placing the dim statement before the first "private sub" in a program:

dim mydata as (whatever)

Public Sub Page_Load(etc..)

End Sub

This makes information that I place in this global variable available to
every click event on the page that I create just as it does in regular
VB.NET apps.

I'm curious to know if this is not a "best practice" - is info left in
memory if the user clicks away - or is this a "great practice".  I haven't
seen any demos that use this so I'm curious if there is a down side

Thanks very much!

Fred


7. Question from a transforming VB guy on Global Variable - CSharp/C#

8. Global Variable Ends Up as Nothing in Form

Hi All,

I'm an experienced VB6 developer and now starting (newbee) with VB 2008 and 
I'm very excited.

Here's an issue I'm experiencing right off the starting line and cannot make 
sense of it:

I have a module (Public) and in it contains a few global variables (String 
data type). When I go to use these in a Form the value is "Nothing." Why is 
this?
..
Public Module modMain

Public g_strMyPublicVar As String

Then in the form (frmMain), the g_strMyPublicVar does not contain the data 
as it was set in modMain.


Any insight is greatly appreciated and I thank you in advance!

Kind regards - Fred