Jump to content

Trouble With Inireadsection And Scope


Recommended Posts

I'm sure there is nothing broken with the IniReadSection function that I'm using, I just need a little advice about how to use it and keep my code looking nice.

I wanted to keep my main $msg loop clean, so instead of cramming all my code into each case statement, I created a function for all my code, and just put the function call in the case statement. Nothing unusual there, right?

While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $BtnAdd; add entry to listview
            AddEntry()
        Case $msg = $BtnRemove; remove entry from listview
            RemoveEntry()
        Case $msg = $BtnLoad; load plugin's .inf file and parse it
            LoadInf()
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $BtnExit; exit application
            Exit
        Case Else
        ;;;;;;;
    EndSelect
WEnd

Well, when I moved a chunk of code from the main loop to a function, I apparently started to have a scope problem.

One of the things that I put into a function (subroutine, really) was the initial use of the IniReadSection function. IniReadSection returns a 2 dimensional array, and it returns it to a variable that is created inside my function. I want that array to be global, so I figure I should define it as global at the top of my code.

If I try to define it either with or without dimensions, some other functions complain with: Subscript used with non-Array variable. Apparently, the Array, even though I defined it as Global at the top of the code, is getting destroyed when exiting my function. I actually have 3 arrays that use IniReadSection, and they are all destroyed. How can I pass the arrays back to the main level without losing the data stored in them?

Should I just cram all my code in the case statements? It works, but it is ugly.

Should I define the array(s) as global in the main level of the code? If so, how do I define the dimensions if I don't know what they will be?

Should I use ByRef in all my functions to pass the arrays every time? <- this would be uglier than just putting the code in the case statements, and cause me to use even more variables that I would just waste anyway.

Is there some other technique that I have overlooked? I'm not experienced at writing code, but I can mostly muddle thru.

I appreciate your suggestions.

BTW: I'm using v3.1.1.113 (beta)

Code above is for illustrative purposes only.

Link to comment
Share on other sites

Hi,

it is easier to help, if you post the complete code.

So long,

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

One of the things that I put into a function (subroutine, really) was the initial use of the IniReadSection function. IniReadSection returns a 2 dimensional array, and it returns it to a variable that is created inside my function. I want that array to be global, so I figure I should define it as global at the top of my code.

If I try to define it either with or without dimensions, some other functions complain with: Subscript used with non-Array variable. Apparently, the Array, even though I defined it as Global at the top of the code, is getting destroyed when exiting my function. I actually have 3 arrays that use IniReadSection, and they are all destroyed. How can I pass the arrays back to the main level without losing the data stored in them?

You sort of assigne a new array to your glkobal array when you use IniReadSection

Is there some other technique that I have overlooked? I'm not experienced at writing code, but I can mostly muddle thru.

So don't bother to much about how it looks. Make it work first..:think:) But your on the track. Use descriptive functions. Use arguments to transfer variables from caller to function (rather than Globals).

And do as @th.meger said. Your sample code did not match the problem you described (tal least not as I read it)

Link to comment
Share on other sites

Should I use ByRef in all my functions to pass the arrays every time? <- this would be uglier than just putting the code in the case statements, and cause me to use even more variables that I would just waste anyway.

For the details that you give, the solution would be seemingly to use byref keyword. However, you should paste the code here, mainly keeping in mind the problem of the scopes.

Link to comment
Share on other sites

For the details that you give, the solution would be seemingly to use byref keyword. However, you should paste the code here, mainly keeping in mind the problem of the scopes.

Thank you for the advice... I did use ByRef which keeps my code nicely organized. After doing that, I still got the error, but I found out *WHY* I got the error... It was just my own stupid fault.

Sometimes, just asking someone with more programming experience is just what it takes to solve a problem.

Thanks again.

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...