Author Topic: create variable on user request  (Read 2037 times)

0 Members and 1 Guest are viewing this topic.

Offline qbkiller101

  • Newbie
  • Posts: 73
    • View Profile
create variable on user request
« 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?

FellippeHeitor

  • Guest
Re: create variable on user request
« Reply #1 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: create variable on user request
« Reply #2 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! 
« Last Edit: June 02, 2020, 12:20:33 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: create variable on user request
« Reply #3 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.  
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack