Invalid cast with IDictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • D Browne

    Invalid cast with IDictionary

    I think this should be a very simple test but I can't figure out what the
    problem is:

    Imports ConsoleApplicat ion1.FruitDetai ls

    Public Class FruitDetails
    Private m_fruitInfo As IDictionary(Of String, FruitColor)
    Public Property FruitInfo()
    Get
    Return m_fruitInfo
    End Get
    Set(ByVal value)
    m_fruitInfo = value
    End Set
    End Property

    Public Enum FruitColor
    orange
    yellow
    green
    End Enum
    End Class

    Module Module1

    Sub Main()
    Dim Detail As New FruitDetails

    Dim Fruit As New Dictionary(Of String, FruitColor)
    Fruit.Add("Bana na", FruitColor.yell ow)

    Detail.FruitInf o = Fruit

    Console.Write(G etFruit(Detail) )

    End Sub

    Public Function GetFruit(ByVal FruitInformatio n As FruitDetails) As
    String

    Try
    For Each Fruit As DictionaryEntry In FruitInformatio n.FruitInfo
    'specific cast is not valid
    Return String.Concat(" This ", Fruit.Key, "Is ", Fruit.Value)
    Next
    Catch ex As Exception
    Console.Write(e x.ToString)
    End Try

    End Function

    End Module


    The information I'm really looking for is the first DictionaryEntry in
    DetailLevel without having to do a For Each loop ... maybe
    IEnumerator.Ent ry?

    Thanks

  • Teemu Keiski

    #2
    Re: Invalid cast with IDictionary

    With generic Dictionary, the type of iterated item is

    "KeyValuePair<( Of <(TKey, TValue>)>) Generic Structure"


    --
    Teemu Keiski
    AspInsider, ASP.NET MVP




    "D Browne" <gordigor@commu nity.nospamwrot e in message
    news:7673FB6B-E628-473E-946E-956B2B8E9240@mi crosoft.com...
    >I think this should be a very simple test but I can't figure out what the
    >problem is:
    >
    Imports ConsoleApplicat ion1.FruitDetai ls
    >
    Public Class FruitDetails
    Private m_fruitInfo As IDictionary(Of String, FruitColor)
    Public Property FruitInfo()
    Get
    Return m_fruitInfo
    End Get
    Set(ByVal value)
    m_fruitInfo = value
    End Set
    End Property
    >
    Public Enum FruitColor
    orange
    yellow
    green
    End Enum
    End Class
    >
    Module Module1
    >
    Sub Main()
    Dim Detail As New FruitDetails
    >
    Dim Fruit As New Dictionary(Of String, FruitColor)
    Fruit.Add("Bana na", FruitColor.yell ow)
    >
    Detail.FruitInf o = Fruit
    >
    Console.Write(G etFruit(Detail) )
    >
    End Sub
    >
    Public Function GetFruit(ByVal FruitInformatio n As FruitDetails) As
    String
    >
    Try
    For Each Fruit As DictionaryEntry In FruitInformatio n.FruitInfo
    'specific cast is not valid
    Return String.Concat(" This ", Fruit.Key, "Is ",
    Fruit.Value)
    Next
    Catch ex As Exception
    Console.Write(e x.ToString)
    End Try
    >
    End Function
    >
    End Module
    >
    >
    The information I'm really looking for is the first DictionaryEntry in
    DetailLevel without having to do a For Each loop ... maybe
    IEnumerator.Ent ry?
    >
    Thanks

    Comment

    • D Browne

      #3
      Re: Invalid cast with IDictionary

      Yes that was the issue. I don't know how I missed that! Thanks.
      "Teemu Keiski" <joteke@aspalli ance.comwrote in message
      news:%23D6wqVbx IHA.5580@TK2MSF TNGP04.phx.gbl. ..
      With generic Dictionary, the type of iterated item is
      >
      "KeyValuePair<( Of <(TKey, TValue>)>) Generic Structure"

      >
      --
      Teemu Keiski
      AspInsider, ASP.NET MVP


      >
      >
      "D Browne" <gordigor@commu nity.nospamwrot e in message
      news:7673FB6B-E628-473E-946E-956B2B8E9240@mi crosoft.com...
      >>I think this should be a very simple test but I can't figure out what the
      >>problem is:
      >>
      >Imports ConsoleApplicat ion1.FruitDetai ls
      >>
      >Public Class FruitDetails
      > Private m_fruitInfo As IDictionary(Of String, FruitColor)
      > Public Property FruitInfo()
      > Get
      > Return m_fruitInfo
      > End Get
      > Set(ByVal value)
      > m_fruitInfo = value
      > End Set
      > End Property
      >>
      > Public Enum FruitColor
      > orange
      > yellow
      > green
      > End Enum
      >End Class
      >>
      >Module Module1
      >>
      > Sub Main()
      > Dim Detail As New FruitDetails
      >>
      > Dim Fruit As New Dictionary(Of String, FruitColor)
      > Fruit.Add("Bana na", FruitColor.yell ow)
      >>
      > Detail.FruitInf o = Fruit
      >>
      > Console.Write(G etFruit(Detail) )
      >>
      > End Sub
      >>
      > Public Function GetFruit(ByVal FruitInformatio n As FruitDetails) As
      >String
      >>
      > Try
      > For Each Fruit As DictionaryEntry In
      >FruitInformati on.FruitInfo 'specific cast is not valid
      > Return String.Concat(" This ", Fruit.Key, "Is ",
      >Fruit.Value)
      > Next
      > Catch ex As Exception
      > Console.Write(e x.ToString)
      > End Try
      >>
      > End Function
      >>
      >End Module
      >>
      >>
      >The information I'm really looking for is the first DictionaryEntry in
      >DetailLevel without having to do a For Each loop ... maybe
      >IEnumerator.En try?
      >>
      >Thanks
      >
      >

      Comment

      Working...