Page 1 of 1

Variable to use with constants

Posted: Tue May 24, 2005 4:46 pm
by PB&J Lover
Is something like this allowed?

Code: Select all

For x = 1 To 8
menu$ = "MENU_"+Str(x)
DisableMenuItem(menu$,1)
Next x
I know this doesn't work, but is there a way to address multiple constants with variables?

Thanks

Posted: Tue May 24, 2005 4:53 pm
by Fred
No, PureBasic is a compilated langage which means than such expression can't be resolved at runtime (this isn't the case of PHP for example).

constants and variables

Posted: Tue May 24, 2005 5:02 pm
by PB&J Lover
OIC, that makes sense. I think. Although I know of other compiled language that do this.

Anyway, you're doing a great job Fred. Love the language (PB). This language could be as ubiquitous as MicroSoft one day...keep at it (don't forget us app writers, the gamers have enough stuff right now. We need double floats and such). :D

Thanks.

Re: Variable to use with constants

Posted: Tue May 24, 2005 7:06 pm
by Num3
dbwisc wrote:Is something like this allowed?

Code: Select all

For x = 1 To 8
menu$ = "MENU_"+Str(x)
DisableMenuItem(menu$,1)
Next x
I know this doesn't work, but is there a way to address multiple constants with variables?

Thanks
But you can do this :twisted:

Code: Select all

#menu1=1
#menu2=2
#menu3=3

For x = 1 To 3 ; or whatever number interval defined for menu's
DisableMenuItem(x,1)
Next x

variable and constants

Posted: Tue May 24, 2005 8:01 pm
by PB&J Lover
Right, I see. But I don't know the constant values in this case because it is enumerated in a separate file, but thanks for the tip.

What about using variable and constants.

I have a procedure that talks to various controls. Can I say:

ConID = #Gadget

And then: SetGadgetState(ConID, 1)

Is using variables like this Kosher?

Thanks.

Re: variable and constants

Posted: Tue May 24, 2005 11:03 pm
by Doobrey
dbwisc wrote:Right, I see. But I don't know the constant values in this case because it is enumerated in a separate file
Assuming all your menu constants are enumerated in one block, try something like this..

Code: Select all


Enumeration
 #Menu_Start
 #Menu_1=#Menu_Start
 #Menu_2
  .. blah blah...
 #Menu_666
 #Menu_End
EndEnumeration

For a = #Menu_Start To #Menu_End-1
  DisableMenuItem(a,1)
Next
Edit.. didn't see the second part of your post :roll:
Yes,perfectly legal to use a variable as the gadget number parameter.

Posted: Mon May 30, 2005 9:20 pm
by Sub-Routine
I made a lookup table in my program to refer to some of the constants using an array. I needed an 8 X 4 array, but it should work with more or less dimensions. The Array is dimensioned early in the program and the sub-routine need be called only once.

Code: Select all


Constants_To_Array:

Array(0,0) = #Reverse_Gear_Input
Array(0,1) = #Reverse_Gear_Output
Array(0,2) = #Reverse_Gear_Overall
Array(0,3) = #Reverse_Gear_Text
Array(1,0) = #First_Gear_Input
Array(1,1) = #First_Gear_Output
Array(1,2) = #First_Gear_Overall
Array(1,3) = #First_Gear_Text
Array(2,0) = #Second_Gear_Input
Array(2,1) = #Second_Gear_Output
Array(2,2) = #Second_Gear_Overall
Array(2,3) = #Second_Gear_Text
Array(3,0) = #Third_Gear_Input
Array(3,1) = #Third_Gear_Output
Array(3,2) = #Third_Gear_Overall
Array(3,3) = #Third_Gear_Text
; etc...

Return

Kind of a pain to make and I'm sure it could be done a better way, but it only need be done once.

Then I can use the variables in loops:

Code: Select all


For I = 0 To Speeds
  Gear$=GetGadgetText(Array(I,0)) : Gear = ValF(Gear$) : Gears(I) = Gear
  SetGadgetText(Array(I,2),StrF(Final_Drive*Gear,2))
  Gosub Top_Speed ; returns MPH
  SetGadgetText(Array(I,1),StrF(MPH,2))
Next
Return

Took a couple of days and some distraction before it hit me. Turned a lot of spaghetti into nice, neat, cloverleafs.

Rand

Arrays and constants

Posted: Mon Jul 25, 2005 5:08 pm
by PB&J Lover
I tried this and I got some system errors. PB didn't seem to like me using arrays with constants, but regular variable work.

Also, when I tried a FOR/NEXT loop using commonly enumerated constants, I got system errors too.