473,228 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,228 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 1376
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: William Payne | last post by:
Hello, consider the following two classes (parent and child): #ifndef SINGLETON_HPP #define SINGLETON_HPP #include <cstddef> /* NULL */ template <typename T> class Singleton {
11
by: Rob Williscroft | last post by:
I have the following code , part of a trait to detect wether a type has a member operator "Type" (), it works fine with g++ 3.4 (msvc compiles it but it doesn't work for template'd operator T ())...
13
by: Eric | last post by:
I have a template class that instantiates a variable of the template parameter type as follows: template <class T> struct TemplateClass { T t; }; struct TestClass
13
by: Alex Vinokur | last post by:
I have the following problem. class Base { public: void (*pfunc) (int) = 0; }; class Derived : public Base {
3
by: Angel | last post by:
Hello again (and again, and again...) I think I'm getting closer to solving my initial problem of calling unmanaged code. I managed to call the functions with user-defined structs w/o getting any...
8
by: Tomás | last post by:
Given the following: static_cast<T>( expr ) This will evaluate to an l-value if T is a reference type -- otherwise it will evaluate to an r-value. The same goes for reinterpret_cast. ...
14
by: Dave Booker | last post by:
It looks like the language is trying to prevent me from doing this sort of thing. Nevertheless, the following compiles, and I'd like to know why it doesn't work the way it should: public class...
15
by: archana | last post by:
Hi all, can anyone tell me differene between public static and private static method. how they are allocated and access?. thanks in advance.
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.