Home All Groups Group Topic Archive Search About

Problem with Hashtable

Author
4 Mar 2006 11:14 AM
Bernard_Bourée
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

Author
4 Mar 2006 11:55 AM
Armin Zingler
"Bernard Bourée" <bern***@bouree.net> schrieb
> I have the following code
> [...]

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


Armin
Author
4 Mar 2006 12:29 PM
Bernard_Bourée
Thanks for the advise.
It is the case now but still have the same problem!!


Bernard


Armin Zingler a écrit :
Show quoteHide quote
> "Bernard Bourée" <bern***@bouree.net> schrieb
>> I have the following code
>> [...]
>
> First, you should switch Option Strict On and remove the errors.
>
>
> Armin
>
Author
4 Mar 2006 12:12 PM
Armin Zingler
Show quote Hide quote
"Bernard Bourée" <bern***@bouree.net> schrieb
>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
Author
4 Mar 2006 12:44 PM
Bernard_Bourée
Armin Zingler a écrit :
Show quoteHide quote
> "Bernard Bourée" <bern***@bouree.net> schrieb
>> 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
Armin

Thanks a lot.

Bernard