Jump to content

Dynamically creating variables


Quinch
 Share

Recommended Posts

Let's say, not so hypothetically, that I want to open a dozen files, each with its handle, and later in the script dump stuff into them. So, not very theoretically, instead of manually defining each file, I'd try to do this;

For $sortnumber = 1 to 12
    $"sort"&$sortnumber = FileOpen("G:\NR_Lists\Department"&$sortnumber&".txt", 2)
Next

Of course, it doesn't work - the program pops up a message about a badly formatted variable or macro. So my question is, is there a way to make something that basically does "take something and something, and create/refer-to a variable name from it"?

TIA,

Quinch

Link to comment
Share on other sites

Hmm...why not put an opt("ArrayStartNumber", option) in where option can equal 1 or 0? Default can equal 0 so it doesn't bork existing scripts.

Quote ripped from the VBA Excel help file.

Option Base Statement

Used at the module level to declare the default lower bound for array subscripts.

Syntax

Option Base {0 | 1}

Remarks

Because the default base is 0, the Option Base statement is never required. If used, the must appear in a module before any procedures. Option Base can appear only once in a module and must precede array declarations that include dimensions.

Note The To clause in the Dim, Private, Public, ReDim, and Static statements provides a more flexible way to control the range of an array's subscripts. However, if you don't explicitly set the lower bound with a To clause, you can use Option Base to change the default lower bound to 1. The base of an array created with the the ParamArray keyword is zero; Option Base does not affect ParamArray (or the Array function, when qualified with the name of its type library, for example VBA.Array).

The Option Base statement only affects the lower bound of arrays in the module where the statement is located.

Edited by Blue_Drache

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Hmm...why not put an opt("ArrayStartNumber", option) in where option can equal 1 or 0? Default can equal 0 so it doesn't bork existing scripts.

Just... no. First, asking for an Opt() is bad. Second, in this case it's doubly bad. Arrays are 0-based, period. No wishy-washy, "oh, let's let users choose because we developer's are too dumb to make a decision". They are 0-based, like it or not.
Link to comment
Share on other sites

Just... no. First, asking for an Opt() is bad. Second, in this case it's doubly bad. Arrays are 0-based, period. No wishy-washy, "oh, let's let users choose because we developer's are too dumb to make a decision". They are 0-based, like it or not.

LOL. I take it this isn't the first time it's come up then.

Lofting the cyberwinds on teknoleather wings, I am...The Blue Drache

Link to comment
Share on other sites

Thanks, that seems to work {I'll probably revisit this topic later, but let's hope not}. Another question, though, why the need to define the array as one value higher than the number of values inputted into it?

Then...

Dim $arr[12]

For $sortnumber = 1 to 12
    $arr[$sortnumber - 1] = FileOpen("G:\NR_Lists\Department"&$sortnumber&".txt", 2)
Next

or

Dim $arr[12]

For $sortnumber = 0 to 11
    $arr[$sortnumber] = FileOpen("G:\NR_Lists\Department"&$sortnumber+1&".txt", 2)
Next
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...