Question:
In Visual Basic 5.0, how do you set up variables so they're available to the whole program?
2008-06-16 10:27:59 UTC
I don't want to have to keep copying code.
(Microsoft Visual Basic 5.0,) very much a novice here! Anyway, can someone please explain how to set up variables so that they're available to the whole program not just to a particular event procedure and give an example? I've tried all the obvious stuff like making sure I explicitly declared all the variables (in this case as variant,) and writing the assignment statements at the top of the code window in what I thought was the general object but things aren't working the way they should. I'm just try to put together a very simple calculator program for the sake of learning the concepts. But I keep having to copy the code lines over and over again which I know I shouldn't have to do.
There must be some simple way to do this. Can you please explain in everyday English with examples?
Three answers:
Mahdi Saffari
2008-06-16 10:50:39 UTC
VB5! um, lemme think...



Add a Module to your application and use the Global keyword to define you variables and make the variable's scope, the entire application.



Simply, declare your variable in the scope you want them to be. If you want it outside your subs and functions, well "DIM" it outside the subs and functions.



But, why do you want to do "Global declaration"? Re-design your application or use alternative ways like files or database tables; Global variables are known to be hard to maintain over time. Think of the time you're opening the vb files you've closed 6 months ago! You'll certainly won't know what's there!!



And one last point, using VB versions prior to VB.Net WILL reduce your functionality and sometimes you will have to write some C++ line to make the dreams come true (for Performance/Size/Professional Programming). I'd suggest while you're a novice as you said, be a novice in VB.Net instead of VB5!



Good luck :)



------------------------------------------

I'm not criticizing you or VB5. I've been coding Basic since Sinclair's Spectrum 48 and I was a VB developer too and I loved the shift from vb4 to vb5 ...



Anyway; You seem to want the answer and nothing more! OK, I'll give you the code:



'Add a "MODULE" to your application, just like when you want to add a form to your app.

'Then write these lines of code inside it.

'Module1.bas

Global nSomeVar As Integer

Global strSomeOtherVar As String



'Now you may use these two variables everywhere in your application. Remember, Since a global variable is always in the scope, It will never reset and will always save its value...



'See section 4 in the below link to get a better idea on what's goin' on...

------------------------------

BTW, I thought I read you wrote "I don't want to have to keep copying code.", that's why I didn't supply the code.



Even MSDN website does not support VB5 users anymore... The oldest VB version on MSDN is 6!



gL ;)
youngboy1606
2008-06-16 11:11:10 UTC
Mahdi couldn't say it better. Why do VB5 apps when you can get a free express .NET version which WILL let you write apps for operating systems moving forward?



Nothing written today in 5 OR 6 will be of any use. Unless your keeping some relic program alive it's time to say good bye.



************************



No your not thats lame. You can get a copy of 2008 express for free that allows you practically all the functionality of a full blown copy of 2008. I can assure you that VB 05 is not going to run well on any newer OS like XP and Vista. That development package came out during Windows 98. That's 10 years ago. If there weren't free express versions available from MS not some cracked POS from who knows where, your argument would hold water.



The other person is correct. Learning VB5 will gain you almost no programming exp that will translate over to .NET. He pointed out very well that if you are learning a lang then learn something that will make you money and be usefull now and tomorrow. Not something you will have to start over again the minute you move to .NET.
2016-04-03 09:11:24 UTC
Hello I know programming and i programme too. Well a variable are values which can be changed. A variable is a instruction or an integer number which is stored and can be chaged by instructions. For example: a = 5; b = 2; a = a + 1; result = a - b; Obviously, this is a very simple example since we have only used two small integer values, but consider that your computer can store millions of numbers like these at the same time and conduct sophisticated mathematical operations with them. Therefore, we can define a variable as a portion of memory to store a determined value. Each variable needs an identifier that distinguishes it from the others, for example, in the previous code the variable identifiers were a, b and result, but we could have called the variables any names we wanted to invent, as long as they were valid identifiers. A valid identifier is a sequence of one or more letters, digits or underscore characters (_). Neither spaces nor punctuation marks or symbols can be part of an identifier. Only letters, digits and single underscore characters are valid. In addition, variable identifiers always have to begin with a letter. They can also begin with an underline character (_ ), but in some cases these may be reserved for compiler specific keywords or external identifiers, as well as identifiers containing two successive underscore characters anywhere. In no case they can begin with a digit. Another rule that you have to consider when inventing your own identifiers is that they cannot match any keyword of the C++ language nor your compiler's specific ones, which are reserved keywords. The standard reserved keywords are: asm, auto, bool, break, case, catch, char, class, these are just few of them. There are lots of identifiers. Very important: The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the RESULT variable is not the same as the result variable or the Result variable. These are three different variable identifiers. For example a simple software like a calculator contains lots of variables in his programme codes. All the variables that we intend to use in a program must have been declared with its type specifier in an earlier point in the code, like we did in the previous code at the beginning of the body of the function main when we declared that a, b, and result were of type int. A variable can be either of global or local scope. A global variable is a variable declared in the main body of the source code, outside all functions, while a local variable is one declared within the body of a function or a block. Global variables can be referred from anywhere in the code, even inside functions, whenever it is after its declaration. The scope of local variables is limited to the block enclosed in braces ({}) where they are declared. For example, if they are declared at the beginning of the body of a function (like in function main) their scope is between its declaration point and the end of that function. In the example above, this means that if another function existed in addition to main, the local variables declared in main could not be accessed from the other function and vice versa.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...