Question Serialize an object, exception because form is not Serializable

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I have a bit of a problem here and hope somebody here can help me figure out what the problem is

I have a class called Truck and a form called frmTruck

I have some code like this in a method. It recieves obj as Truck

VB.NET:
Dim ms As New MemoryStream(1000)
Dim bf As New BinaryFormatter(Nothing, New StreamingContext(StreamingContextStates.Clone))
bf.Serialize(ms, obj)

To simplify my code, I have basically something like this for frmTruck:

VB.NET:
public class frmTruck

private truckValue as Truck

public sub DoSomething()
truckValue = LoadTruckFromDatabase()
SerializeTruck(truckValue)
End sub

End class

During the LoadTruckFromDatabase function I call the SerializeTruck method, here everything goes fine. But when I call the SerializeTruck just before End sub of DoSomething, it goes wrong
I get an exception saying that frmTruck is not serializable

My truckinstance doesn't have any references to frmTruck... or at least not as far as I can see... and there shouldn't be any
Is there a way how to figure out at what point it goes wrong, so I can figure out what part of the Truck object it doesn't like?
 
Yes, the Truck class is set to be Serializable. I know forms are not serializable and I don't want to serialize the form. In my Truck class object there should be no reference to the form, it's only the form that has a reference to the truck object
 
The reason I asked was because you said "exception because form is not Serializable" and "that frmTruck is not serializable", neither which would be relevant for your Truck class or truckValue variable.
Does Truck class have any members? Remember it can't have any members that is not serializable also.
 
It has different members, but all of them is serializable
Also as I said, I do use the serialize on the Truck object before it is added as a private variable, here it works fine, but after adding it, the error occours
 
From the information you have provided so far I can't tell what the problem might be. If you serialize a serializable Truck class instance that has only serializable members then there is no problem, so there must be something else you do wrong.
 
Is there a way where I can automatically go through all variables in the truck object and see if there's something there that points to something that is not serializable?
The Truck class has lots of properties, where none of them should contain references to the frmTruck form, but I guess somewhere something goes wrong, but I haven't been able to figure out how to find exactly where in the Truck class the problem is
 
The exception message will tell you that, for example
SerializationException said:
Type 'Something' in Assembly 'WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
 
But that something doesn't make sense... it just tells me that frmTruck is not serializable. Of course it's not serializable, but that shouldn't make any difference as there shouldn't be any references to frmTruck in the Truck
The problem only starts AFTER frmTruck gets a reference to the Truck object, but never the other way around
 
The 'Something' is the something in Truck that would not be serializable, not the Truck type itself (unless it is the Truck type that is not serializable :)).
 
Sounds like I'm not good at explaining so I'll try to explain it differently

My Truck class is basically something like this (here's the very short version):
VB.NET:
Public Class Truck
Public Property Id as integer
Public Property RegisterDate as date
Public Property Chassis as String

Public sub SerializeObject
 ' This sub uses some serialize code as mentioned in first post
' Exactly what it does, doesn't matter... but it serializes
End sub

Public shared function Load(if as integer) as Truck
Dim result as new Truck
' Connects to database and fills information as normal:
result.Id = reader.getint32(0)
' Etc... then closes the connection
'Calls the serialize sub... works great
SerializeObject
return result
End function

End Class

Truck has lots and lots of properties like above, but all properties are simple stuff like Date, String or Integer. Nothing in the Truck object should have anything to do with any forms at all

On the frmTruck I do this:

VB.NET:
public class frmTruck

private truckValue as Truck

public sub DoSomething()
' First it loads the truck and remember as seen above, during the Load method it also serializes. This works perfectly
truckValue = Load()
' Then if I later call the exact same method, the Serialize fails
truckValue.SerializeObject()
End sub

End class

Exception: Type 'frmTruck' in Assembly 'WindowsApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

I'm having a real hard time figuring out why it wants to serialize frmTruck. frmTruck (or any form for that matter) is never mentioned in the Truck object
 
You have not applied the Serializable attribute to Truck class like I said in first reply.

Since it says frmTruck is the problem either Truck class has a frmTruck type reference or you are serializing the wrong object.
 
The Truck class is serializable and that's the class I'm serializing, of that I'm 100% sure
I have tried to triplecheck everything in the Truck class, but I can't find ANY references to frmTruck
 
Back
Top