Serialization Problem (ViewState)

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
I have the following class (originally a struct, but wasn't working so changed to class, but still not working). I am saving and trying to pull from ViewState - before adding Serialization attribute had the 'must be Serializable error', so added the attirbute and interface and followed the help files. Still when saving to view state and pulling back out I get 'Nothing' as the value and the special serialization constructor and GetObjectData methods are never called. Haven't had to mess with serialization up until now, so not sure where to go from here.

What am I doing wrong.

Thanks!

Visual Basic:
<Serializable()> _
	Public NotInheritable Class TimePeriod
		Implements ISerializable

		Private _month, _year As String
		Public Property Month() As String
			Get
				Return _month
			End Get
			Set(ByVal Value As String)
				_month = Value
			End Set
		End Property
		Public Property Year() As String
			Get
				Return _year
			End Get
			Set(ByVal Value As String)
				_year = Value
			End Set
		End Property

		Public Sub New(ByVal m As String, ByVal y As String)
			Month = m
			Year = y
		End Sub

		Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
			Dim m As String = DirectCast(info.GetValue("Month", GetType(String)), String)
			Dim y As String = DirectCast(info.GetValue("Year", GetType(String)), String)
			If Not m Is Nothing AndAlso Not y Is Nothing Then
				Month = m
				Year = y
			Else
				Month = "1"
				Year = "1900"
			End If
		End Sub

		Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, _
		   ByVal context As System.Runtime.Serialization.StreamingContext) _
		   Implements System.Runtime.Serialization.ISerializable.GetObjectData
			info.AddValue("Month", Month, GetType(String))
			info.AddValue("Year", Year, GetType(String))
		End Sub
	End Class
 
Can you show your serialization and deserialization code?

Don't you need an empty public constructor to serialize?
 
The code is above, all you should need according to MSDN is what is above. If you've done it another way, let me see how you code it and maybe I'll see what I'm missing.

Thanks.
 
Back
Top