Results 1 to 6 of 6

Thread: Help with Hashtable Iteration

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    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?

  2. #2
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    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?

  4. #4
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    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.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    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...

  6. #6
    Addicted Member
    Join Date
    Feb 2002
    Location
    closed
    Posts
    196
    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
  •  



Click Here to Expand Forum to Full Width