Computer Science Canada too many variables? |
Author: | cutecotton [ Tue Dec 24, 2002 5:38 pm ] |
Post subject: | too many variables? |
hi everyone, what i'm trying to do with my program is i'll have 5 litlte buttons, and the buttons will change when the user runs their mouse over it. So let's say for button 1 i have 2 variables for it, one contains the pic b4 the mouse is run over, the 2nd contains the pic that will be dispalyed when the mouse is over it. Now my question is, i'll have to do that for all 5 buttons, so does that mean i'll have to declare 10 variables? since i'll need 2 for each button? that seems to take a lot of...well variables and i don't know if there's a limit of the amoutn of varaibles i can declare. is there a more efficient way of doing this? or will i really have to declare a variable for every single pic i put on screen? |
Author: | Tony [ Tue Dec 24, 2002 6:20 pm ] | ||
Post subject: | |||
try using arrays.
that assuming you know what arrays are. |
Author: | cutecotton [ Tue Dec 24, 2002 6:22 pm ] | ||
Post subject: | |||
*ahem* goes to find an array tutorial hmm...well i found an array tutorial and tried to understand it best as ic an..so from waht u posted.. var picID : array 1..5, 1..2 of int for i:1..5 for i2:1..2 picID(i,i2) := Pic.FileNew("image"+i+i2) end for end for u have 5 variables, and within them there's 2. What i dont' get is this line:
I undersantd the Pic.FileNew, and then u put hte filename in replacement of image, but wha'ts the +i+i2 thing? [/code] |
Author: | Tony [ Tue Dec 24, 2002 6:29 pm ] |
Post subject: | |
http://www.compsci.ca/v2/viewtopic.php?t=71 though it was intended for Visual Basic. But it explains what arrays are and how to use them. In turing you declear arrays as var arrayname : array first demention , second demention , etc of type where dementions are ranges such as 1..10 (1 to 10) and work same as in for loop type is string/int/real/etc array variable is accessed using arrayname(1st demention , 2nd demetion) as in arrayname(5,2) |
Author: | cutecotton [ Tue Dec 24, 2002 6:36 pm ] |
Post subject: | |
ooo i get it...*kinda* have to test it out myself to se eif it works..will post back in a few minutes ^^ |
Author: | cutecotton [ Tue Dec 24, 2002 6:43 pm ] | ||
Post subject: | |||
right..so now what do i do if i wanna use the variables and make them show up? sof ar i go:
so in my Pic.Draw (buttons, 60, 130, picCopy)...i don't know what i shoudl put in replacement of my "button"? |
Author: | Tony [ Wed Dec 25, 2002 12:04 am ] |
Post subject: | |
it should be button(1,1) for first button, 1st view button(1,2) for first button, 2nd view etc... button(5,2) is 5th button, 2nd view instead of numbers you can use variables to make it easier. such as button(x,y) |
Author: | cutecotton [ Sat Dec 28, 2002 10:23 pm ] | ||
Post subject: | |||
hmm yeah, but then each button woudl have a differnet file. Example (the goal here is that i have 5 levels, and the user select which lvl they want. So lvl 1 would have 2 pics, but ecah lvl's pic is differnt, cuz the pic woudl say lvl 1 for lvl1, lvl 2 for lvl 2, etc) So what i did was i used arry to delcare all the variables, and then i just took ecah one and idenfied it. So i have something like
|
Author: | Albrecd [ Thu Nov 10, 2005 2:49 pm ] |
Post subject: | |
Or you could just declare 10 variables... it's not really that many... and it doesn't matter because there's no limit. |
Author: | do_pete [ Thu Nov 10, 2005 2:52 pm ] |
Post subject: | |
yeah but array are more efficient |
Author: | do_pete [ Thu Nov 10, 2005 2:56 pm ] |
Post subject: | |
wow i just realized that this topic is from Tue Dec 24, 2002. you realize that was almost THREE YEARS AGO |
Author: | MysticVegeta [ Thu Nov 10, 2005 3:02 pm ] | ||
Post subject: | |||
yeah i was wondering how come no one pointed it out, anyways, instead of going buttons(1), buttons(2) why not PSEUDOCODE
Smart way to do it |
Author: | Cervantes [ Thu Nov 10, 2005 3:44 pm ] |
Post subject: | |
Albrecd wrote: Or you could just declare 10 variables... it's not really that many... and it doesn't matter because there's no limit.
You couldn't be more wrong. Compared to one, ten is a lot. And you say there's no limit. If that's true, that makes your method even worse. |
Author: | codemage [ Fri Nov 11, 2005 9:01 am ] |
Post subject: | |
I'm sure cutecotton has either solved this problem or given up by now. |
Author: | Cervantes [ Fri Nov 11, 2005 4:54 pm ] |
Post subject: | |
codemage wrote: I'm sure cutecotton has either solved this problem or given up by now.
Yeah, but Albrecd hasn't solved his problem. |
Author: | Albrecd [ Mon Nov 14, 2005 12:47 pm ] |
Post subject: | |
Quote: yeah but array are more efficient
Unless you don't know about arrays, in which case it would take far less time and effort to stop worrying about arrays and just use the 10 variables. |
Author: | do_pete [ Mon Nov 14, 2005 1:01 pm ] |
Post subject: | |
if you don't know how to use arrays you won't get to far in programming |
Author: | wtd [ Mon Nov 14, 2005 1:22 pm ] |
Post subject: | |
Albrecd wrote: Quote: yeah but array are more efficient
Unless you don't know about arrays, in which case it would take far less time and effort to stop worrying about arrays and just use the 10 variables. Programming concepts are like tools in a toolbox. "It'll take too long to learn how to use this fancy ratcheting wrench. I'll just use a standard wrench." It doesn't take long before the time saved by learning better ways to do things pays for itself. |
Author: | codemage [ Mon Nov 14, 2005 1:31 pm ] |
Post subject: | |
Even if you have the "tools", there's a fuzzy area (for me, it's usually in the area of 3ish related variables) where using arrays vs. independent variables are worthwhile. No sense bludgeoning yourself with a bunch of for loops (for stepping through arrays) when you can do without. For 10 variables though, we're definitely in the area where arrays make the code cleaner & more readable. Use the "tool." It's free. |