Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / How do I use shared variables in DB 1.13?

Author
Message
Video Boy
20
Years of Service
User Offline
Joined: 21st Mar 2004
Location:
Posted: 6th Jul 2004 06:11
I instinctly used the word SHARED to declare one of my variables as shared, but... that keyword doesn't exist!
So, how can I declare shared variables?
Don Malone
21
Years of Service
User Offline
Joined: 27th Apr 2003
Location: Birmingham, Alabama
Posted: 6th Jul 2004 06:41 Edited at: 6th Jul 2004 06:47
The only way to create shared variables is by declaring them in an array I believe.

Any array is global or shared by default.

Otherwise you will have to pass your variables to your functions in the function call.

[Edit]

from the DarkBasic Classic help

Your parameter list can be left empty, or you can specify up to 255 parameter names inside the brackets. A parameter is declared by simply entering a name. If you require more than one parameter, make sure you use commas to separate them. You may enter commands for your function starting on the line following the declaration. You must declare the end of a function using the ENDFUNCTION command. If you wish your function to return a value, simply add the variable or value after the ENDFUNCTION command. You must leave a space between the endfunction command and the variable or value you wish to return.

Any variables declared in the function are what are known as local variables. Local variables can only be recognized and used from within the function. Global variables on the other hand are variables declared outside of any function. Your function cannot recognize or use global variables. Your program cannot recognize or use local variables if you are running outside of the function in question. This allows you to re-use variable names within a function without fear of overwriting some other global variable elsewhere in your program, amongst other advantages. The same rules apply to arrays and parameters passed into the function. Parameters declared by the function can be used in the same way you would use any local variable, in order to bring information from the outside world.

Wasting CPU Cycles since the 286 was a hot machine.
Dgamer
22
Years of Service
User Offline
Joined: 30th Sep 2002
Location:
Posted: 6th Jul 2004 07:31
or use the command global

This sig has been dullified!
Don Malone
21
Years of Service
User Offline
Joined: 27th Apr 2003
Location: Birmingham, Alabama
Posted: 6th Jul 2004 07:54 Edited at: 6th Jul 2004 07:56
Note this sentence from the help.

Quote: "Global variables on the other hand are variables declared outside of any function. Your function cannot recognize or use global variables."


I hope you prove me wrong though. There are times when I would love to use global variables even though it is considered bad programming practice.

General wisdom is expose no more variables to inadvertent change than is required.

Video Boy, Are you meaning shared with all program functions or shared with outside programs?

Wasting CPU Cycles since the 286 was a hot machine.
SandraD
20
Years of Service
User Offline
Joined: 30th May 2004
Location: Down on the corner, out in the street.
Posted: 6th Jul 2004 10:22
The global command only works for DBPro, not DBC. For shared values in DB Classic (1.12/1.13) just make them into arrays;

dim a(0)
dim b(0)

and so on. It makes the code look a little more complicated than it really is, but it works.
S.

Any truly great code should be indisguishable from magic.

Login to post a reply

Server time is: 2024-11-27 00:34:12
Your offset time is: 2024-11-27 00:34:12