473,385 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Invalid cast with IDictionary

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

Imports ConsoleApplication1.FruitDetails

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("Banana", FruitColor.yellow)

Detail.FruitInfo = Fruit

Console.Write(GetFruit(Detail))

End Sub

Public Function GetFruit(ByVal FruitInformation As FruitDetails) As
String

Try
For Each Fruit As DictionaryEntry In FruitInformation.FruitInfo
'specific cast is not valid
Return String.Concat("This ", Fruit.Key, "Is ", Fruit.Value)
Next
Catch ex As Exception
Console.Write(ex.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.Entry?

Thanks

Jun 27 '08 #1
2 1661
With generic Dictionary, the type of iterated item is

"KeyValuePair<(Of <(TKey, TValue>)>) Generic Structure"
http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"D Browne" <go******@community.nospamwrote in message
news:76**********************************@microsof t.com...
>I think this should be a very simple test but I can't figure out what the
problem is:

Imports ConsoleApplication1.FruitDetails

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("Banana", FruitColor.yellow)

Detail.FruitInfo = Fruit

Console.Write(GetFruit(Detail))

End Sub

Public Function GetFruit(ByVal FruitInformation As FruitDetails) As
String

Try
For Each Fruit As DictionaryEntry In FruitInformation.FruitInfo
'specific cast is not valid
Return String.Concat("This ", Fruit.Key, "Is ",
Fruit.Value)
Next
Catch ex As Exception
Console.Write(ex.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.Entry?

Thanks

Jun 27 '08 #2
Yes that was the issue. I don't know how I missed that! Thanks.
"Teemu Keiski" <jo****@aspalliance.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
With generic Dictionary, the type of iterated item is

"KeyValuePair<(Of <(TKey, TValue>)>) Generic Structure"
http://msdn.microsoft.com/en-us/library/5tbh8a42.aspx

--
Teemu Keiski
AspInsider, ASP.NET MVP
http://blogs.aspadvice.com/joteke
http://teemukeiski.net
"D Browne" <go******@community.nospamwrote in message
news:76**********************************@microsof t.com...
>>I think this should be a very simple test but I can't figure out what the
problem is:

Imports ConsoleApplication1.FruitDetails

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("Banana", FruitColor.yellow)

Detail.FruitInfo = Fruit

Console.Write(GetFruit(Detail))

End Sub

Public Function GetFruit(ByVal FruitInformation As FruitDetails) As
String

Try
For Each Fruit As DictionaryEntry In
FruitInformation.FruitInfo 'specific cast is not valid
Return String.Concat("This ", Fruit.Key, "Is ",
Fruit.Value)
Next
Catch ex As Exception
Console.Write(ex.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.Entry?

Thanks

Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sanjay Vyas | last post by:
Sorry, forgot to cross post this one.. This is rather unusual as we would expect any Collection class to implement ICollection interface and furthermore a Dictionary class should implement...
4
by: John C | last post by:
I'm new to C#, so just point me at the correct reference material if this question has been answered before. When creating a new class which implements the IDictionary interface, two versions of...
5
by: Nick Flandry | last post by:
I'm running into an Invalid Cast Exception on an ASP.NET application that runs fine in my development environment (Win2K server running IIS 5) and a test environment (also Win2K server running IIS...
3
by: John Howard | last post by:
Making the following call to a local MSAccess database works fine: Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) Dim intRows As Integer Dim strSQL As String Dim ds As New...
1
by: Hifni Shahzard | last post by:
Hi, I got a stored procedure, where it returns a value. But if I execute it. It gives an error as "Invalid cast from System.Int32 to System.Byte.". To make clear how do I execute this, below I'm...
1
by: John | last post by:
I have a collection of properties from a WMI query and I want to recreate the collection and exclude duplicates and then enumerate. In VBScript I used the "Scripting.Dictionary" object and the...
4
by: Zark3 | last post by:
Hi all, I was wondering if anybody could enlighten me on the possibility of dynamic casting. Or, well, whether or not I'm actually trying to do this the right way. What I have is a base class...
10
by: J055 | last post by:
Hi I have an OrderedDictionary object where the key is an enum. Is there an easy way to cast it to an integer? Examples/document appreciated. Thanks Andrew
1
by: pierre.k | last post by:
Hello, how can I do a cast like IDictionary<int, stringas IDictionary<object, string ? Thanks in advance for your help, pierre.k
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.