
-----------------------------------
StarGateSG-1
Thu May 18, 2006 7:23 pm

Global Variables
-----------------------------------
I was wondering it there a global variable set up for VB6, my plan is to have a series of forms that will collect data form a user and then allow all that information to be placed in a final forms for processing and display.
So can this be done?

-----------------------------------
wtd
Thu May 18, 2006 10:01 pm


-----------------------------------
Of course you can have global variables in VB6.  It's a phenomenally bad idea, though.

-----------------------------------
StarGateSG-1
Thu May 18, 2006 10:02 pm


-----------------------------------
Well what would be a better idea, past the data to a file and open it later. If not could you point in a good direction?

-----------------------------------
wtd
Thu May 18, 2006 10:41 pm


-----------------------------------
If you need to have an app gather data, then exit, then run again and be aware of that previously gathered data, then you want a database.  If you're using VB6, then you're limiting yourself to Windows, and there you can use the free versions of Microsoft's database.

Research "MSDE".

-----------------------------------
NikG
Fri May 19, 2006 12:24 am


-----------------------------------
I'd like to know why global variables are a bad idea.

In case you're still interested Stargate, you just have to declare the variable outside of any procedures/functions to make it global for that whole page of code. If you want a variable global to the whole program, just create it in a module.

Also, if you don't have a lot of data to remember, but you want a permanent source to keep it at, I believe VB6 has a way to save app settings to the registry... you'd have to look it up because I've forgotten what the functions were.

-----------------------------------
StarGateSG-1
Fri May 19, 2006 6:52 am


-----------------------------------
I don't want to get into databases atm, with the time closing in I need to stay within my knowledge, Thats why I want global variables.

Also I know that program is limited to windows, it would not make sense otherwise because my entire board will be using this when it is done.

-----------------------------------
Darkmantis
Fri May 19, 2006 8:15 am


-----------------------------------
just right click on one of your forms(it doesnt matter which) then click new then module just like the screen shot.
http://www.geocities.com/darkmantis2004/scet4.jpg

then type "global" then all your values after.
for example:
 Global value1, value2, value3

-----------------------------------
StarGateSG-1
Fri May 19, 2006 11:09 am


-----------------------------------
Umm...

I tryed that and it did not work, it just kept coming up with errors, are you sure that works in VB6

-----------------------------------
wtd
Fri May 19, 2006 11:24 am


-----------------------------------
Global variables won't give you persistent data between runs on the program, unless VB has a definition of global variables that's very different from any other languages'.

-----------------------------------
Darkmantis
Fri May 19, 2006 12:51 pm


-----------------------------------
well I tryed :(

-----------------------------------
GlobeTrotter
Fri May 19, 2006 3:11 pm


-----------------------------------
Obviously you can use global variables, but it is strongly reccomended not to.  If you must, the declarations are like this (in a module):

Global VarNum1 as Integer
Global StrInput as String


In order to avoid the use of such variables, look into running your program from a main procedure, Sub Main().  Create data types that each form will export, and then call the forms as function.  eg:

Public Sub Main()
   Dim DataResult1 as MyType1
   Dim DataResult2 as MyType2
   Dim frm1 as New FormInput1
   Dim frm2 as New FormInput2
   frm1.Show
   DataResult1 = frm1.GetUserInput()
   frm2.Show
   DataResult2 = frm2.GetUserInput()
End Sub


That's the kind of thing you should be trying.

-----------------------------------
NikG
Fri May 19, 2006 11:45 pm


-----------------------------------
Following up to my post above, the commands to save and get from the registry are GetSetting and SaveSetting.
(see [url=http://www.vbforums.com/showthread.php?t=232113]here)
They work by saving anything you want under your application's name...  I believe the problem with them is that they save to HKEY_CURRENT_USER so opening the program in another user account would make the saved values unavailable.

-----------------------------------
StarGateSG-1
Wed May 24, 2006 7:43 am


-----------------------------------
I got the global variables working, it was a just a typo that I did not notice sorry for posting that I didn't get it. Also I am sorry for any confusion.

I have only been learning VB for about 8 days.

-----------------------------------
codemage
Wed May 24, 2006 8:40 am


-----------------------------------
Global variables are bad, in general, because of their non-local-limited scope.  They can potentially be modified from anywhere, and any part of the program may depend on them. They have unlimited potential for creating mutual dependencies, which in turn, needlessly increases complexity.

There are, of course, exceptions to every rule.  The exception for GVs are that sometimes they are very efficient.

-----------------------------------
StarGateSG-1
Wed May 24, 2006 9:13 am


-----------------------------------
The program is never going to used as a public tool and only poeple trained to use and edit it will be using it.
