473,847 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serialization questions

My app is near completed for the basic feature of version 1.0.

I have an extensive object model and I now want to persist my objects using
serialization.
I have chosen binaryformatter to serialize, and custom serialization, which
I understand will allow me the flexibility of not breaking old things when I
add members to classes in the future and send to existing customers.

1.) is there anything else to consider with the custom serialization

2.) when doing the info.addvalue in GetObjectData, if one of the members of
my class is an instance of another class, do I go ahead and add it--and that
somehow calls the serialization process of that class to fullfill this?

3.) some of my classes are collections. They inherited from collectionbase.
Is collection base not serializable? Assuming it may not be serializable,
the documentation says serialization will fail. If so, how in the world can
I handle this issue? I need to serialize all items of the collection base
and then deserialize them.

Are there any other comments about this? Any other gotchas?

Thanks a bunch.

Shane
Nov 21 '05 #1
10 1492
SSStory,

The nicest I have seen, I cannot make it nicer.

\\\Tom Shelton
Private Function SerializeFontOb ject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream
Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))
Try
Return DirectCast(bf.D eserialize(mem) , Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function
////

I hope this helps a little bit?

Cor
Nov 21 '05 #2
Thanks for trying.
I don't see how this answers either of my questions.
I do already know how to do the binary formatter and I have that code in
place. Just having trouble as mentioned in the original question, knowing
for one how do I serialize a class that inherits from collectionbase which
apparently isn't serializable.

And the other question also.

A third could be how do I find out if a class is serializable if I don't
have the source--like CollectionBase?

Thanks Cor,

Shane
"Cor Ligthert" <no************ @planet.nl> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
SSStory,

The nicest I have seen, I cannot make it nicer.

\\\Tom Shelton
Private Function SerializeFontOb ject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream
Try
bf.Serialize(me m, fnt)
Return Convert.ToBase6 4String(mem.ToA rray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function
Private Function DeserializeFont Object(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Co nvert.FromBase6 4String(fnt))
Try
Return DirectCast(bf.D eserialize(mem) , Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function
////

I hope this helps a little bit?

Cor

Nov 21 '05 #3

Attributes are listed in the docs. CollectionBase docs say:

<Serializable >
MustInherit Public Class CollectionBase
Implements IList, ICollection, IEnumerable

So, yes, CollectionBase is serializable.

Best regards,

Sam
On Sat, 2 Apr 2005 15:43:58 -0600, "SStory"
<Th*******@TAKE OUTTHISSPAMBUST ERsofthome.net> wrote:
Thanks for trying.
I don't see how this answers either of my questions.
I do already know how to do the binary formatter and I have that code in
place. Just having trouble as mentioned in the original question, knowing
for one how do I serialize a class that inherits from collectionbase which
apparently isn't serializable.

And the other question also.

A third could be how do I find out if a class is serializable if I don't
have the source--like CollectionBase?

Thanks Cor,

Shane


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #4
cool. I did finally find that in MSDN--DUH. Just was looking for it in the
object browser for some reason.

The other question was....

if a member of my class is a class, does deserializer also call the
mechanism for that member class automatically and handle it for you?
(assuming it is a <serializable > class of course)

Thanks,

Shane

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:k4******** *************** *********@4ax.c om...

Attributes are listed in the docs. CollectionBase docs say:

<Serializable >
MustInherit Public Class CollectionBase
Implements IList, ICollection, IEnumerable

So, yes, CollectionBase is serializable.

Best regards,

Sam
On Sat, 2 Apr 2005 15:43:58 -0600, "SStory"
<Th*******@TAKE OUTTHISSPAMBUST ERsofthome.net> wrote:
Thanks for trying.
I don't see how this answers either of my questions.
I do already know how to do the binary formatter and I have that code in
place. Just having trouble as mentioned in the original question, knowing
for one how do I serialize a class that inherits from collectionbase which
apparently isn't serializable.

And the other question also.

A third could be how do I find out if a class is serializable if I don't
have the source--like CollectionBase?

Thanks Cor,

Shane


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #5

Yes, it serializes the entire object graph (hierarchy).

Sam
On Tue, 5 Apr 2005 11:04:56 -0500, "Shane Story"
<sh************ **@dv-corp.com> wrote:
cool. I did finally find that in MSDN--DUH. Just was looking for it in the
object browser for some reason.

The other question was....

if a member of my class is a class, does deserializer also call the
mechanism for that member class automatically and handle it for you?
(assuming it is a <serializable > class of course)

Thanks,

Shane

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:k4******* *************** **********@4ax. com...

Attributes are listed in the docs. CollectionBase docs say:

<Serializable >
MustInherit Public Class CollectionBase
Implements IList, ICollection, IEnumerable

So, yes, CollectionBase is serializable.

Best regards,

Sam

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #6
Thanks Samuel.

Considering what else I have read on the subject, it seems that if you
serialize and object and later make any changes, that the things serialized
before the change would not be readable. For this reason custom
serialization seems better. Would you agree?

Thanks
"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:su******** *************** *********@4ax.c om...

Yes, it serializes the entire object graph (hierarchy).

Sam
On Tue, 5 Apr 2005 11:04:56 -0500, "Shane Story"
<sh************ **@dv-corp.com> wrote:
cool. I did finally find that in MSDN--DUH. Just was looking for it in
the
object browser for some reason.

The other question was....

if a member of my class is a class, does deserializer also call the
mechanism for that member class automatically and handle it for you?
(assuming it is a <serializable > class of course)

Thanks,

Shane

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:k4****** *************** ***********@4ax .com...

Attributes are listed in the docs. CollectionBase docs say:

<Serializable >
MustInherit Public Class CollectionBase
Implements IList, ICollection, IEnumerable

So, yes, CollectionBase is serializable.

Best regards,

Sam

B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #7

Unfortunately there's a huge leap between XmlSerializer and custom
serialization--quick and dirty vs totally do it yourself. There are
lots issues that XmlSerializer doesn't address and for that reason a
lot of people (most it seems) recommend doing it yourself.

XmlSerializer does do a good job for what it's programmed to do, so if
you can use it in your situation I'd suggest doing so--you can always
switch to custom if you really need more functionality. We of course
have the same concerns over versioning that you do and have an
architecture to get around it (well, planned architecture) by using a
bookmarked XML reader combined with XSLT.

Basically, we'll read the XML up to a Version attribute on the root
element. If that matches the current, then reset the reader to the
beginning and process as normal.

If the version is old, then reset the reader and perform a
transformation on it to get the new format (requires writing XSLT,
which some consider more complicated then writing a custom
serializer).

If the version is newer, then cancel or take whatever action is
appropriate in your application.

Here's a bookmarked XML reader from MS:

http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp

It has some issues of it's own--it won't even compile as distributed.
But once they're fixed, it seems to work pretty well (fixes: invalid
boolean check at end with bool != null, and a Debug.Assert that always
gets triggered).

HTH,

Sam
On Tue, 5 Apr 2005 14:41:12 -0500, "Shane Story"
<sh************ **@dv-corp.com> wrote:
Thanks Samuel.

Considering what else I have read on the subject, it seems that if you
serialize and object and later make any changes, that the things serialized
before the change would not be readable. For this reason custom
serializatio n seems better. Would you agree?

Thanks


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.
Nov 21 '05 #8
Thanks Samuel for all of your help.

I am trying to use binaryformatter . I have no trouble saving (serializing),
but when I go to deserialize I always get errors. It appears that I cannot
set a breakpoint or anything to discover where the problem is.

I am serializing by implementing ISerializable: The GetObjectData method &
the custom Constructor.

The last problem I got was an exception--TargetInvocatio nException class to
be exact.

My object model has things like the following to deal with:

MDIForm (which has a bunch of frmA's)
frmA
which has a Workspace object. This object has some members and then some
other objects.

for the sake of this newsgroup let me give an example scenario and see what
I should do.

Workspace has a reference to the form that "owns" it (this is not
serializable of course).

So imagine
class workspace
private A as single
private B as single
private ParentForm as frmA
private objSomething as Something
end class

class Something
'class something has a reference to the parentworkspace to which it
belongs
private ParentWorkSpace as workspace
end class

my thought was, in GetDataObject for workspace object to just do the
following:
info.addvalue(" A",A)
info.addvalue(" B",B)
info.addvalue(" objSomething",S omething)

and then in the specialized contstructor for deserializing
A=info.getsingl e("A")
B=info.getsingl e("B")

objSomething=ct ype(info.getval ue("objSomethin g",gettype(some thing)),somethi n
g)
'then at this point, maybe call a method on objSomething to set the
parent workspace object
'reference to this object
objSomething.Se tParentWorkspac e=me

I would do the same thing to the Workspace object itself after being
deserialized, it would set the ParentForm with a method.

Am I on the wrong track? Any ideas, from you or any other group member
about the exception.

Also any ideas on how to debug this stuff, since it seems to just happen and
not allow me to step into the call, which I assume just eventually calls the
constructor.

Thanks for all your help.

Shane

"Samuel R. Neff" <bl****@newsgro up.nospam> wrote in message
news:ot******** *************** *********@4ax.c om...

Unfortunately there's a huge leap between XmlSerializer and custom
serialization--quick and dirty vs totally do it yourself. There are
lots issues that XmlSerializer doesn't address and for that reason a
lot of people (most it seems) recommend doing it yourself.

XmlSerializer does do a good job for what it's programmed to do, so if
you can use it in your situation I'd suggest doing so--you can always
switch to custom if you really need more functionality. We of course
have the same concerns over versioning that you do and have an
architecture to get around it (well, planned architecture) by using a
bookmarked XML reader combined with XSLT.

Basically, we'll read the XML up to a Version attribute on the root
element. If that matches the current, then reset the reader to the
beginning and process as normal.

If the version is old, then reset the reader and perform a
transformation on it to get the new format (requires writing XSLT,
which some consider more complicated then writing a custom
serializer).

If the version is newer, then cancel or take whatever action is
appropriate in your application.

Here's a bookmarked XML reader from MS:

http://msdn.microsoft.com/XML/Buildi...mlBkMkRead.asp
It has some issues of it's own--it won't even compile as distributed.
But once they're fixed, it seems to work pretty well (fixes: invalid
boolean check at end with bool != null, and a Debug.Assert that always
gets triggered).

HTH,

Sam
On Tue, 5 Apr 2005 14:41:12 -0500, "Shane Story"
<sh************ **@dv-corp.com> wrote:
Thanks Samuel.

Considering what else I have read on the subject, it seems that if you
serialize and object and later make any changes, that the things serializedbefore the change would not be readable. For this reason custom
serializatio n seems better. Would you agree?

Thanks


B-Line is now hiring one Washington D.C. area VB.NET
developer for WinForms + WebServices position.
Seaking mid to senior level developer. For
information or to apply e-mail resume to
sam_blinex_com.

Nov 21 '05 #9
Both classes need to have the "<serializeable ()>" attribute before you can
properly serialize/deserialize. FYI, and you may already know this, if the
number of members in either of the classes changes, it'll render your
serialized object useless -- meaning that deserialization of your object
will throw exceptions.

If I were you I would look at XML serialization if you want to persist the
state of your application. I know that there are a lot of developers out
there who will probably flame me for say so, but you can essentially use
*.config files to save the information. Config files are troublesome in
that some systems you might deploy on have strict security settings for
user accounts, and most will only have ONE location in which to write to.
There are ways to find out where these locations are (based on the user
account...) check out references to "IsolatedStorag e" (C:\Documents and
Settings\<user> \Local Settings\Applic ation Data\IsolatedSt orage). If you go
with the *.config file route, take some time to explore google for the pros
and cons. I'm personally using it in an application I'm developing, and it
seems to be working well with different security settings/user accounts.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1054
by: To | last post by:
Hi. I've two big serialization problems: The first is about the <nonserialized> attribute on a field on a mother class. His inheritance seems to be effective only if the member is public. If it is not the case, when I try to seriliazise the child class, vb tries to serialize my non serializable member!!!! The second problem is about the serialization of a class which inherit from a hastable. The hashtable implements ISerializable so I...
37
5041
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
1
3178
by: Ayende Rahien | last post by:
Two questions: I've a XmlTextWriter that I want to use to build a string in memory. However, when I'm using a StringWriter, the xml comes out at UTF-16, which isn't good for me. Currently, I solved the problem by using XmlDocument, but it seems a waste to use that if I can use XmlWriter. I tried to spesify the encoding, and then it complained about needing a Stream and not a TextWriter. I *must* have this in memory, writing to file is...
16
9559
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ....... </ArrayOfClassname>
5
2371
by: Arjen | last post by:
Hello, Can somebody help me a little bit? I can't get it to work. Please see my code below... I have placed some comments like "// And whats next?". I'm sure that I have to code something here. But what? What do I want?
0
1256
by: Goethals Frederik | last post by:
Hi, I have some questions that are a little difficult to explain, so I give it a try... I have an application (aSP.NET with VB.NET codebehind) and I would like to store my data on disk because the users could continue later on there project with the same data already filled in. So I would use the soap-serialization to store the info (the nested objects, collections, ...). I think this process will work very well, also the
0
1071
by: Shane Story | last post by:
My other thread on serialization seemed to be stagnant and I still have questions, so I decided to post this example of what I need to know. I am trying to use binaryformatter. I have no trouble saving (serializing), but when I go to deserialize I always get errors. It appears that I cannot set a breakpoint or anything to discover where the problem is. I am serializing by implementing ISerializable: The GetObjectData method and the...
0
282
by: Art | last post by:
Hi, I've got a lot of confusion about XML serialization. 2 specific questions: I'm using a book by Robin Reynolds-Haertle. In her section on serialization she starts with binary and follows it up with XML. I did that. Then I notice, I think, that for the XML part I don't need the <serializable> attribute, the ISerializable interface and the GetObjectData method. It
5
4330
by: RobinS | last post by:
I want to serialize a class that I am using to retain some information the user types into a screen. I have 3 questions. 1) I serialized it as XML to start with. This works, but how do I serialize the strings so that they are not messed up if they have XML in them, or control characters? Is there a way to do that in XML, or do I have to use BinaryFormatters? 2) So I tried using a binary formatter, and it won't serialize/deserialize...
0
9734
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10995
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10654
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9493
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7888
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4130
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.