|
-
May 9th, 2003, 10:59 PM
#1
Thread Starter
Hyperactive Member
Help with Hashtable Iteration
Ok, I have always been able to iterate though a hashtable with the "Option Strict turned Off" but after someone suggesting I should code with it ON for good practice reasons, Now I'm running into problems Iterating though a Hashtable that I store a Class object in..
The old way worked fine..
Code:
Dim en As IDictionaryEnumerator = HashTest.GetEnumerator
With lstID
.BeginUpdate()
.Items.Clear()
While en.MoveNext
Dim R As clsTest = en.Value
.Items.Add(R.MyID)
End While
.EndUpdate()
End With
Now that I have turned Option Strict on, it bugs out, I get an error that says..
Option Strict On disallows implicit conversions from 'System.Object' to 'Hashing.clsTest'.
which applies to this:
Code:
Dim R As clsTest = en.Value
I have tried to cast it by doing
Code:
Dim R As clsTest = CType(en.Value, clsTest)
But it won't let me cast it to that class type..
Does anyone know what I need to do in order to do this?
-
May 10th, 2003, 12:19 PM
#2
Addicted Member
this any help...
Code:
Dim ht As New Hashtable()
'
ht.Add("1", New clsTest("OBJECT 1"))
ht.Add("2", New clsTest("OBJECT 2"))
'
Dim di As DictionaryEntry
Dim o As clsTest
'
For Each di In ht
o = DirectCast(di.Value, clsTest)
With Me.ListBox1
.Items.Add(o.MyID)
End With
Next
'
...PS I overloaded the constructor of your clsTest to accept some string values
-
May 10th, 2003, 06:48 PM
#3
Thread Starter
Hyperactive Member
Thanks... Funny thing is once I rebooted and tried that same code again, it worked fine... Go Figure huh....
Anyways I didn't want to start a new thread about the same topic so I want to extend on this question and add a second part to it..
Now that I got the Iteration working, I'm faced with a new problem.. I am not just storing one special class in the hashtable but instead I have 3 different classes I am storing in the hashtable..
Say I have:
Class1
Class2
Class3
Now there is no logical order they are stored in the hashtable.. so I need to know how to identify the class that I am casting when iterating through a hashtable...
so how do I identify this:
Code:
For Each di In ht
o = DirectCast(di.Value, clsTest)
With Me.ListBox1
.Items.Add(o.MyID)
End With
Next
The di.Value can be one of 3 classes so I need to figure out what "o" should be before I can cast "di.value" to it, how do I do this?
-
May 11th, 2003, 03:23 PM
#4
Addicted Member
Could use the generic o.getType.toString and a few case statements but this example cries out for a common interface shared among your three classes. That way you could cast whatever your hashtable contains to the interface type without much worry. See the revised code
Code:
Dim ht As New Hashtable()
ht.Add("1", New clsTest("CLASS TEST 1"))
ht.Add("2", New clsTest2("CLASS TEST 2"))
Dim di As DictionaryEntry
Dim o As clsCommon
For Each di In ht
o = DirectCast(di.Value, clsCommon
With Me.ListBox1
.Items.Add(o.MyId)
End With
Next
Last edited by powdir; May 12th, 2003 at 05:44 AM.
-
May 11th, 2003, 08:22 PM
#5
Thread Starter
Hyperactive Member
Could use the generic o.getType.toString and a few case statements but this example cries out for a common interface shared among your three classes.
Ok this sounds interesting, would you mind touching a bit deeper on these "interfaces"? I am a VB6 migrater and .Net is still pretty new to me.. I have heard of "interfaces" but haven't gotten my hands dirty or seen them in use for that matter.. Thanks...
-
May 12th, 2003, 04:50 AM
#6
Addicted Member
Interfaces (to a degree!) are available to the VB6 programmer - but for semantics and a bit of VB.NET syntax check out:
http://msdn.microsoft.com/library/de...interfaces.asp
i get the impression that your 3 classes have a good deal in common. If that IS the case think about using the above in your app. Try and factor the methods/properties that are shared to an interface, but don't shoehorn classes to fit with an interface if they clearly dont belong.
I s'pose the concept is similar to using one of those 'all in one' TV/satellite/stereo remotes in that it [the remote control] provides a common set of buttons to operate on the various devices.
HTH
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|