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

    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
                          • 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?

                            J Offline
                            J Offline
                            Jeff Dickey
                            wrote on last edited by
                            #38

                            ...to a sufficiently motivated/determined idiot. No useful language can enforce a fixed floor for intelligence or sensibility of its (ab)user. Languages that make significant efforts in those directions, like Logo or Wirth's original Pascal, find themselves compartmentalised and marginalised safely out of the way of any Real Programmers™. You can't effectively use language semantics to guard against process failure. The entire reasoning for practices like XP pair programming, test-driven development that requires every bit of code to be tested, and so on is precisely to guard against such ingenious stupidity.

                            Jeff Dickey Seven Sigma Software and Services Phone/SMS: +65 8333 4403 Yahoo! IM: jeff_dickey MSN IM:    jeff_dickey at hotmail.com ICQ IM:    8053918 Skype:     jeff_dickey

                            D 1 Reply Last reply
                            0
                            • J Jeff Dickey

                              ...to a sufficiently motivated/determined idiot. No useful language can enforce a fixed floor for intelligence or sensibility of its (ab)user. Languages that make significant efforts in those directions, like Logo or Wirth's original Pascal, find themselves compartmentalised and marginalised safely out of the way of any Real Programmers™. You can't effectively use language semantics to guard against process failure. The entire reasoning for practices like XP pair programming, test-driven development that requires every bit of code to be tested, and so on is precisely to guard against such ingenious stupidity.

                              Jeff Dickey Seven Sigma Software and Services Phone/SMS: +65 8333 4403 Yahoo! IM: jeff_dickey MSN IM:    jeff_dickey at hotmail.com ICQ IM:    8053918 Skype:     jeff_dickey

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

                              No useful language is idiot-proof... But languages can be FOR idiots..... *see Java

                              J 1 Reply Last reply
                              0
                              • D Derek Bartram

                                No useful language is idiot-proof... But languages can be FOR idiots..... *see Java

                                J Offline
                                J Offline
                                Jeff Dickey
                                wrote on last edited by
                                #40

                                Java, as originally conceived (J2SE, that is) is a perfectly decent language for several different problem domains, including particularly those similar to that for which it was created. The idiocy comes from two things. First, the "Java should do everything including the kitchen sink" mentality that gave us J2ME and (good working definition of "coding horror") J2EE. Secondly, the dishonest marketing commonly associated with the infinite battalions of H1B coders who have perfectly good-sounding (though mass-mimeographed) "qualifications" but couldn't solve a problem in Java (or apparently anything else) if you gave them a flashlight, a map, all the textbooks they could carry and a two-year head start. But they're the fashion, so thousands of lemmings cleverly disguised as "software-using companies" are throwing themselves of the cliff. (It's a little late to realize that you need an exit strategy when you've already been accelerating at 10 m/sec/sec for two or three minutes....)

                                Jeff Dickey Seven Sigma Software and Services Phone/SMS: +65 8333 4403 Yahoo! IM: jeff_dickey MSN IM:    jeff_dickey at hotmail.com ICQ IM:    8053918 Skype:     jeff_dickey

                                D 1 Reply Last reply
                                0
                                • J Jeff Dickey

                                  Java, as originally conceived (J2SE, that is) is a perfectly decent language for several different problem domains, including particularly those similar to that for which it was created. The idiocy comes from two things. First, the "Java should do everything including the kitchen sink" mentality that gave us J2ME and (good working definition of "coding horror") J2EE. Secondly, the dishonest marketing commonly associated with the infinite battalions of H1B coders who have perfectly good-sounding (though mass-mimeographed) "qualifications" but couldn't solve a problem in Java (or apparently anything else) if you gave them a flashlight, a map, all the textbooks they could carry and a two-year head start. But they're the fashion, so thousands of lemmings cleverly disguised as "software-using companies" are throwing themselves of the cliff. (It's a little late to realize that you need an exit strategy when you've already been accelerating at 10 m/sec/sec for two or three minutes....)

                                  Jeff Dickey Seven Sigma Software and Services Phone/SMS: +65 8333 4403 Yahoo! IM: jeff_dickey MSN IM:    jeff_dickey at hotmail.com ICQ IM:    8053918 Skype:     jeff_dickey

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

                                  Jeff Dickey wrote:

                                  Java, as originally conceived (J2SE, that is) is a perfectly decent language for several different problem domains, including particularly those similar to that for which it was created.

                                  Out of curiosity what domains it is better suited for? Personally i've only ever see the advantage of Java being for mobile and cross platform applications, something I see in the second case as being of limited value.

                                  J 1 Reply Last reply
                                  0
                                  • D Derek Bartram

                                    Jeff Dickey wrote:

                                    Java, as originally conceived (J2SE, that is) is a perfectly decent language for several different problem domains, including particularly those similar to that for which it was created.

                                    Out of curiosity what domains it is better suited for? Personally i've only ever see the advantage of Java being for mobile and cross platform applications, something I see in the second case as being of limited value.

                                    J Offline
                                    J Offline
                                    Jeff Dickey
                                    wrote on last edited by
                                    #42

                                    I'd say small, embedded systems like set-top boxes or microwave ovens or TiVos or suchlike....close enough to the original problem domain addressed by what eventually became J2SE 1.0, without all the EE or additional-API BS-cleaverly-disguised-as-APIs. When Java was hijacked from scratching-an-itch to become the cornerstone of the One "True" Programming Religion, with hundreds of thousands of low-cost, mass-produced "proessionals", the nice, more-or-less-forward progress of software discovery and evolution took a nasty, twisted turn. For many shops controlled by Javacolytes, that 'twisted turn' has straightened out nicely. They're under a constant acceleration of ten meters per second per second...

                                    Jeff Dickey Seven Sigma Software and Services Phone/SMS: +65 8333 4403 Yahoo! IM: jeff_dickey MSN IM:    jeff_dickey at hotmail.com ICQ IM:    8053918 Skype:     jeff_dickey

                                    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