Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. What is my interim pattern for readonly structs

What is my interim pattern for readonly structs

Scheduled Pinned Locked Moved C#
questiondesignregexarchitectureannouncement
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JoeRip
    wrote on last edited by
    #1

    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?

    P 1 Reply Last reply
    0
    • J JoeRip

      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?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Use properties with public getters and internal setters. Also add a private boolean to indicate locked.

      J 2 Replies Last reply
      0
      • P PIEBALDconsult

        Use properties with public getters and internal setters. Also add a private boolean to indicate locked.

        J Offline
        J Offline
        JoeRip
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • P PIEBALDconsult

          Use properties with public getters and internal setters. Also add a private boolean to indicate locked.

          J Offline
          J Offline
          JoeRip
          wrote on last edited by
          #4

          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!

          P 1 Reply Last reply
          0
          • J JoeRip

            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!

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Right, that should do it.

            J 1 Reply Last reply
            0
            • P PIEBALDconsult

              Right, that should do it.

              J Offline
              J Offline
              JoeRip
              wrote on last edited by
              #6

              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".

              N 1 Reply Last reply
              0
              • J JoeRip

                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".

                N Offline
                N Offline
                N a v a n e e t h
                wrote on last edited by
                #7

                Because it is a struct, a value type not a reference type. When you write someObject.SomeStruct, you are getting a new copy of the somestruct 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

                J 1 Reply Last reply
                0
                • N N a v a n e e t h

                  Because it is a struct, a value type not a reference type. When you write someObject.SomeStruct, you are getting a new copy of the somestruct 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

                  J Offline
                  J Offline
                  JoeRip
                  wrote on last edited by
                  #8

                  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.

                  N 1 Reply Last reply
                  0
                  • J JoeRip

                    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.

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups