Dynamic constant name

  1. 6 weeks ago

    Tim K

    Mar 13 Pre-Release Testers, Xojo Pro
    Edited 6 weeks ago

    I have a list of field names and constants for the corresponding field length that I would like to dynamically set the name for.

    for example
    i have const kCitylen =31

    I can use select case if need be but was hoping not to have to add 20 or so cases
    The idea is to display an edit box to allow interactive truncation
    The problem portion is shown below
    dim field_name as string = "City" dim field_val as string = "Any City 1234567890123456789123456789" dim const_name as string const_name= "k" + field_name + "len" left(field_val,const_name)

    TIA Tim

  2. Dave S

    Mar 13 San Diego, California USA

    create a dictionary instead. the names of constants , variables etc cannot be changed at runtime... that is a feature usually only found in an interpeted language.

  3. Norman P

    Mar 13 Pre-Release Testers, Xojo Pro Enjoying Whistler BC

    the trick with constants and trying to look them up at runtime is that they dont actually retain their name

    effectively everwhere you used a constant treat it like you have just swapped in the literal value

    const kCitylen =31
    
    dim i as integer = kCityLen

    get treated like you wrote

    const kCitylen =31
    dim i as integer = 31
  4. Douglas H

    Mar 13 Pre-Release Testers, Xojo Pro

    @Tim K I have a list of field names and constants for the corresponding field length that I would like to dynamically set the name for.

    Those two are mutually exclusive. By definition a constant is, well, constant. When you need a dynamically set value, use something other than a constant. Such as the dictionary mentioned by Dave.

or Sign Up to reply!