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. Property that can only be set once...

Property that can only be set once...

Scheduled Pinned Locked Moved C#
questiondiscussion
9 Posts 5 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.
  • E Offline
    E Offline
    Edmundisme
    wrote on last edited by
    #1

    Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!

    P A 2 Replies Last reply
    0
    • E Edmundisme

      Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.

      Deja View - the feeling that you've seen this post before.

      My blog | My articles

      E P 2 Replies Last reply
      0
      • E Edmundisme

        Hello, I have a class with a property that allows the getting and setting of a field. However, once that field has been set, I don't want it to be set again. Is it reasonable to have a get/set property that only allows the property to be set once? Am I trampling on some best practice here or am I completely OK? (In my particular case, it won't work to have the field set by the constructor.) Thanks!

        A Offline
        A Offline
        Anthony Mushrow
        wrote on last edited by
        #3

        I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:

        private string name;

        public void setName(string Name)
        {
        if(name != null)
        name = Name;
        else
        //name is already set.
        }

        You could just do the same thing using a set method (and then being able to just use Y = X;) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.

        My current favourite word is: Bauble!

        -SK Genius

        E P 2 Replies Last reply
        0
        • P Pete OHanlon

          If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.

          Deja View - the feeling that you've seen this post before.

          My blog | My articles

          E Offline
          E Offline
          Edmundisme
          wrote on last edited by
          #4

          Thanks for your thoughts.

          1 Reply Last reply
          0
          • A Anthony Mushrow

            I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:

            private string name;

            public void setName(string Name)
            {
            if(name != null)
            name = Name;
            else
            //name is already set.
            }

            You could just do the same thing using a set method (and then being able to just use Y = X;) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.

            My current favourite word is: Bauble!

            -SK Genius

            E Offline
            E Offline
            Edmundisme
            wrote on last edited by
            #5

            Thanks for your reply. I'll ponder this some more.

            1 Reply Last reply
            0
            • P Pete OHanlon

              If you are making this available for use elsewhere then it's not good practice to have a single set field. Part of the problem you are going to have is that your field can be got to and set via reflection, so any single-set updates can be bypassed.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

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

              Pete O'Hanlon wrote:

              your field can be got to and set via reflection

              Ummm... what? My privates aren't private?

              M P 2 Replies Last reply
              0
              • P PIEBALDconsult

                Pete O'Hanlon wrote:

                your field can be got to and set via reflection

                Ummm... what? My privates aren't private?

                M Offline
                M Offline
                mav northwind
                wrote on last edited by
                #7

                PIEBALDconsult wrote:

                Ummm... what? My privates aren't private?

                Exactly. Using reflection there's not much that can't be done. Accessing otherwise not accessible members via reflection is one of the easier tasks.

                Regards, mav -- Black holes are the places where God divided by 0...

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  Pete O'Hanlon wrote:

                  your field can be got to and set via reflection

                  Ummm... what? My privates aren't private?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  PIEBALDconsult wrote:

                  My privates aren't private

                  Ignoring the innuendo that arises here, the answer is no. Reflection allows you to get right at the heart of your code if you know what you are looking for.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  1 Reply Last reply
                  0
                  • A Anthony Mushrow

                    I was going to suggest using a read-only variable, but then i read about not being able to use the constructor. Instead, try setting up a private (or protected) variable for whatever it is you want (which you already have), then just use a regular method to set it, but check that it is null, or whatever its default value is first, like this:

                    private string name;

                    public void setName(string Name)
                    {
                    if(name != null)
                    name = Name;
                    else
                    //name is already set.
                    }

                    You could just do the same thing using a set method (and then being able to just use Y = X;) but it just doesn't seem to be the type of place to put it. Plus, you could have your regular method return a bool so that you know if you have set the variable or not.

                    My current favourite word is: Bauble!

                    -SK Genius

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    Unfortunately, this design (while being good) doesn't address the fact that the private member could still be set directly. The poster needs to think some more about his design before he tries this.

                    Deja View - the feeling that you've seen this post before.

                    My blog | My articles

                    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