471,317 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,317 software developers and data experts.

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

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?

Jan 5 '06 #1
4 1336
PIEBALD <PI*****@discussions.microsoft.com> wrote:
That didn't come out right, but what I mean is something like:
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 5 '06 #2
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.
Jan 6 '06 #3
PIEBALD <PI*****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 6 '06 #4
PIEBALD wrote:
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

Jan 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Rob Williscroft | last post: by
13 posts views Thread by Eric | last post: by
13 posts views Thread by Alex Vinokur | last post: by
8 posts views Thread by Tomás | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.