Read-only properties
-
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
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!?!?!??!?
-
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!?!?!??!?
Sure they can be abused, but I wouldn't get rid of them just because of that.
-
Sure they can be abused, but I wouldn't get rid of them just because of that.
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)
-
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)
-
You need this scenario with XML deserialization.
xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out now -
But properly written properties do data validation, and will modify any other values that need to be modified when that property changes.
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?
-
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?
-
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
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 -
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 nowThe
set
accessor should then beprivate
. However, the base class' contract may not allow that and then you're stuck. -
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.
Yes, but it leads to misconceptions about what the code does. I suppose it all comes down to good code documentation perhaps
-
The
set
accessor should then beprivate
. However, the base class' contract may not allow that and then you're stuck. -
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 nowOh, I will... I will...
-
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 nowI 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 ;
-
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 ;
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!
<
-
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 ;
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 -
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!
<
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:
-
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...
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.
-
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 nowWhy 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
-
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:
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 nowmodified on Saturday, March 22, 2008 11:38 PM
-
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:
You missed public virtual Property { get {...} set {..}}