Variable to use with constants

Windows specific forum
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Variable to use with constants

Post 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
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Fred
Administrator
Administrator
Posts: 18357
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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).
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

constants and variables

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Re: Variable to use with constants

Post 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
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

variable and constants

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Doobrey
Enthusiast
Enthusiast
Posts: 218
Joined: Sat Apr 26, 2003 4:47 am
Location: Dullsville..population: me
Contact:

Re: variable and constants

Post 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.
Sub-Routine
User
User
Posts: 82
Joined: Tue May 03, 2005 2:51 am
Location: Wheeling, Illinois, USA
Contact:

Post 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
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Arrays and constants

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Post Reply