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 / issue with a global variable

Author
Message
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 15th Sep 2014 09:40
first off id like to apologize for the naming of my variables i usually just give them a letter and a number and move on however i tried to comment as best i could.
so i had an itch to build a custom browser for my world editor for loading and exporting models ect.
My problem is im trying to reuse me function text_position() on line 405. it worked fine when it was part of the main body of the other function normallisting() you can see i stuck it back in there so it would run. i sucessfully reused it in my other function file_type_filter(). but the same code wont run in the function i built it for and im kinda baffled so i thought i would try to global the command obviously but it seems to throw me a compiler error and i dont understand because its always used as an integer.attached is the media


A child's dream never dies.

Attachments

Login to view attachments
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 15th Sep 2014 09:41
aa is the variable i was trying to global.

A child's dream never dies.
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 15th Sep 2014 13:09
That kind of variable typing isn't good in the global scope; as your project grows it will make it nigh on impossible to know what "aa" is supposed to represent.
I would recommend providing the value of aa as a parameter to your function(s); if you need to also update them from the function you can return the new value and call it something like "aa = my_function(aa)".
If you need to CHANGE the value of more than one value from within the function you can use globals, but beware so that no other function / part of your main code is also changing the same variable then (proper naming will help avoid doing this by accident). If you just need ACCESS to more values in your functions you can make them take more than one argument; something like this:




"Why do programmers get Halloween and Christmas mixed up?"
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 16th Sep 2014 06:26
hmm not sure i follow
"That kind of variable typing isn't good in the global scope; as your project grows it will make it nigh on impossible to know what "aa" is supposed to represent."
aa always represents the same thing, the current checklist value.
1 to whatever the maximum checklist value is. Every time I use aa it is in the loop or a function in the loop. therefore aa always has the same value because the only value going into it is from the checklist and when used in a function is in reference to the checklist. The program as u can see runs as it should. but my same function that i used before doesnt seem to work the same way under simular circumstances, theres very little difference in between the two usages of text_position(), my question is why doesnt the same function work. it completely jacks the program up, if you change filter=0 to filter=1 at the start of the program it works just fine using the function to organise the filtered checkilist "only .csm,.x and.obj files will show" and it works fine when it is put back in place of normallisting()but not as a function. so basically code works as code but not as function confused as to why. sorry if i babble. so code that works in main loop for filter and normal listing just fine only works for one as as a function. so i tried globaling it and got a weird result.

A child's dream never dies.
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 20th Sep 2014 04:37 Edited at: 20th Sep 2014 06:04
What he means is that aa as a name gives no information about what sort of data it contains. What happens if later you forget that you used aa already and create a 'new' variable aa? Weird stuff that is very difficult to track down.

If aa is meant to store the current checklist value, why not just name it curListValue or something which makes it clear what is stored and is less likely to be accidentally reused elsewhere?

After looking through your code, the real problem here is that I think you do not have a clear understanding of variable scope.

None of your functions have any parameters being passed in to them, yet they are using the same variable names as elsewhere in the code.

if the variables are not global, then aa used here:

function text_position()
if aa <=20

has a completely different value to aa used here:

function normallisting()

perform checklist for files
find first
ab#=checklist quantity()
for aa =1 to ab#

and aa used here is completely different yet again:

print checklist string$(i)
aa=0

without global, this same variable name actually refers to 3 separate local variables with separate values.

Now, if you make aa global, you are going to get weird results because you are using it in compelely different manners in different places.

aa used the function normallisting is used as an iterator in a for:next loop and stores the current position within the checklist of files.

this value seems to be fundamentally different from the way aa is used in function file_type_filter:

inc aa,1
text_position()
`ink rgb (0,1,0),0
print checklist string$(i)
aa=0
endif
next : REM -> -> -> !!! This should probably be 'next i' to properly close off 'for i=1 to ab#' above !!!

From what I can tell, file_type_filter is only called after normallisting, therefore, if aa is global, its value in file_type_filter will always be = the last file # / total count of the checklist in normallisting, + 1 due to inc aa,1

without global, aa in file_type_filter will always start = 0, be increased to 1 then reset back to 0 during the function call.

Anyway, it is not really clear what aa is meant to achieve in file_type_filter, I can see that it is used to try to establish the position at which to write some text, but in both functions you are iterating over a checklist for files, during file_type_filter, aa will only ever be equal to the first or last item of the list, and will not be in sync with the iteration of for i = 1 to ab#

smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 22nd Sep 2014 01:16
ty ortu your exactly right. started taking a c++ class last week, looks like ive been using functions wrong for the most part and not keeping track of private and public vars correctly. Its a bit hard to grasp the underlying concepts of dbpro with no previous programming experience and no tutorials. been a dbpro help file learner until now. gonna wait a while and rebuild it from scratch when i know how, wanted to program it differently actually but didnt know how to do a few things like arrays and i guess its polymorphism im thinking of like a mutating variable.

A child's dream never dies.
Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 26th Sep 2014 00:24
To help getting into the practice and understanding of "Global" variables, also know that you can use "Local" command on your function variables too. By default, they are local, but typing it out makes it look nice and clear, easy to read.

Also, when I declare a set of global variables, I usually tuck them away in a subroutine, as the subroutine can be folded away easily.

Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia
Ortu
DBPro Master
16
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 26th Sep 2014 06:11 Edited at: 26th Sep 2014 06:12
Local is helpful. I also use all caps for globals:

GLOBAL SOME_GLOBAL_VARIABLE as string : SOME_GLOBAL_VARIABLE = "oh hi there!"

Some people prefix it with some variant of 'g' : gSomeVariable GsomeVariable g_someVariable G_someVariable you get the idea. The more at a glance information a variable's name can give the better.

Burning Feet Man
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 26th Sep 2014 06:43 Edited at: 26th Sep 2014 13:36
One draw back of actually naming a variable as Global_Variable_A, is that if your code changes scope and suddenly your Global_Variable_A is no longer Global, then the perfectionist in you will be doing a find & replace on all the incorrectly named variables, which can be a little tedious if something is missed during the process.

So one thing that solves this problem, is you use an advanced IDE (I use Indigo) which will automagically inform you of what's global & what's local at a quick glance. Theme globals red on yellow text, and make local blue & bold Comic Sans. Straight away, when you're staring at comic sans, you know it's local, and if it's bright red & yellow the that's a global.

EDIT: An image says a thousand lines of code... note how the local variable theme does not extend outside of the function where it's declared.



Help build an online DarkBASIC Professional help archive.
DarkBasic Help Wikia

Login to post a reply

Server time is: 2024-04-19 17:29:08
Your offset time is: 2024-04-19 17:29:08