Write the contents of a dictionary to a message box

sizo

Member
Joined
Jun 17, 2011
Messages
5
Programming Experience
Beginner
Hi,

I'm struggling to write the contents of a dictionary to a message box. Ideally I'd like to write the contents to an XML file, but I'm having difficulty even writing the contents to a message box!

Im using basic code to get me started....

Dim users As Generic.Dictionary(Of Integer, String)

Dim x As New Dictionary(Of Integer, Users)
x.Add("1", New Users("1", "Joe"))
For Each item In x
MsgBox(item)
Next

I have a class created called Users with the required code stubs. But when i try to run the code i get the following error message...

Argument 'Prompt' cannot be converted to type 'String'.

I have tried writing to string but i still get the same error message.

Any help/guidance would be much appreciated!

Many Thanks
 
What exactly are you expecting to see in the message box? Each item in a Dictionary(Of TKey, TValue) is a NameValuePair(Of TKey, TValue). With that code you are assuming that that object, which contains both the key and the value, will be implicitly converted into a String, which it won't. If you had Option Strict On, which everyone should really, then that code wouldn't even compile. The item has both Key and Value properties, so if you want to display both of them then you should get both of them and then format them into a single String, which you then display in the message box, preferably with MessageBox.Show rather than MsgBox.
 
Hi and thanks for the reply! I'm pretty new to using dictionaries but i was told its what i should be using.

Basically i have a list of attributes of computer users in a dictionary (User name, Computer name etc). All I'm trying to do is to eventually write the contents of the dictionary to an xml document. However i struggling to format each entry into a single string.

Would you be able to point me in the right direction of something i can research that will help me out?

Thanks
 
Would you be able to point me in the right direction of something i can research that will help me out?
I thought that I already had.
Each item in a Dictionary(Of TKey, TValue) is a NameValuePair(Of TKey, TValue).
That should have been KeyValuePair though. Sorry about that.
 
Back
Top