Jump to content

HELP - Converting a string to $var


 Share

Recommended Posts

Hi all mates,

the program i'm developing uses lots of similar vars:

if GUICtrlRead($enabled_Port1) = "1" then _status(6,"Port1 is Enabled")
Sleep(150)
if GUICtrlRead($enabled_Port2) = "1" then _status(6,"Port2 is Enabled")
Sleep(150)
if GUICtrlRead($enabled_Port3) = "1" then _status(6,"Port3 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port4) = "1" then _status(6,"Port4 is Enabled")
sleep(150)
if GUICtrlRead($enabled_Port5) = "1" then _status(6,"Port5 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port6) = "1" then _status(6,"Port6 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port7) = "1" then _status(6,"Port7 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port8) = "1" then _status(6,"Port8 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port9) = "1" then _status(6,"Port9 is Enabled") 
Sleep(150)
if GUICtrlRead($enabled_Port10) = "1" then _status(6,"Port10 is Enabled")

and

if $s_Port1Enabled = $GUI_CHECKED Then
        if $S_w1r1 = $GUI_CHECKED Then
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c1)
        ElseIf $S_w1r2 = $GUI_CHECKED Then 
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c2)
        ElseIf $S_w1r3 = $GUI_CHECKED Then
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c3)
        ElseIf $S_w1r4 = $GUI_CHECKED Then
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c4)
        ElseIf $S_w1r5 = $GUI_CHECKED Then
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c5)
        ElseIf $S_w1r6 = $GUI_CHECKED Then
            _status(6,"Port 1 Enabled and Message sent: " & $S_w1c6)
        Else        
        EndIf
    Else ...

is it possible to use a FOR cycle, similar to

for $i = 1 to 10
if guictrlread(eval($enabled_Port1) & $i) = "1" then _status(6,"....")
next

?

This doesn't solve me the problem so obviously something is missing...

Is there a way to convert a string

(i.e. ENABLED_PORT + $i) to a var $ENABLED_PORT1 ... $ENABLED_PORT10 ?

Thanks everyone, mates,

Marco

Link to comment
Share on other sites

@Zedna: yeah, my fault, now it works.

This solve some of my problems, but not all of them...

in the Case i call this function:

Case $PortCallButton
    _browseExe(GUICtrlRead($Instance))

func _browseExe($i)
    Select
    case $i = "1"
        $s_EXEFILE_1 = FileOpenDialog("Executable Filename ",@DesktopDir,"Exe (*.exe)")
        GUICtrlSetData($EXEFILE_1, $s_EXEFILE_1)
    case $i = "2"
        $s_EXEFILE_2 = FileOpenDialog("Executable Filename ",@DesktopDir,"Exe (*.exe)")
        GUICtrlSetData($EXEFILE_2, $s_EXEFILE_2)
...
...
    case $i = "9"
        $s_EXEFILE_9 = FileOpenDialog("Executable Filename ",@DesktopDir,"Exe (*.exe)")
        GUICtrlSetData($EXEFILE_9, $s_EXEFILE_9)
    case $i = "10"
        $s_EXEFILE_10 = FileOpenDialog("Executable Filename ",@DesktopDir,"Exe (*.exe)")
        GUICtrlSetData($EXEFILE_10, $s_EXEFILE_10)
    EndSelect
EndFunc

i tried to subst with:

func _browseExe($i)
eval("s_exefile_" & $i) = FileOpenDialog("Executable Filename ",@DesktopDir,"Exe (*.exe)")
GUICtrlSetData(("exefile_" & $i), ("s_exefile_" & $i))
endfunc

But it returns me error.

Is there a way to simplify the code avoiding all that cases and doing something like you told me before?

Thanks again,

Marco

Link to comment
Share on other sites

I'm a little bored.

And, I'm a little confused at the liberal use of the "eval" statement in this thread.

If, for instance, you have a ton of checkbox controls defined for a range of ports, you could to do something like:

Global $Checkbox_Port[64], $Port[64]

GuiCreate("My Pgm", 600, 400)

$left = 20
$top = 0
For $i = 0 to 63; Create an array of indexed checkboxes
    $top += 20
    If $top > 320 Then
        $top = 20
        $left += 80
    EndIf
    $Checkbox_Port[$i] = GUICtrlCreateCheckbox("Port " & $i, $left, $top)
Next

GUISetState()

While 1
WEnd

;-------------------------------------------------------------------------
Func Scan_Checkboxes()
    For $i = 0 to 63; Scan all checkboxes
        $Port[$i] = GUICtrlRead($CheckBox_Port[$i])
    Next
EndFunc

Func Process_Checkboxes()
    For $i = 0 to 63; Process all checkboxes
        If $Port[$i] Then 
            Enable_Port($i)
        Else
            Disable_Port($i)
        EndIf
    Next
EndFunc

Func Enable_Port($i)
; Do the voodoo, that you do
EndFunc

Func Disable_Port($i)
; Undo the voodoo, that you dood
EndFunc

I'm willing to bet that commands like "eval", "assign" and "execute" are entirely resolved at run-time and should be avoided if you have a choice. Using arrays and subscripts is the standard practice, rather than creating a mass of unique variable names, then chopping those variable names up as strings and perfoming a slew of commands that aren't easily preprocessed/interpreted/compiled.

Edited by Spiff59
Link to comment
Share on other sites

@Spiff:

thanks mate for your hint. That works fine to create/reading datas. But how to write to a setting file the values stored (i.e.) in the checkboxes to load it at beginning with a _loadsettings?

I suppose i need to declare anyway the settings.ini vars (usually $s_...) and Iniwrite / Iniread them? Correct??

Edited by marko001
Link to comment
Share on other sites

Well it seems to me that you've already answered your own question.... :) Why don't you have a go first?

Also, please don't bump your posts in a 24 hour period from your last post...

Cheers,

Brett

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...