What is my interim pattern for readonly structs
-
I have a library class which does a bunch of work and eventually returns to the caller a struct (call it
UserInfo
). That struct contains other structs. All of the members of all of the structs are public readonly, as I don't want the caller to able to change any of the values. However, I want all of the values visible. So I'm limited to using constructors to build all the nested structs. However, this isn't practical in the flow of my class - I need to be able to create a new UserInfo and update the members of its nested structs as it bounces around inside my class. I really only want to "seal" the struct right before I pass it to the caller. What's the best design pattern for this? Should I create a mirrored "TempUserInfo" struct with all public members and fill that out instead, and at the last minute copy those values into the final UserInfo struct? -
I have a library class which does a bunch of work and eventually returns to the caller a struct (call it
UserInfo
). That struct contains other structs. All of the members of all of the structs are public readonly, as I don't want the caller to able to change any of the values. However, I want all of the values visible. So I'm limited to using constructors to build all the nested structs. However, this isn't practical in the flow of my class - I need to be able to create a new UserInfo and update the members of its nested structs as it bounces around inside my class. I really only want to "seal" the struct right before I pass it to the caller. What's the best design pattern for this? Should I create a mirrored "TempUserInfo" struct with all public members and fill that out instead, and at the last minute copy those values into the final UserInfo struct?Use properties with public getters and internal setters. Also add a private boolean to indicate locked.
-
Use properties with public getters and internal setters. Also add a private boolean to indicate locked.
I'm still not following. I have fields who's value I want the caller to be able to see. I need to change them internally, before I "finalize" the values, but I don't want the caller to be able to change them. Are you suggesting that I implement properties, and in the "Set" property, I check my boolean to see if the struct has been "finished"? If so, what access properties do I put on this boolean, so that I can change it to "locked" but the caller cannot change it to "unlocked"? I'm assuming that I'm misunderstanding your suggestion.
-
Use properties with public getters and internal setters. Also add a private boolean to indicate locked.
Thanks, I believe you lead me down the right path. I've implemented all the struct fields as auto-properties:
public DateTime TimeStamp { get; internal set; }
The internal setter allows my class to change fields at will, but prevents the caller from doing so. Bravo!
-
Thanks, I believe you lead me down the right path. I've implemented all the struct fields as auto-properties:
public DateTime TimeStamp { get; internal set; }
The internal setter allows my class to change fields at will, but prevents the caller from doing so. Bravo!
Right, that should do it.
-
Right, that should do it.
Doh. Apparently you can't set properties in nested structs... the following is not allowed:
my Class
{
myStructInner
{
public int myInt { get; set; }
}myStructOuter
{
public myStructInner structProp { get; set; }
}myMethod()
{
myStructOuter thingy = new myStructOuter();
myStructOuter.myStructInner.myInt = 5;
}
}Compiler claims that you cannot "change the return value from myStructOuter.myStructInner.myInt", as it's not a "variable".
-
Doh. Apparently you can't set properties in nested structs... the following is not allowed:
my Class
{
myStructInner
{
public int myInt { get; set; }
}myStructOuter
{
public myStructInner structProp { get; set; }
}myMethod()
{
myStructOuter thingy = new myStructOuter();
myStructOuter.myStructInner.myInt = 5;
}
}Compiler claims that you cannot "change the return value from myStructOuter.myStructInner.myInt", as it's not a "variable".
Because it is a struct, a value type not a reference type. When you write
someObject.SomeStruct
, you are getting a new copy of thesomestruct
instance and modifying something on that copy won't reflect on the original one. To avoid this issue, you should create a new struct instance and assign to the property.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Because it is a struct, a value type not a reference type. When you write
someObject.SomeStruct
, you are getting a new copy of thesomestruct
instance and modifying something on that copy won't reflect on the original one. To avoid this issue, you should create a new struct instance and assign to the property.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
yeah, that kind of defeats my purpose. I need more resolution than that. I need to be able to hand the top level struct around and let any code in my class modify any individual field therein, no matter how nested. So I'm left with either (a) flattening out the entire struct, which I am loathe to do or (b) writing Set() methods at the top level which are knowledgeable about all of the fields of the nested structs. I'm leaning towards (b). But I hate it.
-
yeah, that kind of defeats my purpose. I need more resolution than that. I need to be able to hand the top level struct around and let any code in my class modify any individual field therein, no matter how nested. So I'm left with either (a) flattening out the entire struct, which I am loathe to do or (b) writing Set() methods at the top level which are knowledgeable about all of the fields of the nested structs. I'm leaning towards (b). But I hate it.
Are there any specific reasons for a choosing struct ? It's hard to make it right. Use classes instead.
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions