What is a Common Shared variable% and how do you use it?

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
VERY CONFUSED

What is a Common Shared variable% and how do you use it?

Post by VERY CONFUSED »

Hello again...What is a Common Shared variable% and how do you use it?
HELP???
:roll:
(yes...again)
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

A Common Shared variable is one that is "global", aka is accessible anywhere in your project. It's especially useful when you make programs with mutiple modules. Say you have something like this:

Code: Select all

COMMON SHARED a1 AS INTEGER
a1 would be accessible from anywhere in your program. When you use COMMON SHARED to share variables across modules, you put the same COMMON SHARED lines in the exact same order at the beginning of each module. That way, that a1 would be accessible from every module that has the code at its top.
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

:p
I would like to clarify a few things.
First off you don't need COMMON SHARED at the beginning of each module in order for it to be global. You only need it at the beginning of your main module.
Second when we say module we mean your Subs, Functions, and your Main Module.
Third if you want to transfer an array across you do this code:
DIM SHARED Arrayname(Subscript limits) AS Typename

Sorry Nekro,
-Mit

P.S. you put the COMMON SHARED in the exact order at the beginning of each module that you want to change the variable names for that module.
QB never Transferrs variables across modules. It just transferrs the values of those variables and stores them in new ones. You need COMMON SHARED at the beginning of your Main Module but not the others because when you ommit that in the others QB automaticaly stores the values that were transferred in duplicate variables that were used in the Main Module. However if you want to change those variables and not have them having the same name, you put COMMON SHARED at the beginning of that Module in ORDER. The values will be transferred between the corresponding variables thats why order matters.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Yes, you do need it in each module.

And no, a module is another basfile, not your subs and functions.

And no, to share an array:

Code: Select all

Common shared Array() As MyType
Dim shared Array(100) As MyType
(Might be you can skip a shared somewhere, doesent really matter)


The variable namechange that they talk about is:

Module 1:

Code: Select all

Common shared a, b
a = 10
b = 5
Module 2:

Code: Select all

Common shared c, d
In module 2 c would equal 10, and d would equal 5 (Or any other value when/if you change the variables in either module)

That is, if you change a in module 1, c will change in module 2 too.
Same in reverse, change d in module 2, and b will change in module 1

Although changing variable names isnt something I would recomend, it's easier to keep the same ones across modules.
I have left this dump.
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

ummm.. ya sorry about that reply.
You were right on that array thing,
about 10 mins after I posted it I realized
that you could do it the other way too :( .
Also about the module thing, we were talking
about two separte things so that's why the info was
confused but I know I'm right when you
are talking about the SUB and FUNCTION thing.
Also, I believe that I'm right about the module being
a sub, function or your main and that another
BAS file is just Another BAS file attached to your program...
I'm pretty sure about this but I may be wrong.
I also agree with you about the changing name thing is not a good idea
because it just isn't, and you are right about the other BAS files too.
As for my Last Also, ummmm... vry Confused: which do you mean, the
COMMON SHARED for subs, and functions or the other BAS thing?

-Mit
P.S. GOTOs are B...A...D... :wink:
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Perhaps you should learn your terminology. QB itself refers to additional sources as Modules. It refers to Subs and Function as ... well, as Subs and Functions. There should be zero confusion as to this. As for the Sub and Function thing regarding COMMON SHARED, of course you're right, just try putting a COMMON SHARED inside a Sub or Function and see if QB doesn't produce an error. However, next time you may want to actually know what you're talking about before you tell someone like me that they're wrong... :P

"I also agree with you about the changing name thing is not a good idea because it just isn't" It just isn't, eh? Umm okay...nice and concise. It's not because it "just isn't", it's because variables shared across modules are shared for a very good reason...to have that data accessible in those other modules. Why would you want to change the name, when you already know what the variable is for in the first place? Changing the name would only confuse you in the long run unless you had some elaborate naming scheme set up, and if that's the case, you shouldn't be programming...you should be an interior decorator.

Now what's this all about? "P.S. GOTOs are B...A...D..." Do you even know what you're talking about? :? I don't know how many times it's been said, but there is nothing wrong with using GOTO if it speeds up development. It can lead to bad programming habits in the hands of a newbie, but to an experienced coder, it can be a quick way out. Not recommened for group projects, but works great for the lone wolf coder at times.
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

Ok:
1. Yea I guess I do have to learn my terminology better...
2. I thought that the reason that changing variable names wasn't a good idea was implied.
3. Like I said before I shouldn't have even been typing yesterday
but I'm better today, and GOTOs should still DIE :twisted: !

-Mit
Mitth'raw'nuruodo

Post by Mitth'raw'nuruodo »

ummm...PLEASE ignore that comment about the GOTOs :)
Refer to the other forum that I was mentioning GOTOs for an explination.
srry... :oops:

-Mit
Post Reply