variable geht verloren

  • Hi,
    habe folgendes Problem:
    eine variable geht beim aufruf einer anderen funktion verloren, hier der code:

    Spoiler anzeigen
    [autoit]


    #include <guiconstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $x = 10
    $y = 10
    $GUI = GUICreate ("Teams eingeben",200, 70)
    $lbl = GUICtrlCreateLabel ("Teams getrennt durch Kommas eingeben!", 5, 5, 200, 15)
    $OK = GUICtrlCreateButton ("OK!", 80, 48, 40, 20)
    $teams = GUICtrlCreateInput ("Team1,Team2", 10, 25, 180, 20)
    GUISetState (@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $guimsg = GUIGetMsg()
    Switch $guimsg
    Case $OK
    GUISetState (@SW_HIDE)
    _getvalues()
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _getvalues()
    $input = GUICtrlRead ($teams)
    $team = StringSplit ($input, ",")
    MsgBox (0,0,$team[0])
    _calc()
    EndFunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _calc()
    $ergebnis_Gui = GUICreate ("Aufstellungen", 120, 200)
    $exit = GUICtrlCreateButton ("OK!", 40, 170, 40, 25)
    For $i = 2 To $Team[0]
    GUICtrlCreateLabel ($team[1] & " - " & $team[$i], 10, $y)
    $y = $y + 12
    Next
    For $i = 3 To 5
    GUICtrlCreateLabel ($team[2] & " - " & $team[$i], 10, $y)
    $y = $y + 12
    Next
    For $i = 4 To 5
    GUICtrlCreateLabel ($team[3] & " - " & $team[$i], 10, $y)
    $y = $y + 12
    Next
    For $i = 5 To 5
    GUICtrlCreateLabel ($team[4] & " - " & $team[$i], 10, $y)
    $y = $y + 12
    Next
    GUISetState (@SW_SHOW, $ergebnis_GUI)
    Sleep (10000)
    Exit
    EndFunc

    [/autoit]

    ich krieg folgenden error:

    Spoiler anzeigen
    Code
    C:\Dokumente und Einstellungen\XxxxXxxxX\Eigene Dateien\autoit\Ultima\xvsx.au3 (34) : ==> Variable used without being declared.:
    For $i = 2 To $Team[0]
    For $i = 2 To ^ ERROR

    hat jemand nen tip?

    Einmal editiert, zuletzt von unaimed (20. Mai 2009 um 21:28)

  • Also, versetz dich mal in die Lage des Programms:
    Woher soll er die Variable nehmen? Achte auf groß- und kleinschreibung ;) Dann würde ich am Anfang des Skript Team als Global deklarieren.

    • Offizieller Beitrag

    Groß-/Kleinschreibung bei Variablen ist unter AutoIt egal.
    Das Problem ist die Deklaration von $team. Diese findet hier innerhalb einer Funktion statt und ist damit eine lokale Variable.
    $team muss also am Anfang als Global deklariert werden (wie MatthiasG ja auch schon geschrieben hat).

  • Die Funktion _calc() könnte man auch bedeutend eleganter schreiben, zumal dann auch automatisch auf die eingegebene Anzahl der Teams reagiert wird:

    Spoiler anzeigen
    [autoit]

    #include <guiconstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    $x = 10
    $y = 10
    $GUI = GUICreate("Teams eingeben", 200, 70)
    $lbl = GUICtrlCreateLabel("Teams getrennt durch Kommas eingeben!", 5, 5, 200, 15)
    $OK = GUICtrlCreateButton("OK!", 80, 48, 40, 20)
    $teams = GUICtrlCreateInput("Team1,Team2", 10, 25, 180, 20)
    Global $team
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $guimsg = GUIGetMsg()
    Switch $guimsg
    Case $OK
    GUISetState(@SW_HIDE)
    _getvalues()
    Case $GUI_EVENT_CLOSE
    Exit
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _getvalues()
    $input = GUICtrlRead($teams)
    $team = StringSplit($input, ",")
    MsgBox(0, 0, $team[0])
    _calc()
    EndFunc ;==>_getvalues

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func _calc()
    $ergebnis_Gui = GUICreate("Aufstellungen", 120, 200)
    $exit = GUICtrlCreateButton("OK!", 40, 170, 40, 25)
    For $i = 1 To $team[0] - 1
    For $j = $i+1 To $team[0]
    GUICtrlCreateLabel($team[$i] & " - " & $team[$j], 10, $y)
    $y = $y + 12
    Next
    Next

    GUISetState(@SW_SHOW, $ergebnis_Gui)
    Sleep(10000)
    Exit
    EndFunc ;==>_calc

    [/autoit]

    Die Message-Verarbeitung hab ich mal so gelassen, da musst du auch noch was machen. :)

    Gruß Ashpool

    Gruß Ashpool

    Zitat von mir

    Bin immer wieder erstaunt, wie begrenzt ein Horizont sein kann.