|
-
Jun 14th, 2007, 11:43 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] Retreiving String from object dictionary
I have a dictionary set up like so.
vb.net Code:
Dim dict As New Dictionary(Of String, Object)
dict.Add("Filename", s)
dict.Add("Type", CType(id(3), FileType))
I then store the dictionary into a dynamically created form's Tag property.
I have tried to extract the "Filename" (which is a string) by using:
vb.net Code:
CType(Me.Tag, Dictionary(Of String, Object))("Filename").ToString
I get the error: Cannot convert Dictionary(Of String, Object) to String.
I am not trying to convert the dictionary, just the "Filename" entry.
Any idea of how to do this? Thanks, Troy.
Prefix has no suffix, but suffix has a prefix.
-
Jun 14th, 2007, 11:48 PM
#2
Re: [2005] Retreiving String from object dictionary
Why are you using the Tag property at all? Just assign the Dictionary to a private field. If you need to expose it outside the form too then create a public read-only property of the appropriate type.
Also, don't use CType when you can use DirectCast.
-
Jun 15th, 2007, 01:11 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] Retreiving String from object dictionary
I am using Tag because the form is created dynamically from a dll that is loaded. The dll is not referenced by the main application, and the form is different for each dll.
I had tried DirectCast first, then tried CType.
Prefix has no suffix, but suffix has a prefix.
-
Jun 15th, 2007, 01:49 AM
#4
Re: [2005] Retreiving String from object dictionary
You're using 'Me.Tag' so you're accessing it from inside the form class. If the form itself knows that its Tag will contain a Dictionary then why can't the form itself declare a field/property pair specifically for that Dictionary?
That aside, whenever a long expression like that fails the first thing to do is to break it up to establish exactly where the issue actually is, e.g.:
vb.net Code:
Dim dict As Dictionary(Of String, Object) = TryCast(Me.Tag, Dictionary(Of String, Object))
If dict Is Nothing Then
MessageBox.Show("The Tag did not contain a Dictionary(Of String, Object)")
Else
Dim fileName As Object = dict("Filename")
If fileName Is Nothing Then
MessageBox.Show("No object found.")
Else
MessageBox.Show(fileName.ToString())
End If
End If
-
Jun 15th, 2007, 12:43 PM
#5
Thread Starter
Hyperactive Member
Re: [2005] Retreiving String from object dictionary
Thanks.
I had figured it out last night, but when I built the dll I forgot to put it in the right folder so my other program didn't read the newly built dll.
Prefix has no suffix, but suffix has a prefix.
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
|