Serialize class with hashtable

D

Dick

Hello,

I'm trying to serialize a class with a Hashtable within:

' Class code:
Imports System.Collections
Class clsOptions
Public countID As Integer
Public persons As New Hashtable
End Class

'Main app code:
Dim serializer As XmlSerializer = New XmlSerializer(GetType(clsOptions))


This doesn't work.
I've read something in the helpfiles that I have to add a count property and
add method, because Hashtable implements IList.
But I have no clue what is needed.
 
O

Oldkrot

Dick said:
Hello,

I'm trying to serialize a class with a Hashtable within:

' Class code:
Imports System.Collections
Class clsOptions
Public countID As Integer
Public persons As New Hashtable
End Class

'Main app code:
Dim serializer As XmlSerializer = New XmlSerializer(GetType(clsOptions))


This doesn't work.
I've read something in the helpfiles that I have to add a count property and
add method, because Hashtable implements IList.
But I have no clue what is needed.

You must put atribuite before class declaration
private countID As Integer
private persons As Hashtable
public sub new
persons =new Hashtable

end sub
 
O

Old Krot

serialization functions
Imports System.IO
Imports System

''' <summary>
''' this class in charge of all operations with reaction <database>
''' </summary>
Public Class clsDataServices

''' <summary>
''' load relation data from HD
''' </summary>
Private Function LoadRelationData() As PluginsRelation
Dim data As PluginsRelation
Dim fs As FileStream
Try
fs = New FileStream(relationPath, FileMode.Open)
Dim bf As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
data = CType(bf.Deserialize(fs), PluginsRelation)
WriteToMonitor.writeDebugModeLine("Successful", "Load relation
data", System.Drawing.Color.Magenta)
Catch ex As Exception
WriteToMonitor.writeDebugModeLine("Error during load relation
data", ex.Message, System.Drawing.Color.Red)
Finally
Try
fs.Close()
Catch ex1 As Exception
End Try
End Try
Return data
End Function

''' <summary>
''' save operation for relation data
''' </summary>
''' <param name="data"></param>
Private Function SaveRelationData(ByVal data As PluginsRelation) As
Boolean
Dim result As Boolean = False
Dim fs As FileStream
Try
fs = New FileStream(relationPath, FileMode.OpenOrCreate)
Dim bf As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
bf.Serialize(fs, data)
WriteToMonitor.writeDebugModeLine("Successful", "Save relation
data", System.Drawing.Color.Magenta)
result = True
Catch ex As Exception
WriteToMonitor.writeDebugModeLine("Error during save relation
data", ex.Message, System.Drawing.Color.Red)
Finally
Try
fs.Close()
Catch ex1 As Exception
End Try
End Try
Return result
End Function
end class
******************************************************
Class for serialization,it's work this is code from my application

' ----------------------------------------------------
' Created on: 05/04/2003
' Created by: Gregory
'
' Name: SidCamera
' Version: 1.00
' Description:Get all inform about relationship between sidname ->
camera number
'per ptz model
' ----------------------------------------------------
' Edited last on:
' Edited last by:
'
' Added/Changed:
' 1. ??????
' 2. ??????
' 3. ??????
' ----------------------------------------------------
<Serializable()> Public Class PluginsRelation
Private objPtzModelRelationHash As Hashtable
Public Sub New()
objPtzModelRelationHash = New Hashtable
End Sub
Public Property PluginRelationsData(ByVal plugin As
Constant.Enum.ePTZModel) As cameraRelation
Get
Return objPtzModelRelationHash.Item(plugin)
End Get
Set(ByVal Value As cameraRelation)
objPtzModelRelationHash.Item(plugin) = Value
End Set
End Property

Public Property PluginsData() As Hashtable
Get
Return objPtzModelRelationHash
End Get
Set(ByVal Value As Hashtable)
objPtzModelRelationHash = Value
End Set
End Property
End Class

<Serializable()> Public Class cameraRelation
Private objSidNameToSidIndexHash As Hashtable
Private objCamerasArray As clsCamera()
Private pluginID As Constant.Enum.ePTZModel

Public Property Plugin() As Constant.Enum.ePTZModel
Get
Return pluginID
End Get
Set(ByVal Value As Constant.Enum.ePTZModel)
pluginID = Value
End Set
End Property
Public Sub New(ByVal count As Integer)
objSidNameToSidIndexHash = Hashtable.Synchronized(New Hashtable)
ReDim objCamerasArray(count - 1)
End Sub
'*******************************************************************
' created by: Gregory
'input camera number, sidname
' adding camera to hashtable for building relationship between
' sid -> physical camera number
' History:
' 1.
' 2.
'********************************************************************

Public Overloads Sub AddCamera(ByVal Sid As String, ByVal
cameraNumber As Int32)
Try
Dim index As Integer
Dim camera As clsCamera
Try
If objSidNameToSidIndexHash.ContainsKey(Sid) Then
index = objSidNameToSidIndexHash.Item(Sid)
camera = New clsCamera
With camera
.Camera = cameraNumber
.Index = index
.Name = Sid
End With
objCamerasArray(index) = camera
End If
Catch ex As Exception

End Try
Catch ex As Exception
ExceptionManagement.ExceptionManager.Publish(ex)
End Try
End Sub

Public Overloads Sub AddCamera(ByVal SidName As String, ByVal
sidIndex As Integer, ByVal cameraNumber As Int32)
Try
Dim index As Integer
Dim camera As clsCamera
Try
If objSidNameToSidIndexHash.ContainsKey(SidName) Then
camera = New clsCamera
With camera
.Camera = cameraNumber
.Index = sidIndex
.Name = SidName
End With
objCamerasArray(sidIndex) = camera
objSidNameToSidIndexHash.Item(SidName) = sidIndex
End If
Catch ex As Exception

End Try
Catch ex As Exception
ExceptionManagement.ExceptionManager.Publish(ex)
End Try
End Sub

Public Sub RemoveCamera(ByVal SidName As String)
Try
Dim index As Integer
Try
If objSidNameToSidIndexHash.ContainsKey(SidName) Then
index = objSidNameToSidIndexHash.Item(SidName)
objCamerasArray(index) = Nothing
End If
Catch ex As Exception
End Try
Catch ex As Exception
ExceptionManagement.ExceptionManager.Publish(ex)
End Try
End Sub

'*******************************************************************
' created by: Gregory
'sid object
' getting physical camera from hashtable
' sid -> physical camera number
' History:
' 1.
' 2.
'********************************************************************
Public Overloads Function getCameraNumber(ByVal sid As Integer) As
Integer
Dim cameraNumber As Integer = -1
Dim camera As clsCamera
Try
camera = objCamerasArray(sid)
If Not camera Is Nothing Then
cameraNumber = camera.Camera
End If
Catch ex As Exception
ExceptionManagement.ExceptionManager.Publish(ex)
End Try
Return cameraNumber
End Function

Public Overloads Function getCameraNumber(ByVal sid As String) As
Integer
Dim cameraNumber = -1, index As Integer
Dim camera As clsCamera
Try
If objSidNameToSidIndexHash.ContainsKey(sid) Then
index = objSidNameToSidIndexHash.Item(sid)
camera = objCamerasArray(index)
If Not camera Is Nothing Then
cameraNumber = camera.Camera
End If
End If
Catch ex As Exception
ExceptionManagement.ExceptionManager.Publish(ex)
End Try
Return cameraNumber
End Function

Public Property SidNameToSidIndex() As Hashtable
Get
Return objSidNameToSidIndexHash
End Get
Set(ByVal Value As Hashtable)
objSidNameToSidIndexHash = Value
End Set
End Property

Public Property getCameras() As clsCamera()
Get
Return objCamerasArray
End Get
Set(ByVal Value As clsCamera())
objCamerasArray = Value
End Set
End Property
End Class

<Serializable()> Public Class clsCamera
Private sidIndex As Integer
Private cameraNumber As Integer
Private cameraName As String

Public Sub New()
End Sub
Public Property Index() As Integer
Get
Return sidIndex
End Get
Set(ByVal Value As Integer)
sidIndex = Value
End Set
End Property

Public Property Camera() As Integer
Get
Return cameraNumber
End Get
Set(ByVal Value As Integer)
cameraNumber = Value
End Set
End Property

Public Property Name() As String
Get
Return cameraName
End Get
Set(ByVal Value As String)
cameraName = Value
End Set
End Property
End Class
 
O

Old Krot

And this variant use xml serialization
Private Function SaveRelationData(ByVal data As PluginsRelation) As
Boolean
Dim result As Boolean = False
Dim fs As FileStream
Try
fs = New FileStream(relationPath, FileMode.OpenOrCreate)
'Dim bf As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim bf As New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
bf.Serialize(fs, data)
WriteToMonitor.writeDebugModeLine("Successful", "Save relation
data", System.Drawing.Color.Magenta)
result = True
Catch ex As Exception
WriteToMonitor.writeDebugModeLine("Error during save relation
data", ex.Message, System.Drawing.Color.Red)
Finally
Try
fs.Close()
Catch ex1 As Exception
End Try
End Try
Return result
End Function

Reagards OldKrot
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top