Properties in structures
-
Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg.
struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } }
This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005 -
Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg.
struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } }
This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005 -
Hi Is it possible to set values to a property of a structure in the structure's constructor. For eg.
struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { Propertyx = i; } }
This is not compiling whats wrong and why ? But i am able to directly assign the value to the field. Regards Deepak -- modified at 12:26 Tuesday 11th October, 2005It looks like, from reading the specification, you're not allowed to utilize any instance members of the type until after it's been fully initialized (viz. all of its consituent fields have been initialized). And since Propertyx is an instance member and fieldx hasn't been set yet, you can't use Propertyx. I could be wrong through. Try setting fieldx and then using Propertyx...see what that gives you. :)
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson
-
Try this:
struct MyStruct { int fieldx; public int Propertyx { get { return fieldx; } set { fieldx = value; } } public MyStruct(int i) { fieldx = i; } }
He did say that he was able to assign directly to the field. He just wanted to know why he couldn't assign to the property. ;)
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson
-
He did say that he was able to assign directly to the field. He just wanted to know why he couldn't assign to the property. ;)
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson
Ahhh.. I read right over that... Well, you can assign the property as long as you assign the field also. When building a struct constructor, the constructor must not exit until all fields have been assigned. Of course, you're thinking to yourself, "It does get assigned when the property is assigned"... unfortunately the compiler doesn't see that :(
-
Right. And that's how I answered him. ;) I can see the logic behind the compiler not looking through the property to see that the field gets initialized. For instance, what if your property setter utilized another field in the struct that hadn't been initialized yet? If you just make it a hard and fast rule of structs that they must have all their fields initialized before you use any of them, then it simplifies the work that the compiler has to do. There's always reference types. ;)
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson
-
Ahhh.. I read right over that... Well, you can assign the property as long as you assign the field also. When building a struct constructor, the constructor must not exit until all fields have been assigned. Of course, you're thinking to yourself, "It does get assigned when the property is assigned"... unfortunately the compiler doesn't see that :(
Right. And that's how I answered him. ;) I can see the logic behind the compiler not looking through the property to see that the field gets initialized. For instance, what if your property setter utilized another field in the struct that hadn't been initialized yet? If you just make it a hard and fast rule of structs that they must have all their fields initialized before you use any of them, then it simplifies the work that the compiler has to do. There's always reference types. ;)
Picture a huge catholic cathedral. In it there's many people, including a gregorian monk choir. You know, those who sing beautifully. Then they start singing, in latin, as they always do: "Ad hominem..." -Jörgen Sigvardsson