Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Retreiving String from object dictionary

  1. #1

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    Resolved [RESOLVED] [2005] Retreiving String from object dictionary

    I have a dictionary set up like so.

    vb.net Code:
    1. Dim dict As New Dictionary(Of String, Object)
    2. dict.Add("Filename", s)
    3. 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:
    1. 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. Dim dict As Dictionary(Of String, Object) = TryCast(Me.Tag, Dictionary(Of String, Object))
    2.  
    3. If dict Is Nothing Then
    4.     MessageBox.Show("The Tag did not contain a Dictionary(Of String, Object)")
    5. Else
    6.     Dim fileName As Object = dict("Filename")
    7.  
    8.     If fileName Is Nothing Then
    9.         MessageBox.Show("No object found.")
    10.     Else
    11.         MessageBox.Show(fileName.ToString())
    12.     End If
    13. End If
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member Troy Lundin's Avatar
    Join Date
    May 2006
    Posts
    489

    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
  •  



Click Here to Expand Forum to Full Width