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.
  • 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
                        • P PIEBALDconsult

                          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 Offline
                          L Offline
                          leppie
                          wrote on last edited by
                          #29

                          Implementing your own custom IXmlSerializable is cheating ;P For starters you need to use the XmlSerializer. And you code should just represent a basic C# object. Example:

                          using System;
                          using System.Xml.Serialization;
                          using System.IO;

                          public class Foo
                          {
                          public int Bar { get; private set; }
                          }

                          class Entrypoint
                          {
                          static void Main()
                          {
                          XmlSerializer ser = new XmlSerializer(typeof(Foo));

                          using (Stream s \= File.OpenWrite("foo.xml"))
                          {
                            ser.Serialize(s, new Foo());
                          }
                          

                          }
                          }

                          Fails!

                          <

                          P 1 Reply Last reply
                          0
                          • P PIEBALDconsult

                            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 Offline
                            L Offline
                            leppie
                            wrote on last edited by
                            #30

                            This is a continuation of the sample: You might say, then use the XmlIgnore attribute, but in that case the property is never emitted, but what if you want to emit it, but you simply dont care about the result after deserializing it?

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

                            1 Reply Last reply
                            0
                            • L leppie

                              Implementing your own custom IXmlSerializable is cheating ;P For starters you need to use the XmlSerializer. And you code should just represent a basic C# object. Example:

                              using System;
                              using System.Xml.Serialization;
                              using System.IO;

                              public class Foo
                              {
                              public int Bar { get; private set; }
                              }

                              class Entrypoint
                              {
                              static void Main()
                              {
                              XmlSerializer ser = new XmlSerializer(typeof(Foo));

                              using (Stream s \= File.OpenWrite("foo.xml"))
                              {
                                ser.Serialize(s, new Foo());
                              }
                              

                              }
                              }

                              Fails!

                              <

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

                              That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try. This is like trying to use a DataAdapter on a query with a join or a view or something and then complaining that .Update won't work. And as I said, "Not if it's done right"; I done it right, you didn't. I win, neener neener neener! :laugh:

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

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

                                mav.northwind wrote:

                                Black holes are the places where God divided by 0...

                                Man: Hey, God, why did you make black holes? God: I like a good BM in the morning, same as the next guy.

                                1 Reply 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
                                  #33

                                  Why write a class in a particular (bad) way just to bypass a flaw in some other brain-dead class? Dealing with a class that someone else wrote which doesn't implement the desired serialization is another matter entirely, but when you're writing the class you have control. If you're writing a class intending it to be serialized, then it should be marked with SerializableAttribute and implement ISerializable and/or IXmlSerializable as appropriate. "Use the right tool for the right job." -- Scotty, et al

                                  1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try. This is like trying to use a DataAdapter on a query with a join or a view or something and then complaining that .Update won't work. And as I said, "Not if it's done right"; I done it right, you didn't. I win, neener neener neener! :laugh:

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

                                    PIEBALDconsult wrote:

                                    That's just crazy talk; if the class doesn't implement IXmlSerializable then clearly it can't be serialized to XML, so don't try.

                                    The way I showed you is exactly how web services does it. [update] I think, it's 5:30 am, I just woke for a smoke, not thinking really [update]

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

                                    modified on Saturday, March 22, 2008 11:38 PM

                                    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:

                                      S Offline
                                      S Offline
                                      Super Lloyd
                                      wrote on last edited by
                                      #35

                                      You missed public virtual Property { get {...} set {..}}

                                      P 1 Reply Last reply
                                      0
                                      • S Super Lloyd

                                        You missed public virtual Property { get {...} set {..}}

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

                                        Well that is what I was alluding to when I said: " The set accessor should then be private. However, the base class' contract may not allow that and then you're stuck. " (In a different branch.) However in your example, the derived class should at least call base.Property. If the base class is abstract then you're stuck (but an exception should be thrown or something!). However, I argue that an abstract class should probably not specify that a set is required, which may be a whole new topic.

                                        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

                                          F Offline
                                          F Offline
                                          Fatbuddha 1
                                          wrote on last edited by
                                          #37

                                          COOL!!!! That one is outstanding :laugh: Cheers

                                          You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)

                                          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