initialising properties in static constructor

M

Mike P

I have a class with a dozen+ properties, some of which will be set a
value, and some not, depending on the constructor called. I also have a
method which has only one overload and all of the properties are being
sent to it.

My question is, because some of these properties will not be initialised
by the constructor used, calling this method in some cases will crash my
app unless I initialise all the properties at some point in my class.
Is the static constructor the best place to do this?


Regards,

Mike
 
J

Jon Skeet [C# MVP]

Mike P said:
I have a class with a dozen+ properties, some of which will be set a
value, and some not, depending on the constructor called. I also have a
method which has only one overload and all of the properties are being
sent to it.

My question is, because some of these properties will not be initialised
by the constructor used, calling this method in some cases will crash my
app unless I initialise all the properties at some point in my class.
Is the static constructor the best place to do this?

No, a static constructor can't do this. A static constructor has no
instance to deal with.

I'm not sure what you want to happen when the method is called when the
properties haven't been set. Do you want to use some default values (in
which case initialising the properties to the default values at
construction time would be the right way to go) or do you want an
exception to be thrown (in which case, check the current state and
throw InvalidOperationException if the relevant properties haven't all
been set).
 
M

Mike P

I want to use default values...I just wondered whether I should be
setting them in the static constructor or my other constructors. I
guess the answer is to set the defaults in the other constructors.


Thanks,

Mike
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
My question is, because some of these properties will not be initialised
by the constructor used, calling this method in some cases will crash my
app unless I initialise all the properties at some point in my class.
Is the static constructor the best place to do this?

Nop, unless those properties are static too, meaning that they belong to the
type and not to a particular instance. if this is not the case you should
initialize them inside the constructor ( if they have some default values )
or otherwise throw an exception if they are used without initialization.

Cheers,
 
J

Jon Skeet [C# MVP]

Mike P said:
I want to use default values...I just wondered whether I should be
setting them in the static constructor or my other constructors.

Static constructors are used to initialise static variables.
I guess the answer is to set the defaults in the other constructors.

Or in the variable declarations.
 
G

Guest

Mike,

If the properties are based on simple types, you can just initialise the
corresponding fields inline, eg:

private int _aNumber = 12;
private string _someString = "default text";

Cheers,
Chris.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I would suggest you that take a look at Jon's article about constructors to
know the use and the particularity of a static constructor.

you could also do this

private int i=4;

it does initialize it and you don;t have to put that code in a constructor.


cheers,
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Mike,

When static constructor is static it cannot initialize instance variables
simply because it has no relations to any instances. Thus you cannot use it
for this purpose unless the fields are not static as well.

You have options:

1. To intialize them in the constructor. If you have more then one
constructor, though, it might be hard to maintain such a code. In this case
initialize them at the moment of their decalration

class Foo
{
Bar b = new Bar()

public Foo(...)
{
}
}

The code for such initialization will be populated by the compiler at the
begining of all constructrs your calass may have. The drawback that some of
the variables will be initialized twise, which depending on the type of the
obejct might be no acceptable. The other limitation is that you cannot use
*this* which in the constructor is allowed.

2. You can initialize the backup fileds of the properties on demand. The
first time the propety is read if the value is not initialized then you
initialize it. This however works for reference types, but not for value
types unless you don't keep some flags and stuff.

public Foo MyProp
{
get
{
if(this.myProp == null)
{
this.myProp = new Foo();
}
return myProp;
}
}

3. you can throw an exception of the roperty is not initialized. Again it
worgs well for reference types, but not for value types unless you don have
special flags
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top