QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: qbkiller101 on June 02, 2020, 12:09:05 pm

Title: create variable on user request
Post by: qbkiller101 on June 02, 2020, 12:09:05 pm
hi,
is it possible to create a variable of which the name will be supplied using input and then use the var without actually knowing its' name?
Title: Re: create variable on user request
Post by: FellippeHeitor on June 02, 2020, 12:12:13 pm
Not possible. Closest you will get is having an array for "names" and another for actual contents, and store data there. Or maybe:

Code: QB64: [Select]
  1. TYPE userVarType
  2.     Name AS STRING
  3.     StringContents AS STRING
  4.     NumberContents AS _FLOAT
  5.  
  6. REDIM userVars(100) AS userVarType
  7.  
  8. 'then, based on user input, you can increase a variable counter:
  9. totalvars = totalvars + 1
  10. userVars(totalvars).Name = whateverTheUserWants$
  11. userVars(totalvars).NumberContents = whateverTheUserEntered
  12.  

Hope the pseudocode above makes sense.
Title: Re: create variable on user request
Post by: SMcNeill on June 02, 2020, 12:18:45 pm
You'd need to DIM an array to get the user variable name and then compare the user input verses that array for matches.


DIM UserVar(2) AS STRING

FOR I = 0 TO 2
    INPUT "Enter a variable name=>"; UserVar(1)
NEXT

DO
    'Let user input whatever they want
    'Parse what they want and compare their variable names to those stored in the array
    'Display results
LOOP 'Until the user is finished


So at start the user can enter X, Y, Z for three variable names.  Then the user can do things like X = 3.  Y = 5.  Z = X + Y...  as long as you parse and code to allow the user such actions in your program.

** Fellippe beat me to it.  I type too slow on my iPad.  :'(   I want my new PC already! 
Title: Re: create variable on user request
Post by: RhoSigma on June 02, 2020, 02:02:37 pm
An alternative is my tag string API as I use in my GuiTools Framework. The API consists of the TagSupport.bi/.bm files in the folder dev_framework\support. The API is not GuiTools specific and can be used as is in any application. Documentation is in docs\GuiToolsFramework.pdf or the include files itself.

Code: QB64: [Select]
  1. '$INCLUDE: 'TagSupport.bi'
  2.  
  3. userData$ = ""
  4. LINE INPUT "1.VarName: "; n$
  5. LINE INPUT "1.  Value: "; v$
  6. SetTag userData$, n$, v$
  7.  
  8. LINE INPUT "2.VarName: "; n$
  9. LINE INPUT "2.  Value: "; v$
  10. SetTag userData$, n$, v$
  11.  
  12. LINE INPUT "3.VarName: "; n$
  13. LINE INPUT "3.  Value: "; v$
  14. SetTag userData$, n$, v$
  15.  
  16. LINE INPUT "Query which VarName: "; n$
  17. PRINT      "   ... its value is: "; GetTagData$(userData$, n$, "unknown variable")
  18.  
  19. '$INCLUDE: 'TagSupport.bm'
  20.