Board Logo
« Get/set variable by name »

Welcome Guest. Please Login or Register.
Nov 20th, 2017, 6:49pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Please use the forums Search feature before asking.
Please post code using the code box described in Format Your Messages.
This will keep indentation, separate it better form the message and prevent gibberish.
If the code is too long for one post or additional files are needed, upload a ZIP archive to the Just BASIC Files Archive Site.

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Get/set variable by name  (Read 84 times)
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3614
xx Get/set variable by name
« Thread started on: Nov 14th, 2017, 04:17am »

New JB2.0 command (texteditor "!insert") made possible get/set numeric variables by name
(gets integer only, sets any)
(using texteditor and graphicbox as a means.
Likely really slow)
For sring variables, it works even in JB 1.01.

I see it as possibility to add debug console to inspect/change any(? must be visible) program variable during program' runtime

Anyway, it's just cool for me.
Code:
'Get/Set var by name (not quite EVAL but...)
'tsh73 Nov 2017
'new texteditor command - INSERT - added in JB2.0 allows get number variable value by it's name
'this allows kind of debug console - to ask/change any variable in a program during runtime
'   (for numbers, get only integer part)

'Now, fiels set to string variable (a$) and string value
'so get/set str pair of buttons work.
'Change a value, give it a try.
'Log set to check a$ variable then setting - so it you change var name it will not log
'(but it'll continue work as getStr shows)

'now change fileds to "a" and "123"
'It works for numeric variables.
'   Actually set works with any number (1e3,2.5, 2.5e-2),
'       while get returns truncated integer (test shows truncated to zero)



if val(Version$) <2 then notice "GetNum button does not work in JB 1.01 (command needed)"

    nomainwin

    WindowWidth = 550
    WindowHeight = 410

    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)

    texteditor #main.txtEd, 26, 16, 100, 100
    button #main.button3, "setStr", [button3Click], UL, 268, 59, 122, 25
    button #main.button4, "getStr", [button4Click], UL, 271, 92, 122, 25
    texteditor #main.log, 24, 128, 497, 220
    textbox #main.name, 164, 19, 100, 25
    textbox #main.val, 290, 19, 100, 25
    graphicbox #main.grBox, 152, 48, 100, 68
    button #main.button10, "setNum", [button10Click], UL, 408, 58, 122, 25
    button #main.button11, "getNum", [button11Click], UL, 410, 93, 122, 25

    menu #main, "Edit"    '<--- Texteditor Menu can be moved but not removed.

    open "Get/Set var by name (not quite EVAL but...)" for window as #main
    print #main, "trapclose [quit.main]"

    print #main, "font ms_sans_serif 10"
    #main.name "a$"
    #main.val "Hello"

    wait

[quit.main]
    Close #main
    END


[button3Click]
    'set var by name - string
    'var$= "a$"      'works for strings and numbers.
    #main.name "!contents? var$"
    'val$ = "Hello"
    #main.val "!contents? val$"

    #main.txtEd "!cls"
    #main.txtEd val$;
    #main.txtEd "!contents? ";var$

    #main.log "a$=";a$
    wait


[button4Click]
    'read var by name - string
    'var$= "a$"
    #main.name "!contents? var$"

    #main.txtEd "!contents ";var$
    #main.txtEd "!contents? b$"
    b$ = trim$(b$)
    #main.log "b$=";b$

    wait


[button10Click]
    'set var by name. About same as string
    'var$= "a"
    'val = 123
    #main.name "!contents? var$"
    #main.val "!contents? val"

    #main.txtEd "!cls"
    #main.txtEd val;
    #main.txtEd "!contents? ";var$

    #main.log "a=";a
    wait


[button11Click]
    'read var by name - number
    'read string first,
    'var$= "a"
    #main.name "!contents? var$"

    #main.txtEd "!cls"
    #main.txtEd "!insert ";var$ 'this thing doees not work in JB 1.01
    #main.txtEd "!contents? b$"
    b$ = trim$(b$)
    'then use it in grBox
    #main.grBox "place 0 ";b$
    #main.grBox "posxy dummy b"
    #main.log "b=";b

    wait
 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
bplus
Senior Member
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 1213
xx Re: Get/set variable by name
« Reply #1 on: Nov 14th, 2017, 2:27pm »

Hi tsh73,

I am having hard time following what you are saying /doing here but this sounds very interesting!
Quote:
'new texteditor command - INSERT - added in JB2.0 allows get number variable value by it's name
'this allows kind of debug console - to ask/change any variable in a program during runtime
' (for numbers, get only integer part)



not to mention this:
Quote:
(not quite EVAL but...)


I did manage to assume by "fiels" and "fileds", you mean "fields" not "files".
I will try again later when my errands run and chores finished and less is distracting me. All those boxes and buttons...
User IP Logged

B+
tsh73
JB-Supporter


member is offline

Avatar




PM

Gender: Male
Posts: 3614
xx Re: Get/set variable by name
« Reply #2 on: Nov 15th, 2017, 4:06pm »

Ok, now distilled to a debug console.
Idea is to include it with another program...
Mayhem.bas sounds as good candidate
(most time it waits for input;
it is written without subs so all variables are visible)
But to make some use it needs some interesting vars
- so probably it should be a bit changed (parametrised) first.
Code:
'   debug console
'   tsh73 nov 2017

    nomainwin
    if val(Version$) <2 then notice "Debug console does not work in JB 1.01 (commands needed)"

    'these two variables are left to experiment with get/set
    'but you can use (or make) any variables. No arrays, sorry
    a$="Hello"
    b=321

    WindowWidth = 325
    WindowHeight = 580

    UpperLeftX=int((DisplayWidth-WindowWidth)/2)
    UpperLeftY=int((DisplayHeight-WindowHeight)/2)

    groupbox #debug.groupbox2, " variable ", 6, 6, 305, 145
    statictext #debug.statictext1, "name", 31, 26, 49, 20
    statictext #debug.statictext3, "value", 31, 76, 58, 21
    textbox #debug.varName, 26, 46, 135, 25
    textbox #debug.varVal, 26, 96, 135, 25
    button #debug.get, "Get value", [debug.get], UL, 171, 46, 122, 25
    button #debug.set, "Set value", [debug.set], UL, 171, 96, 122, 25
    statictext #debug.lblLog, "log", 26, 151, 50, 20
    texteditor #debug.log, 11, 171, 300, 355
    'hidden out-of-sight parts
    graphicbox #debug.gr, -400, 10, 20, 20
    texteditor #debug.txt, -400, 50, 20, 20
    menu #debug, "Edit"    '<--- Texteditor Menu can be moved but not removed.

    open "debug console" for window as #debug
    print #debug.gr,"down; fill white; flush"
    print #debug, "trapclose [quit.debug]"

    print #debug, "font ms_sans_serif 10"
     #debug.log "!autoresize"

    wait

[quit.debug]
    Close #debug
    END


[debug.get]    'Perform action for the button named 'get'
    'is it string or number?
    #debug.varName "!contents? debug.var$"
    debug.var$ = trim$(debug.var$)
    if debug.var$ = "" then notice "Debug console"+chr$(13)+"Error: variable name is empty": wait
    if right$(debug.var$, 1) = "$" then   'string
        #debug.txt "!contents ";debug.var$
        #debug.txt "!contents? debug.val$"
        debug.val$ = mid$(debug.val$,1,len(debug.val$)-2)   'strip CRLF
        #debug.log debug.var$;" is >"; debug.val$;"<"
    else    'number
        'read as string first,
        #debug.txt "!cls"
        #debug.txt "!insert ";debug.var$ 'this thing does not work in JB 1.01
        #debug.txt "!contents? debug.val$"
        debug.val$ = trim$(debug.val$)
        if debug.val$ ="" then #debug.log debug.var$;" is undefined": wait
        'then use it in grBox
        #debug.gr "place 0 ";debug.val$
        #debug.gr "posxy dummy debug.val"
        #debug.log debug.var$;" is ";debug.val
    end if

wait


[debug.set]    'Perform action for the button named 'set'
    'is it string or number?
    #debug.varName "!contents? debug.var$"
    debug.var$ = trim$(debug.var$)
    if debug.var$ = "" then notice "Debug console"+chr$(13)+"Error: variable name is empty": wait
    if right$(debug.var$, 1) = "$" then   'string
        #debug.varVal "!contents? debug.val$"
        #debug.txt "!cls"
        #debug.txt "!insert debug.val$"    'finally solved extra CRLF
        #debug.txt "!selection? ";debug.var$ 
        #debug.log debug.var$;" set to >";debug.val$;"<"
    else
        #debug.varVal "!contents? debug.val"
        #debug.txt "!cls"
        #debug.txt debug.val;
        #debug.txt  "!contents? ";debug.var$ 
        #debug.log debug.var$;" set to ";debug.val
    end if

wait
 
User IP Logged

Q: "And if I took your codes and compile them, and sell them for a profit"?
A: Go ahead. I had my share of good then I coded it for fun, if you can make better use of it - please do.
(enjoying JB 1.01 on WinXP, netbook and desktop)
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls