Hello,
coming from C++ and using a lot of MFC stuff, I wonder what object exists in C# which does the job of a CObList in C++
I mean being serializable and being able to stock different types of runtime class objects.
Thanks for any further information ...
Best regards
MMM

Object List in C#
Sergey Bereznikov
Hello once more,
I got an example working but I'm not really happy with one detail:
The serialized objects don't use a function serialize as in C++ within which I could take care about historical versions of the object, which allowed me to keep a compatibility with any old serialized archive. How can I do this in C#
Deserializing older objects data into a newer version of the object
E.g. an Object circle which had 2 parameters Linewidth and Radius in version 1 of the object circle and in the version 2 of the circle the parameter linecoulor would be added. When loading the version 1 object with the version 2 software I used to initialize the not serialized parameter color to black and due to the fact that the serializing in C++ first constructed the object with the default parameters from the constructor and then overwrote the parameters from the archive file this was working fine and I kept compatibility by memorizing for each object first its version number and then during deserializing I used this version number to define which parameters were explicitly deserialized in the Serialize function (switch over all versions when deserializing=serialize with archive isLoading)
How would I do this in C# where there is no serialize method required inside my object circle when I deserialize my objects (circle, rectangle etc...) through my arrayList.
Best regards
_MMM_
stellag
Binary Serialization is effeicient way to serialize some object. Its fast and a serialized object takes lesser space in memory on the other hand XMl Serialization / Soap Serialization takes more memory and is not much efficient like Binary Serialization.
One other thing, Binary Serialization is preferred one your system is .Net based, both serialization and deserialization because you not to have same assembly to serialize and deserialize the object.
But suppose you have a situation where you need to serialize some object which have to be deserialized using Java or some other langauge then only XML serialization will help because its resulting memory is parsable unlike binary......
But if system is pure .Net based then Binary Serialization is the ritght choice!
Best Regards,
Rizwan
Blast
You can use as keyword to determine the type and fo casting both with a single statement
just a example:
SomeClass1 objectToCheck = new SomeClass1();
object tempObject = objectToCheck;
SomeClass1 someObject1 = tempObject as SomeClass1; // If tempObject was really an object of SomeClass1 as in this case its true it'll assing someObject1 to its value otherwise set it to null.
now you can just check this:
if(someObject1 != null)
{
// It was object of SomeClass1
}
else
{
// It was not an object of SomeClass1
}
There is also a key word is:
like:
int i = 1;
object objInt = i;
now
if(objInt is int)
{
// its int, so now parse it
int j = (int) objInt;
}
else
{
// Its not int... Dont parse it otherwise it'll rase an exception
}
Better is to use as keword which is more efficent and avoid redundancy in casting...
I hope it makes good sense!
Best Regards,
Rizwan
Alvin Kuiper
Thanks for your help Rizwan,
just a little detail what can I do if I don't know what object was stored In the C++ CObList this was done automatically when deserializing (loading from a serialized CObList) The objects were recreated due to some RUNTIME_CLASS information to the correct classes and I took care about the different versions of this class inside the deserialize depending on a first value which is allways serialized as the first item.
Anything similar existing in C#
Best regards
_MMM_
hrubesh
Hello Sven,
maybe a very stupid question, but the ArrayList class has no Serialize or deserialize method
In the code below the compiler misses the Deserialize method:
private void buttonDeserialize_Click(object sender, System.EventArgs e){
arrayList1.Deserialize();
}
The compiler replies:
System.Collections.ArrayList does not contain a definition for 'Deserialize'
What do I do wrong
Best regards
_MMM_
pkafka
Sorry - I thought you knew how serialization works in the framework.
As Rizwan pointed out BinaryFormatter is a way to go. When you look up "BinaryFormatter class" in MSDN you'll see an example how to use it. Quite straight forward. Create the formatter and pass the Serialize method a stream an the object root (your arraylist) to serialize.
--
SvenC
Jive Dadson
Hi,
ArrayList could be used in a similar way I guess.
--
SvenC
MichaelEaton
You can retrieve the object as type Object and use GetType() on the returned object to get information of type the object is.
Serializing and deserializing should still be automatic, as every objects serialize/deserialize code will be called upon serializing/deserializing the arraylist.
--
SvenC
BMalone
ArrayList doesnot contain any Serialize or Deserialize Methods but these methods are available thorugh the use of BinaryFormatter or XmlFormatter Classes...
By the way what you are trying to achieve
Best Regards,
cisco0407
XmlFormatter is new with WCF - don't know much about. I think the idea is that you must explicitly specify what is serialized and by default private is off. I guess because private members are not thought of as belonging to the contract of the public exchange of messages what WCF is all about.
BinaryFormatter and SoapFormatter serialize all members by default. BinaryFormatter creates compatible streams between 1.1 and 2.0, so one might hope that stays true for further framework versions. It is more compact, so I would prefer that. While one might think that you can fiddle around in the output of SoapFormatter or XmlFormatter that might be quite painfull when your object arrays get bigger with large objects which again reference objects and so on. So if editing the serialized content is your intent of using xml, well have a look at a serialized stream...
--
SvenC
JWei
Hi Sven,
thanks for the information I will make some trials...
Best regards
_MMM_
RDuke
Hello Rizwan,
I want to create a similar idea as I did before with C++ storing and restoring different objects which are created during runtime by the user.
With CObLists this was pretty simple - looks more difficult with CSharp (maybe because I'm not yet fit enough in CSharp)
Is there a more elegant way in C# then using Serialization
The Binary Serialization would be similar to what I earlier did, but XML Serialization seems only to allow public members to be serialized But I would like to get a complete object in its whole status serialized. So when I reload I would have all objects exactly in the same state in memory ...
Thanks for any tips if anyone has a better way to do this.
Best regards
_MMM_
T Julich
Hello Sven,
Thanks again for your help. I will make some more tests, binary has some disadvantaged XML would be preferable but has the disadvantage that is only uses public members
About your comment about my knowledge about the framework usage - you are right, this is mainly new for me ...
... Thats why I as kyou guys which have allready more experience and I'm happy about your help and replies.
Best regards
_MMM_
Vijay R
ArrayList can hold any type of object in it! Putting any type of element is ok but when you need it back u must know what you stored at a specific index for example:
SomeClass1 someObject1 = new SomeClass1();
SomeClass2 someObject2 = new SomeClass2();
ArrayList arrayList = new ArrayList();
arrayList.Add(someObject1);
arrayList.Add(someObject2);
Retreving value back without removing fro the Array list:
object obj = arrayList[0]; \\ OK
SomeClass1 tempObject1 = (SomeClass1) arrayList[0]; //OK, Correctly Parsed because it belonged to the same object before being converted to object.
SomeClass2 tempObject2 = (SomeClass2) arrayList[0]; // Will Raise an Exception when parsing an object to invalid type...
If you are working in C# 2.0 I would recomend using Generic Collection found under System.Collections.Generics
like List<T>, Dictionary etc... the dont allow to store multiple types but saves a lot of CPU time in parsing forth and back and compiler checkng for specific types.
Best Regards,
Rizwan