469,331 Members | 6,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

Serialize class with hashtable

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.
Nov 21 '05 #1
5 2511
"Dick" <Di**@nospam.com> wrote in message news:<eG**************@TK2MSFTNGP10.phx.gbl>...
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
<Serializable()> Class clsOptions private countID As Integer
private persons As Hashtable public sub new
persons =new Hashtable

end sub End Class

Nov 21 '05 #2
Thanks a lot!
I will try it.

"Oldkrot" <ol*******@hotmail.com> wrote in message
news:3c**************************@posting.google.c om...
"Dick" <Di**@nospam.com> wrote in message

news:<eG**************@TK2MSFTNGP10.phx.gbl>...
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
<Serializable()> Class clsOptions
private countID As Integer
private persons As Hashtable

public sub new
persons =new Hashtable

end sub
End Class

Nov 21 '05 #3
It din't work like that.

Find a solution though:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

"Oldkrot" <ol*******@hotmail.com> wrote in message
news:3c**************************@posting.google.c om...
"Dick" <Di**@nospam.com> wrote in message

news:<eG**************@TK2MSFTNGP10.phx.gbl>...
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
<Serializable()> Class clsOptions
private countID As Integer
private persons As Hashtable

public sub new
persons =new Hashtable

end sub
End Class

Nov 21 '05 #4


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.Bin aryFormatter
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.Bin aryFormatter
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #5
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.Bin aryFormatter
Dim bf As New
System.Runtime.Serialization.Formatters.Soap.SoapF ormatter
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by David Sworder | last post: by
reply views Thread by Arjen | last post: by
2 posts views Thread by Diego F. | last post: by
2 posts views Thread by MichiSu11 | last post: by
1 post views Thread by Bill L | last post: by
1 post views Thread by Raed Sawalha | last post: by
7 posts views Thread by Ben Amada | last post: by
6 posts views Thread by Joe | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.