|
-
Aug 7th, 2009, 11:00 AM
#1
Thread Starter
New Member
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:
For Each tKey As DictionaryEntry In Result0.Properties()
TBoxOutput.AppendText(tKey.Key.ToString() & vbNewLine)
For Each mystring As String In tKey.Value
TBoxOutput.AppendText(mystring & vbNewLine)
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
-
Aug 7th, 2009, 11:33 AM
#2
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:
For Each tKey As DictionaryEntry In Result0.Properties() If TypeOf tKey.Value Is String() Then For Each mystring As String In CType(tKey.Value, String()) 'Do stuff Next End If Next
-
Aug 7th, 2009, 12:42 PM
#3
Thread Starter
New Member
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.
-
Aug 7th, 2009, 01:15 PM
#4
Thread Starter
New Member
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:
For Each tKey As DictionaryEntry In Result0.Properties()
For Each mytype In tKey.Value
'MessageBox.Show(mytype.GetType().ToString())
If (mytype.GetType().ToString() = System.String) Then
'do string stuff
Else if (mytype.GetType().ToString() = System.Boolean()) Then
'do array stuff
else if....
End If
Next
Next
-
Aug 7th, 2009, 01:37 PM
#5
Thread Starter
New Member
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:
For Each tKey As DictionaryEntry In Result0.Properties()
TBoxOutput.AppendText(tKey.Key.ToString() & " ")
For Each mytype In tKey.Value
if mytype.getType().tostring() = "System.String" Then
TBoxOutput.AppendText(mytype)
Else If mytype.getType().tostring() = "system.boolean()" Then
'do stuff
End If
Next
Next
-
Aug 7th, 2009, 01:51 PM
#6
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:
For Each tKey As DictionaryEntry In Result0.Properties() Select Case True Case tKey.Value Is GetType(String) 'Write string, don't loop through a string because that iterates through it's character array 'Unless that's what you want 'Write tKey.Value.ToString() Case tKey.Value Is GetType(Boolean()) For Each value As Boolean In DirectCast(tKey.Value, Boolean()) 'Write value.ToString() Next Case Else 'Unhandled type End Select Next
I also recommend turning Option Strict On because the IDE will not let you start looping through an Object type.
-
Aug 7th, 2009, 03:02 PM
#7
Thread Starter
New Member
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.
-
Aug 7th, 2009, 03:18 PM
#8
Thread Starter
New Member
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.
-
Aug 7th, 2009, 04:01 PM
#9
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:
For Each tKey As DictionaryEntry In Result0.Properties()
If tKey.Value.GetType.GetInterface("IEnumerable") IsNot Nothing Then
'The value implements IEnumerable interface. It can be a list, collection, array etc.
'Loop through it and write its items.
For Each obj As Object In tKey.Value
'Write obj.ToString()
Next
Else
'The value doesn't implement IEnumerable interface. It is safe to write it.
'Unless that's what you want
'Write tKey.Value.ToString()
End If
Next
Last edited by VBDT; Aug 7th, 2009 at 04:20 PM.
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
|