Is there a way to swap out the name of a constant using a

Just starting out? Need help? Post your questions and find answers here.
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

Is there a way to swap out the name of a constant using a

Post by mikeschn »

Is there a way to swap out the name of a constant using a variable?

for example, in the procedure below, can I change #String_partno_line0 to #String_partno_line+x?

so that I don't have to type the SetGadgetText(#string_partno_line... 100 times?





Procedure printgrid()
For x = 1 To 100
SetGadgetText(#String_partno_line0,partno1$(x))
Next x
EndProcedure

Thanks,

Mike...
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Is there a way to swap out the name of a constant using

Post by Josh »

Hi mikeschn,

welcome to the Pb-Forum. You gave the answer yourself:

Code: Select all

Procedure printgrid()

   For x = 0 To 99
     SetGadgetText(#String_partno_line0 + x, partno1$(x))
   Next x

 EndProcedure
Of course, this will only run, if all your constants are in ascending sequence.

P.S.: Please use the tags if you publish code-snippets.

Josh
sorry for my bad english
mikeschn
User
User
Posts: 11
Joined: Sat Jun 30, 2012 12:22 am

Re: Is there a way to swap out the name of a constant using

Post by mikeschn »

That worked! I made sure my constants were in some kind of order, and got the value of the first constant in the series... :D

Code: Select all

 Procedure printgrid()
    partno_control_number = #String_partno_line0
    partname_control_number = #String_partname_line0
    vol_control_number = #String_vol_line0
    den_control_number = #String_den_line0
    mass_control_number = #String_mass_line0  
    For k = 0 To 9
      SetGadgetText(partno_control_number+k,partno1$(k))
      SetGadgetText(partname_control_number+k, partname1$(k))
      SetGadgetText(vol_control_number+k, StrF(volume1(k),2))
      SetGadgetText(den_control_number+k, StrF(density1(k),8))
      SetGadgetText(mass_control_number+k, StrF(mass1(k),3))
    Next k  
  EndProcedure
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Is there a way to swap out the name of a constant using

Post by ts-soft »

I think this one is better:

Code: Select all

Enumeration
  #String_partno_line0
  #String_partname_line0
  #String_vol_line0
  #String_den_line0
  #String_mass_line0
EndEnumeration

Procedure printgrid()
  Protected k
  
  For k = 0 To 9
    SetGadgetText(#String_partno_line0 + k, partno1$(k))
    SetGadgetText(#String_partname_line0 + k, partname1$(k))
    SetGadgetText(#String_vol_line0 + k, StrF(volume1(k), 2))
    SetGadgetText(#String_den_line0 + k, StrF(density1(k), 8))
    SetGadgetText(#String_mass_line0 + k, StrF(mass1(k), 3))
  Next k
EndProcedure
Greetings - Thomas
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Post Reply