Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Global Variables
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
StarGateSG-1




PostPosted: Thu May 18, 2006 7:23 pm   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu May 18, 2006 10:01 pm   Post subject: (No subject)

Of course you can have global variables in VB6. It's a phenomenally bad idea, though.
StarGateSG-1




PostPosted: Thu May 18, 2006 10:02 pm   Post subject: (No subject)

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




PostPosted: Thu May 18, 2006 10:41 pm   Post subject: (No subject)

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




PostPosted: Fri May 19, 2006 12:24 am   Post subject: (No subject)

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




PostPosted: Fri May 19, 2006 6:52 am   Post subject: (No subject)

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




PostPosted: Fri May 19, 2006 8:15 am   Post subject: (No subject)

just right click on one of your forms(it doesnt matter which) then click new then module just like the screen shot.
Posted Image, might have been reduced in size. Click Image to view fullscreen.

then type "global" then all your values after.
for example:
code:
Global value1, value2, value3
StarGateSG-1




PostPosted: Fri May 19, 2006 11:09 am   Post subject: (No subject)

Umm...

I tryed that and it did not work, it just kept coming up with errors, are you sure that works in VB6
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri May 19, 2006 11:24 am   Post subject: (No subject)

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




PostPosted: Fri May 19, 2006 12:51 pm   Post subject: (No subject)

well I tryed Sad
GlobeTrotter




PostPosted: Fri May 19, 2006 3:11 pm   Post subject: (No subject)

Obviously you can use global variables, but it is strongly reccomended not to. If you must, the declarations are like this (in a module):
code:

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:
code:

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




PostPosted: Fri May 19, 2006 11:45 pm   Post subject: (No subject)

Following up to my post above, the commands to save and get from the registry are GetSetting and SaveSetting.
(see 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




PostPosted: Wed May 24, 2006 7:43 am   Post subject: (No subject)

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




PostPosted: Wed May 24, 2006 8:40 am   Post subject: (No subject)

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




PostPosted: Wed May 24, 2006 9:13 am   Post subject: (No subject)

The program is never going to used as a public tool and only poeple trained to use and edit it will be using it.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 15 Posts ]
Jump to:   


Style:  
Search: