|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Problem with HashtablePublic 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 "Bernard Bourée" <bern***@bouree.net> schrieb First, you should switch Option Strict On and remove the errors.> I have the following code > [...] Armin 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 >
Show quote
Hide quote
"Bernard Bourée" <bern***@bouree.net> schrieb ....but to answer the question:>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) <==================== 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 Zingler a écrit :
Show quoteHide quote > "Bernard Bourée" <bern***@bouree.net> schrieb Thanks a lot.>> 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 Bernard
Is it worth while in developing VB.NET or C#.NET
tranferring data from Access to SQL Server Express USB Pen Drive Detection - Retrieving Dates are Evil! HELP! Baffeled by Treeview Nodes Index!!!!! crazy UserControl Class in a module or class in a class? Multiple references in the same project. Very Unpredictable ListBox Behaviour Adding New member to TextBox.. |
|||||||||||||||||||||||