How can I initalize a static array member from a base class
I have been writting these SQL database query objects and decided to try and make a single base class for them all because they are 95% identical.
The various methods use an array of structs that have a static initalization (there may be 100s of instances, but they table is the same for each kind of SQL query class).
The way it worked before I made a base class was like:
private static MySqlTable[] Records =
{
new SqlElement(blah, blah, blah),
new SqlElement(blah, blah, blah),
...
}
then in a method it does
foreach (SqlElement E in Records)
{
}
I want to make all this into a base class and then in the subclass I want to inherit from SqlQueryBase which contains all the code to drive it (with some functional exceptions here and there that do not really matter to this question).
In the base class, SqlQueryBase I now declare:
protected static MySqlTable[] Records = null;
The question is, how do I initalize this in my sub-class?
I can't simply say:
protected static MySqlTable[] Records =
{
new SqlElement(blah, blah, blah),
new SqlElement(blah, blah, blah),
...
}
nor can I create a static constuctor and then say
static SubClass()
{
Records =
{
new SqlElement(blah, blah, blah),
new SqlElement(blah, blah, blah),
...
}
}
So how am I supposed to initalize the Records Array from the sub-class?
[1548 byte] By [
DeepT] at [2007-11-20 10:38:58]

# 1 Re: How can I initalize a static array member from a base class
I would have thought you could do this:
static SubClass()
{
Records = new MySqlTable[3] {
new SqlElement(blah, blah, blah),
new SqlElement(blah, blah, blah),
new SqlElement(blah, blah, blah) };
}
# 2 Re: How can I initalize a static array member from a base class
why are you using static?
cjard at 2007-11-9 11:36:44 >

# 3 Re: How can I initalize a static array member from a base class
Well, it's static... so why the hell would you be instantiating it in a child class? Assuming what you want is *not* for the array to be different for every type of child class (which is seemingly what you do want), then just instantiate it once in the base class with a static method, like this:
class BaseClass
{
static MyArray[] = GenerateArray();
static MyArray[] GenerateArray()
{
MyArray[] obs;
DoAllYourComplexLogic();
obs = AndMoreLogid();
return obs;
}
}
As this is *static* data, you do *not* want to be instantiating it from an instance method without a very good reason. And from the looks of it, the method above would be ideal for what you want.
# 4 Re: How can I initalize a static array member from a base class
protected members are accessible to subclasses only (if that's what you're looking for), for reading, writing, etc. However, if the member needs to be static:
1. I'd lazy load it to avoid accidental reloads
2. Be careful on writing to it, as it has broader implications
# 5 Re: How can I initalize a static array member from a base class
Why would he lazy reload it to 'avoid accidental reloads'. If he instantiates it once at construction time (which is what my method does) why would he need to instantiate it again?
If he needs to instantiate it again, does he really want it to be static?
# 6 Re: How can I initalize a static array member from a base class
I have been writting these SQL database query objects
The other thing I was going to say is, if youre on .NET 2, you can get the IDE to write DAL code for you. I've never written a single connection string, added a single parameter, or any line of databinding since switching to .NET 2's data access facilities
cjard at 2007-11-9 11:40:48 >

# 7 Re: How can I initalize a static array member from a base class
Well, it's static... so why the hell would you be instantiating it in a child class?
Good point!
# 8 Re: How can I initalize a static array member from a base class
Why would he lazy reload it to 'avoid accidental reloads'.You could prevent accidental reloads in many ways and lazy load is just one of them due to the pre-loaded check you have perform, not that reloads would hurt anything except eat up some cpu cycles, depending on how it's writen.
If he instantiates it once at construction time (which is what my method does) why would he need to instantiate it again?He wouldn't need to reload it for the subclass you've supplied if it's called in the constructor, but that would apply to other subclasses as well causing reload attempts, which may also be prevented with a little check and that would be an alternate to the lazy load.
If he needs to instantiate it again, does he really want it to be static? That's a far more involved question depending on the scenario.
# 9 Re: How can I initalize a static array member from a base class
Well, for the situation described, static initialisation is the way to go, be it from a constructor or static method. A lazy load is easily doable, i'm just questioning the need for it in this scenario. The only person who'd know the answer is the OP though.
# 10 Re: How can I initalize a static array member from a base class
I only want to initalize it once for each child class. The data is unique to each child class, but identical for every instance of each child class.
What I eventually did was make an abstract property in the base class that was of the type of array I wanted:
Base Class:
protected abstract SingleSqlRecord[] SqlQueryTable
{
get;
}
and in the SubClass:
protected override SingleSqlRecord[] SqlQueryTable
{
get
{
return SubClassSqlRec;
}
}
DeepT at 2007-11-9 11:44:44 >

# 11 Re: How can I initalize a static array member from a base class
I only want to initalize it once for each child class. The data is unique to each child class, but identical for every instance of each child class.
Given this, Mutant_Fruit's first solution seems like the more efficient approach, which, to reiterate, is to supply the SqlQueryTable property value from the base class and hydrate it from within the default constructor of the base class. . .