Can I preload an array via code?

Ok I am at a loss here and have check the docs and can’t find a way to do what I need. I have currently (on paper), arrays of string. These arrays are static and won’t change. I would like to preload them while coding (not real time) so I can reference the arrays when the app runs. Preloading the arrays make it easier for the coder (me). I looked at enumerations but I can’t do something like if enumCodes.indexof( stringCode ) < 0 then msgbox "ERROR" or something like that. or if not enumCodes.hasName( stringCode ) then msgbox "ERROR" . I can do the indexOf on an array but I can’t seem to find a way to have the array pre-built when the app is built.

I have lost my mind trying to figure this one out. it is probably very very simple answer.

thanks for helping me

Have you thought of putting the array in a constant as a json string then parse it on load?

filetype maybe is a workaround? only other thing i can think of is a plugin

I often wish that assigning a value not in the enumeration list of values would raise an exception

ie/

[code]enum Foo
bar
baz
end enum

dim f as Foo
f = Foo( 23 ) // <<<<<<<<< really wish this would raise an exception[/code]

and then an enum would work well for you

But there’s no way to define & assign an array property on a class / module / etc as part of defining it (ie no default value that is an array)

The closest I can think of is stuff it in a module, make it a have a static, and on the first access set the array up & then return a reference to that array

[code]module
function myFoo() as String()
static mArray() as string

     if mArray.ubound < 0 then
          mArray.append 
           .... etc 
    end if
  
    return mArray
end function

end module[/code]

that way you set it up once & reuse the heck out of it

Something like Array?

Dim MyArray() As String = Array("Foo", "Bar", "Foobar")

I use Array and a “debugLoad” module in my projects to preload my class arrays with data. It’s been working nicely since at least 4.5.3 IIRC…

I am having to run a “setup” method to load the array. Wish I could hard code it and not have to build the array at run time. Oh Well.

thanks all.!

or if you need to be conditional… use APPEND

Dim MyArray() As String 
myArray.append "Foo"
myArray.append  "Bar"
myArray.append "Foobar"
if <somecondition> then myArray.append "Foobar2"'

[quote=186991:@Dave S]or if you need to be conditional… use APPEND

Dim MyArray() As String myArray.append "Foo" myArray.append "Bar" myArray.append "Foobar" if <somecondition> then myArray.append "Foobar2"' [/quote]

I don’t but that works for that use case. I have around 200 strings per array and the list is very static.

well stick the list in a constant and assign it with split
that way its 2 lines of code

1 Like