Is it possible to have a static field of a struct type of its type

G

Guest

That didn't come out right, but what I mean is something like:

struct X
{
private int x ;

public readonly X MinValue ;

static X() { MinValue = new X ( 0 ) ; }

public X
(
int x
)
{
this.x = x ;
}
}

The struct has a readonly field to contain the MinValue, and the type of
that field has to be that the struct that's being defined. Ergo I get
"...causes a cycle in the struct layout". I.e. I can't construct the static
value until after the static value is constructed.

So I'm left with making a read-only property for it, which works, but takes
more processing each time one of these "constants" is accessed.

Is there something I'm missing?

Another solution would be to store the minvalue in a private readonly
_object_ and have a property that simply casts it back, that would cut down
the processing.

Any thoughts?
 
J

Jon Skeet [C# MVP]

PIEBALD said:
That didn't come out right, but what I mean is something like:

So I'm left with making a read-only property for it, which works, but takes
more processing each time one of these "constants" is accessed.

Is there something I'm missing?

Yup - and I suspect you'll kick yourself. The problem is that you
haven't declared MinValue to be static. (You clearly know it should be,
given the subject of your post :)

Make MinValue static and it works fine.
 
G

Guest

Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:

struct X
{
private object minvalue = null ;

....

public static X MinValue
{
get
{
if ( minvalue == null )
{
// set minvalue
}

return ( (X) minvalue ) ) ;
}
}

}

Which may be better if a lot of processing goes into calculating the value,
because it won't be calculated if it's never accesssed, and only once if it
is.
 
J

Jon Skeet [C# MVP]

PIEBALD said:
Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:

<snip>

At that stage, I suggest you read
http://www.pobox.com/~skeet/csharp/singleton.html for a few caveats and
suggestions.

(You're not actually implementing a singleton, but the discussions on
laziness and thread safety are likely to be useful.)

Jon
 
J

Jon Skeet [C# MVP]

PIEBALD said:
Indeed kicking self.

On the other hand, to quote myself, "we learn more by making mistakes than
by getting it right the first time".

Had I gotten it to work right off, I would not had come up with:

<snip>

I suggest you have a look at
http://www.pobox.com/~skeet/csharp/singleton.html

Even though you're not implementing a singleton as such, things like
thread safety and laziness are still relevant.
 

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