473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problem with Hashtable

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
Mar 4 '06 #1
4 899
"Bernard Bourée" <be*****@bouree.net> schrieb
I have the following code
[...]


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

Mar 4 '06 #2
"Bernard Bourée" <be*****@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

Mar 4 '06 #3
Thanks for the advise.
It is the case now but still have the same problem!!
Bernard
Armin Zingler a écrit :
"Bernard Bourée" <be*****@bouree.net> schrieb
I have the following code
[...]


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

Mar 4 '06 #4
Armin Zingler a écrit :
"Bernard Bourée" <be*****@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
Mar 4 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ricardo Pereira | last post by:
Hi, I wrote a simple program to test the "out-of-the-box" functionalities of the CMAB (Configuration Management Application Block). After having added 2 values to an hashtable, and having written...
1
by: Timo Qvist | last post by:
Hi, I'm a bit new to STL and really new to SGI's hash_set implementation and I've having problem instantiating a hash_set with a custom hash function, I could really use some help sifting through...
9
by: Billy Patton | last post by:
First, I'm not a student looking for help. Although there's nothing wrong with studens looking for help here. I'm learning c++ move further away from the perl wars here at work (my version is...
3
by: Ronin | last post by:
Hi i am using following code which extracts information from XML file and creates an instance of class which it adds to hash table. problem is i am unable to extract information from hashtable :...
2
by: Marek | last post by:
I am having a problem with Hashtable used in XML web server. I have a WebMethod where clients get registered and added into hashtable. Later, I call another web method which has client ID as...
3
by: julien | last post by:
Hello, I'm sorry to send another message about the custom assemblies. The examples I find online don't help to find out what is wrong in my file. In a dll: This custom attribute: public...
1
by: Wee Bubba | last post by:
i have a class. within the constructor for this class it tests if 2 session objects exist and if they dont it creates them. this is a one off deal. The session objects are both Hashtables. ...
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
6
by: stoogots2 | last post by:
am using Visual Studio 2003, .Net Framework 1.1, C#. I get a SystemNullReferenceException when trying to do a hashtable.add(string,string) from a login page, and I do not understand why because all...
9
by: raylopez99 | last post by:
Hello all— I’m trying to get the below to work and cannot get the format right. It’s from this example: http://msdn.microsoft.com/en-us/library/8627sbea(VS.71).aspx What it is: I’m trying...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.