Results 1 to 9 of 9

Thread: Reading DictionaryEntry problem

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Reading DictionaryEntry problem

    I have a collection with DictionaryEntires. The below code enumerates all of the values as long as they are strings. How can it be modified so it does not throw and error when it runs across other vaule types like binary()?

    vb.net 2008 Express Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()
    2.                 TBoxOutput.AppendText(tKey.Key.ToString() & vbNewLine)
    3.                 For Each mystring As String In tKey.Value
    4.                     TBoxOutput.AppendText(mystring & vbNewLine)
    5.                 Next

    PS: I will not know what the contents of the DictionaryEntry will be.

    ty for reading and thinking

    windowwasher80
    Last edited by windowwasher80; Aug 7th, 2009 at 03:16 PM. Reason: answer given

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Reading DictionaryEntry problem

    Your problem is happening because your code assumes that the Value property of tKey is a String Array. Validate before you run the second For Loop, ie:

    vb.net Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()
    2.      If TypeOf tKey.Value Is String() Then
    3.           For Each mystring As String In CType(tKey.Value, String())
    4.               'Do stuff
    5.           Next
    6.      End If
    7. Next

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Reading DictionaryEntry problem

    It did not work. the code ended up skipping every tkey.value even if it was a string
    Last edited by windowwasher80; Aug 7th, 2009 at 12:57 PM.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Reading DictionaryEntry problem

    Getting closer with "getType()".....will post back when I get a chance to look at it again....unless of course someone knows the answer off of the top of their heads.
    vb.net express 2008 Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()                
    2.     For Each mytype In tKey.Value
    3.                     'MessageBox.Show(mytype.GetType().ToString())
    4.                     If (mytype.GetType().ToString() = System.String) Then
    5.                         'do string stuff
    6.                     Else if (mytype.GetType().ToString() = System.Boolean()) Then
    7.                         'do array stuff
    8.                     else if....
    9.                     End If
    10.                        
    11.                 Next
    12. Next

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Reading DictionaryEntry problem

    Found it....if anyone has a better way please post. For those that are/were letting me pick their brains...thanks:

    vb.net Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()
    2.     TBoxOutput.AppendText(tKey.Key.ToString() & " ")
    3.  
    4.      For Each mytype In tKey.Value
    5.          if mytype.getType().tostring() = "System.String" Then
    6.              TBoxOutput.AppendText(mytype)
    7.          Else If mytype.getType().tostring() = "system.boolean()" Then
    8.              'do stuff
    9.          End If
    10.      Next
    11. Next

  6. #6
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Reading DictionaryEntry problem

    You can't just start iterating through the tKey.Value when you don't know what it is. You need to validate BEFORE you start looping. A For Each statement requires that the object implements IEnumerable. In both of those cases you are safe because both types will allow a For Each to be run on them, but if you get something like an Integer as the value, your code will throw an exception.

    Your code should look something like this:

    vb.net Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()
    2.             Select Case True
    3.                 Case tKey.Value Is GetType(String)
    4.                     'Write string, don't loop through a string because that iterates through it's character array
    5.                     'Unless that's what you want
    6.                     'Write tKey.Value.ToString()
    7.                 Case tKey.Value Is GetType(Boolean())
    8.                     For Each value As Boolean In DirectCast(tKey.Value, Boolean())
    9.                         'Write value.ToString()
    10.                     Next
    11.                 Case Else
    12.                     'Unhandled type
    13.             End Select
    14.         Next

    I also recommend turning Option Strict On because the IDE will not let you start looping through an Object type.

  7. #7

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Reading DictionaryEntry problem

    Thanks!

    Items learned from thread/background work to understand stuff in thread:

    Option Strict: I was wondering why VB.Net was letting me do stuff the lazy way.

    DirectCast vs Ctype: DC is twice as fast as Ctype BUT can only be used when object contains type that is being converted to.

    and all that other good stuff in the thread.

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2009
    Posts
    6

    Re: Reading DictionaryEntry problem

    arrg!....another thing I learned....don't assume the code works until it is fully tested. OK I already knew that one but I have a couple of beers in me and it is Friday afternoon.

    When I use the last piece of code posted I get no results. My Case statements are not resolving as true.....Reason: The values are actually collections....so I will have to go to another for loop I think.

    will post back to thread next week.....it is close though. I can feel it.

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Reading DictionaryEntry problem

    You can check to see if the value implements IEnumerable interface. If it does, than loop through the items. Here is an example:
    vb Code:
    1. For Each tKey As DictionaryEntry In Result0.Properties()
    2.     If tKey.Value.GetType.GetInterface("IEnumerable") IsNot Nothing Then
    3.         'The value implements IEnumerable interface. It can be a list, collection, array etc.
    4.         'Loop through it and write its items.
    5.         For Each obj As Object In tKey.Value
    6.             'Write obj.ToString()                    
    7.         Next
    8.     Else
    9.         'The value doesn't implement IEnumerable interface. It is safe to write it.
    10.         'Unless that's what you want                    
    11.         'Write tKey.Value.ToString()
    12.     End If
    13. Next

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