Initializing arrays (FlexBasic 5.9.20)
Mickster
Posts: 2,681
This (shared) works:dim shared as ubyte plc(4) = { &h01,&h02,&h03,&h04 }
Not shared gives an error:dim as ubyte plc(4) = { &h01,&h02,&h03,&h04 }
Not really a problem but just curious.
Comments
All BASIC files can potentially be used as objects with
class using
, just like Spin files. Variables declared as shared are shared among all instances of such a class, so there's no difficulty in initializing them (there's only one copy to initialize). However, variables declared without "shared" are different in each class, and each copy would have to be initialized when the class is created (i.e. at run time). Right now there's no notion of a "constructor" in BASIC, so this isn't possible.In Spin terms, "shared" variables are in the DAT section (which can be initialized) whereas non-shared variables are in the VAR section (which is always initialized to 0 and cannot be initialized to anything else).
You can always write a "manual" constructor for a class - write a method, call it init or something like this and let it initialize the object. Of course this means calling the "constructor" manually before first using the object.