Solved

Initialising Global Variable

Posted on 2002-04-09
10
169 Views
Last Modified: 2010-05-02
hi everybody,

I have a little problem with the vb. I have defined two sort of global variables. One global variable is defined in main module while one is defined in a form.

I like to know, how to initialise these variable. or in other word how to destroy these variable from the memory.

I have tried to set the form to nothing. but the variable still remains.

Please help me urgently

Sajid
0
Comment
Question by:sajicmajic
10 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 6928060
You can't remove them from memory, the variable defined in the module will always exist, and in VB5 onwards (I think)  the one in the form will always exist as well. You will be able to use the form variable irregardless of whether the form is loaded.

Why do you need to do this?
0
 
LVL 38

Accepted Solution

by:
PaulHews earned 100 total points
ID: 6928183
>I have tried to set the form to nothing. but the variable still remains.

Once the form is unloaded and set to nothing, the variable will not persist.  Here is a simple experiment you can try to prove this.  New project, two forms, three buttons on Form1.  Form1 code:

Option Explicit

Private Sub Command1_Click()
    MsgBox Form2.gX
End Sub

Private Sub Command2_Click()
    Form2.gX = "Hello"
End Sub

Private Sub Command3_Click()
    Unload Form2
    Set Form2 = Nothing
End Sub

Private Sub Form_Load()
    Command2.Caption = "Set form2.gx"
    Command1.Caption = "Show form2.gx"
    Command3.Caption = "Unload Form2"
End Sub


Click the Set form2.gx button.  Now click the show form2.gx button.  It should say "Hello".  Now click the unload button and click the show form2.gx button.  It will show a blank message box.  Once the form is unloaded, any reference to a variable on the form will re-load the form (you can show this by trapping the Form_Load event for this experiment.)

Bottom line:  Unloading a form will destroy all public variables on that form.  There is no way to destroy a public variable in a module, however.  Use of dynamic arrays can help if you are having memory problems, because you can use Erase to clear them from memory.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928236
in my vb version

Form 1

Option Explicit

Private Sub Command1_Click()
Form2.Show
End Sub

Private Sub Command2_Click()
MsgBox Form2.x
Unload Form2
End Sub

Private Sub Command3_Click()
MsgBox Form2.x
End Sub


Form2

Option Explicit

Public x As Long

Private Sub Form_Load()

x = 5

End Sub



  - sequence of msgboxes = 1,2,3  I get command 2 shows '5' as expected - command 3, still shows 5 even though the form is unloaded

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6928253
Don't forget Deighton that you reference to Form2.x will reload the form even when it is unloaded. So it will always be thus.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928458
Tim,

In that case I changed the form2 code to

Private Sub Form_Load()

x = x + 5
MsgBox "Hello!"


End Sub

 - in actual fact clicking on command 3 then simply displays the vaue '5'  - the load event is not fired by referencing the public variable in the form.  This is VB5 I'm using at the moment.  Does VB6 differ?

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 38

Expert Comment

by:PaulHews
ID: 6928478
Yes, and if this wasn't here:

Private Sub Form_Load()

x = 5

End Sub

Then x would be empty (assuming variant) when 3 is clicked.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6928526
Deighton, try setting the form = nothing after you unload it.
0
 
LVL 18

Expert Comment

by:deighton
ID: 6928543
..but I dont get the messagebox

in full, the form2 code is

Option Explicit

Public x As Long

Private Sub Form_Load()

x = 5
MsgBox "Hello!"


End Sub
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6928555
No deighton, you are right.  The variable x is made empty, but the Form_Load doesn't run for command3.  This happens in VB 6 as well.

0
 
LVL 2

Expert Comment

by:corvanderlinden
ID: 6931270
Referencing a global variable in a form or a (selfdefined) property, implicit loads the form's data and code but not the form itself, so no Load event.

PaulHews is right : To get rid of data and code segment unload form and then set form to nothing.

Remember using Form2 you are using the (hidden) global Form2 instance

I prefer using :

dim x as Form2

set x = new Form2

'do the job and let x go out of scope, which is an implicit set x = nothing

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now