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. Other Discussions
  3. The Weird and The Wonderful
  4. Read-only properties

Read-only properties

Scheduled Pinned Locked Moved The Weird and The Wonderful
question
42 Posts 15 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.
  • B BadKarma

    Still, its better then the following

    private int m_iData;
    public int Data
    {
     get 
     {
      return m_iData;
     }
     set
     {
       // store the old data
       //
       int iOldData = m_iData;
    
       m_iData = value;
    
       // reset to old data because its read-only
       //
       m_iData = iOldData
     }
    }
    

    codito ergo sum

    CPalliniC Offline
    CPalliniC Offline
    CPallini
    wrote on last edited by
    #9

    :laugh:

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

    In testa che avete, signor di Ceprano?

    1 Reply Last reply
    0
    • P PIEBALDconsult

      Yes, but silently ignoring the value is poor style.

      P Offline
      P Offline
      Paul Conrad
      wrote on last edited by
      #10

      PIEBALDconsult wrote:

      silently ignoring the value is poor style.

      Yes, it is. I can only imagine the headache of trying to track it down on a Friday afternoon.

      "The clue train passed his station without stopping." - John Simmons / outlaw programmer

      D 1 Reply Last reply
      0
      • M mav northwind

        You guys are so negative! Try to see the good in this approach. Using this approach one could perform consistency checks before not saving the value... :laugh:

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

        G Offline
        G Offline
        GibbleCH
        wrote on last edited by
        #11

        Alternately, if at some point in time, the property was to become (or maybe was in the past) not 'readonly', applications relying on the dll wouldn't necessarily need to be recompiled to use the new dll, since the definition wouldn't change...on that item.

        L 1 Reply Last reply
        0
        • B BadKarma

          Still, its better then the following

          private int m_iData;
          public int Data
          {
           get 
           {
            return m_iData;
           }
           set
           {
             // store the old data
             //
             int iOldData = m_iData;
          
             m_iData = value;
          
             // reset to old data because its read-only
             //
             m_iData = iOldData
           }
          }
          

          codito ergo sum

          K Offline
          K Offline
          Krirk
          wrote on last edited by
          #12

          Excellent code.A guy who wrote this code is genius. :omg: :wtf:X|

          My blog

          1 Reply Last reply
          0
          • G GibbleCH

            Alternately, if at some point in time, the property was to become (or maybe was in the past) not 'readonly', applications relying on the dll wouldn't necessarily need to be recompiled to use the new dll, since the definition wouldn't change...on that item.

            L Offline
            L Offline
            Lutoslaw
            wrote on last edited by
            #13

            Yeach. And if some library user tried to write to a property and didn't notice that it took no effect, then after an upgrade his code could break because the old do-nothing setter would suddenly change something. :doh:

            Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.

            1 Reply Last reply
            0
            • P PIEBALDconsult

              I just found a bunch of properties made read-only like this (I think they're from a template):

              set
              {
              // Do nothing
              }

              Huh? If you want it to be read-only, make it read-only! :mad:

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #14

              You need this scenario with XML deserialization.

              xacc.ide - now with IronScheme support
              IronScheme - 1.0 alpha 2 out now

              P G 2 Replies Last reply
              0
              • L leppie

                You need this scenario with XML deserialization.

                xacc.ide - now with IronScheme support
                IronScheme - 1.0 alpha 2 out now

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

                Good point, but... Not if it's done right. I haven't done much serialization (XML or otherwise), but as I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem. You might have to override the base class' deserializer. Or I may just be showing my ignorance. :-O

                L 1 Reply Last reply
                0
                • P Paul Conrad

                  PIEBALDconsult wrote:

                  silently ignoring the value is poor style.

                  Yes, it is. I can only imagine the headache of trying to track it down on a Friday afternoon.

                  "The clue train passed his station without stopping." - John Simmons / outlaw programmer

                  D Offline
                  D Offline
                  Derek Bartram
                  wrote on last edited by
                  #16

                  Well, personally I say get rid of properties anyway! There is simply no point in them other than making the language more bloated and less clear.... In the good old days.... .name = public class variable, didn't run any code, just gave access.... now will it run code, or won't it!?!?!??!?

                  P 1 Reply Last reply
                  0
                  • D Derek Bartram

                    Well, personally I say get rid of properties anyway! There is simply no point in them other than making the language more bloated and less clear.... In the good old days.... .name = public class variable, didn't run any code, just gave access.... now will it run code, or won't it!?!?!??!?

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

                    Sure they can be abused, but I wouldn't get rid of them just because of that.

                    D 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      Sure they can be abused, but I wouldn't get rid of them just because of that.

                      D Offline
                      D Offline
                      Derek Bartram
                      wrote on last edited by
                      #18

                      Maybe, but it just seams like a lazy way of coding that means it's harder to tell what code is actually doing. Personally I feel it makes using other people's code harder (particularily badly written code where the property name is misleading)

                      G 1 Reply Last reply
                      0
                      • D Derek Bartram

                        Maybe, but it just seams like a lazy way of coding that means it's harder to tell what code is actually doing. Personally I feel it makes using other people's code harder (particularily badly written code where the property name is misleading)

                        G Offline
                        G Offline
                        GibbleCH
                        wrote on last edited by
                        #19

                        But properly written properties do data validation, and will modify any other values that need to be modified when that property changes.

                        D 1 Reply Last reply
                        0
                        • L leppie

                          You need this scenario with XML deserialization.

                          xacc.ide - now with IronScheme support
                          IronScheme - 1.0 alpha 2 out now

                          G Offline
                          G Offline
                          GibbleCH
                          wrote on last edited by
                          #20

                          THAT'S IT! That's why I had to do something similar to this coding horror.

                          1 Reply Last reply
                          0
                          • G GibbleCH

                            But properly written properties do data validation, and will modify any other values that need to be modified when that property changes.

                            D Offline
                            D Offline
                            Derek Bartram
                            wrote on last edited by
                            #21

                            That's the problem though... the amount of people who i've seen doing things like modify data structures in property gets, is pretty high. Why leave elements of the language that can result in really hard to debug code and misconceptions?

                            G J 2 Replies Last reply
                            0
                            • D Derek Bartram

                              That's the problem though... the amount of people who i've seen doing things like modify data structures in property gets, is pretty high. Why leave elements of the language that can result in really hard to debug code and misconceptions?

                              G Offline
                              G Offline
                              GibbleCH
                              wrote on last edited by
                              #22

                              But, often, if you don't modify the data of the class, when you set the property, your data may be invalid...and calling functionX at that time will result in invalid results, or errors.

                              D 1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                Good point, but... Not if it's done right. I haven't done much serialization (XML or otherwise), but as I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem. You might have to override the base class' deserializer. Or I may just be showing my ignorance. :-O

                                L Offline
                                L Offline
                                leppie
                                wrote on last edited by
                                #23

                                PIEBALDconsult wrote:

                                I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem.

                                Sometimes you want only readonly properties in XML serialization. Unfortunately for de/serialization to work, properties need to have both a getter and a setter.

                                xacc.ide - now with IronScheme support
                                IronScheme - 1.0 alpha 2 out now

                                P 2 Replies Last reply
                                0
                                • L leppie

                                  PIEBALDconsult wrote:

                                  I recall the class specifies which members get serialized and deserialized, so this shouldn't be a problem.

                                  Sometimes you want only readonly properties in XML serialization. Unfortunately for de/serialization to work, properties need to have both a getter and a setter.

                                  xacc.ide - now with IronScheme support
                                  IronScheme - 1.0 alpha 2 out now

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

                                  The set accessor should then be private. However, the base class' contract may not allow that and then you're stuck.

                                  L 1 Reply Last reply
                                  0
                                  • G GibbleCH

                                    But, often, if you don't modify the data of the class, when you set the property, your data may be invalid...and calling functionX at that time will result in invalid results, or errors.

                                    D Offline
                                    D Offline
                                    Derek Bartram
                                    wrote on last edited by
                                    #25

                                    Yes, but it leads to misconceptions about what the code does. I suppose it all comes down to good code documentation perhaps

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      The set accessor should then be private. However, the base class' contract may not allow that and then you're stuck.

                                      L Offline
                                      L Offline
                                      leppie
                                      wrote on last edited by
                                      #26

                                      PIEBALDconsult wrote:

                                      The set accessor should then be private.

                                      No Xml serialization (I am getting tired typing that!) will choke on that. Just try it!

                                      xacc.ide - now with IronScheme support
                                      IronScheme - 1.0 alpha 2 out now

                                      P 2 Replies Last reply
                                      0
                                      • L leppie

                                        PIEBALDconsult wrote:

                                        The set accessor should then be private.

                                        No Xml serialization (I am getting tired typing that!) will choke on that. Just try it!

                                        xacc.ide - now with IronScheme support
                                        IronScheme - 1.0 alpha 2 out now

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

                                        Oh, I will... I will...

                                        1 Reply Last reply
                                        0
                                        • L leppie

                                          PIEBALDconsult wrote:

                                          The set accessor should then be private.

                                          No Xml serialization (I am getting tired typing that!) will choke on that. Just try it!

                                          xacc.ide - now with IronScheme support
                                          IronScheme - 1.0 alpha 2 out now

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

                                          I had no trouble with it.

                                          namespace Template
                                          {
                                          public partial class MyClass : System.Xml.Serialization.IXmlSerializable
                                          {
                                          public MyClass
                                          (
                                          )
                                          {
                                          }

                                              public MyClass
                                              (
                                                  string Name
                                              )
                                              {
                                                  this.Name = Name ;
                                                  
                                                  return ;
                                              }
                                          
                                              public string
                                              Name
                                              {
                                                  get ;
                                                  
                                                  **private** set ;
                                              }
                                              
                                              public void 
                                              WriteXml 
                                              (
                                                  System.Xml.XmlWriter Writer
                                              )
                                              {
                                                  System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
                                                  
                                                  doc.AppendChild ( doc.CreateElement ( "MyClass" ) ) ;
                                                  
                                                  doc.DocumentElement.InnerText = this.Name ;
                                                  
                                                  doc.WriteTo ( Writer ) ;
                                                  
                                                  Writer.Close() ;
                                                  
                                                  return ;
                                              }
                                          
                                              public void 
                                              ReadXml 
                                              (
                                                  System.Xml.XmlReader Reader 
                                              )
                                              {
                                                  System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
                                                  
                                                  doc.Load ( Reader ) ;
                                                  
                                                  Reader.Close() ;
                                                  
                                                  **this.Name = doc.DocumentElement.InnerText ;**     
                                             
                                                  return ;
                                              }
                                          
                                              public System.Xml.Schema.XmlSchema
                                              GetSchema
                                              (
                                              )
                                              {
                                                  return ( null ) ;
                                              }
                                          
                                              public override string
                                              ToString
                                              (
                                              )
                                              {
                                                  return ( this.Name ) ;
                                              }
                                          }
                                          
                                          public partial class Template
                                          {
                                              private static System.Xml.Serialization.IXmlSerializable
                                              Write
                                              (
                                                  System.Xml.Serialization.IXmlSerializable Subject
                                              )
                                              {
                                                  Subject.WriteXml ( System.Xml.XmlWriter.Create ( @"C:\\X.xml" ) ) ;
                                                  
                                                  return ( Subject ) ;
                                              }
                                          
                                              private static System.Xml.Serialization.IXmlSerializable
                                              Read
                                              (
                                                  System.Xml.Serialization.IXmlSerializable Subject
                                              )
                                              {
                                                  Subject.ReadXml ( System.Xml.XmlReader.Create ( @"C:\\X.xml" ) ) ;
                                                  
                                                  return ( Subject ) ;
                                              }
                                          
                                              \[System.STAThreadAttribute()\]
                                              public static int
                                              Main
                                              (
                                                  string\[\] args
                                              )
                                              {
                                                  int result = 0 ;
                                          
                                          L 2 Replies 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