0

I have a function called fillUI() whose main purpose is to fill the UI that has been created. In my original script I need to go over more than 20 Edit Controls and fill them with content. The content is stored inside an array.

The problem that I am facing is that I am unaware of how "return" a global variable with content, whose variable name is made up from a string.

It's like saying myVar1 := "Hello World" is the original variable. Then I point to that variable with varName := "myVar1" and then change it with varName := "foobar" but myVar1 will still be Hello World in the end..

Is such a thing possible?

; Start of script
#SingleInstance Force

FillUI()

Gui, Add, Edit, x12 y9 w350 h30 , %Text_1%
Gui, Add, Edit, x12 y89 w350 h30 , %Text_2%
Gui, Add, Edit, x12 y159 w350 h30 , %Text_3%

Gui, Show, w390 h278, Code testing
return

FillUI() {
    words := {1:"Hello World",2:"foobar",3:"lorem ipsum"}

    i := 1
    while(i <= words.MaxIndex()) {
        global variable_name := "Text_" i
        word := words[i]

        ; I need to return a variable here whose variable name needs to be something like "Text_X" and whose content is %word%
        ; This is in order to fill the UI above
        ;
        ; Is this achievable?
        variable_name := word
        i++
    }
}

GuiClose:
    ExitApp
return

; End of script

1 Answer 1

1

This is one approach. The global at the start of the script declares all variables that are used in that script as global, hence the variables that are used only inside the FillUI()function are declared using local

; Start of script
#SingleInstance Force

FillUI()

Gui, Add, Edit, x12 y9 w350 h30 , %Text_1%
Gui, Add, Edit, x12 y89 w350 h30 , %Text_2%
Gui, Add, Edit, x12 y159 w350 h30 , %Text_3%

Gui, Show, w390 h278, Code testing
return

FillUI() {
    global
    local words := {1:"Hello World",2:"foobar",3:"lorem ipsum"}

    local i := 1
    while(i <= words.MaxIndex()) {


        word := words[i]
     ;I need to return a variable here whose variable name needs to be something like "Text_X" and whose content is %word%
        ; This is in order to fill the UI above
        ;
        ; Is this achievable?
        Text_%i% := word

        i++
    }
}

GuiClose:
    ExitApp
return
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.