Problem with Hashtable

  • Thread starter =?ISO-8859-1?Q?Bernard_Bour=E9e?=
  • Start date
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

I have the following code


Public Marchés As New Hashtable
Dim sMar() As String = {"EuronextA", "EuronextB", "EuronextC", "Indices"}

'A class Marché is filled with the above values
Dim Mar As Marché 'DEFINED IN A CLASS COPIED AT THE END
For i = 0 To sMar.Length - 1
Mar = New Marché
Mar.Nom = sMar(i) : Mar.Num = i
Marchés.Add(Mar.Num, Mar)
Next

*'Then I want to retrieve these values one by one but have an error on
CType function*
Dim oMarché As IDictionaryEnumerator = Marchés.GetEnumerator
While oMarché.MoveNext
frmAB.DefInstance.pb2.Value = oMarché.Key + 1
Dim Mar As New Marché
Mar.Nom = CType(oMarché.Value, String) <==================
Mar.Num = CType(oMarché.Key, Short) <====================

End While


Thanks for your help

Bernard
=================================================================================
Option Explicit On
Friend Class Marché
Private mNom As String
Private mNum As Short

Public Property Num() As Short
Get
Return mNum
End Get
Set(ByVal Value As Short)
mNum = Value
End Set
End Property

Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal Value As String)
mNom = Value
End Set
End Property

Public Sub New()
MyBase.New()
End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize()
End Sub
End Class
 
A

Armin Zingler

Bernard Bourée said:
I have the following code


Public Marchés As New Hashtable
Dim sMar() As String = {"EuronextA", "EuronextB", "EuronextC", "Indices"}

'A class Marché is filled with the above values
Dim Mar As Marché 'DEFINED IN A CLASS COPIED AT THE END
For i = 0 To sMar.Length - 1
Mar = New Marché
Mar.Nom = sMar(i) : Mar.Num = i
Marchés.Add(Mar.Num, Mar)
Next

*'Then I want to retrieve these values one by one but have an error on
CType function*
Dim oMarché As IDictionaryEnumerator = Marchés.GetEnumerator
While oMarché.MoveNext
frmAB.DefInstance.pb2.Value = oMarché.Key + 1
Dim Mar As New Marché
Mar.Nom = CType(oMarché.Value, String) <==================
Mar.Num = CType(oMarché.Key, Short) <====================


....but to answer the question:

The 'Value' member of the IDictionaryEnumerator returns the item in the
Hashtable. The type of the item is 'Marché'. The type is not String.

I do not have a problem at the second line (with oMarché.Key).

If you intend to make a copy of the item in the Hashtable:

While oMarché.MoveNext
'frmAB.DefInstance.pb2.Value = oMarché.Key + 1
Dim Mar As New Marché
Dim Item As Marché

Item = DirectCast(oMarché.Value, Marché)
Mar.Nom = Item.Nom
Mar.Num = Item.Num
End While


Armin
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Thanks for the advise.
It is the case now but still have the same problem!!


Bernard


Armin Zingler a écrit :
Bernard Bourée said:
I have the following code
[...]

First, you should switch Option Strict On and remove the errors.


Armin
 
?

=?ISO-8859-1?Q?Bernard_Bour=E9e?=

Armin Zingler a écrit :
...but to answer the question:

The 'Value' member of the IDictionaryEnumerator returns the item in
the Hashtable. The type of the item is 'Marché'. The type is not String.

I do not have a problem at the second line (with oMarché.Key).

If you intend to make a copy of the item in the Hashtable:

While oMarché.MoveNext
'frmAB.DefInstance.pb2.Value = oMarché.Key + 1
Dim Mar As New Marché
Dim Item As Marché

Item = DirectCast(oMarché.Value, Marché)
Mar.Nom = Item.Nom
Mar.Num = Item.Num
End While


Armin
Armin

Thanks a lot.

Bernard
 

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

Similar Threads


Top