KeyValuePair and index

  • Thread starter Thread starter JAG
  • Start date Start date
J

JAG

I'm missing something with regard to navigating a key/value pair
collection by index and can't figure out what it is. The following
code works correctly, allowing me to navigate through the collection
one item at a time using the index (idx) and I was hoping to do the
same for a key/value pair collection but the index behaves a bit
differently:
Private myLink As New List(Of String)
Private idx As Integer = -1
Private count As Integer = 0

Private Function setLink(ByVal currDoc As String)
myLink.Add(currDoc)
count = count + 1
idx = idx + 1
End Function

Private Sub Back_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Back.Click
If idx > 0 Then
idx = idx - 1
ShowImage(myLink(idx))
End If
End Sub

Public Sub ShowImage(ByVal currImage As String)
PictureBox1.Image = Nothing
Dim MyImage As Bitmap
MyImage = New Bitmap(currImage)
setLink(currImage)
PictureBox1.Image = CType(MyImage, Image)
PictureBox1.Show()
End Sub

In the key/value pair collection, the index seems to return a
number(value of idx) of characters for an item in the collection:
Private myLink As New List(Of KeyValuePair(Of String, String))

Why does the index work for List(Of T) and not for List(Of
KeyValuePair)?
Thanks for your input,
Matt
 
JAG,

Use instead of your list a generic dictionary, the text and sample on this
page seems to me seeing your current code enough to get it done.

http://msdn.microsoft.com/en-us/library/xfhwa508.aspx

Be aware it uses in fact a KeyValuePair

Success,

Cor


"JAG" wrote in message

I'm missing something with regard to navigating a key/value pair
collection by index and can't figure out what it is. The following
code works correctly, allowing me to navigate through the collection
one item at a time using the index (idx) and I was hoping to do the
same for a key/value pair collection but the index behaves a bit
differently:
Private myLink As New List(Of String)
Private idx As Integer = -1
Private count As Integer = 0

Private Function setLink(ByVal currDoc As String)
myLink.Add(currDoc)
count = count + 1
idx = idx + 1
End Function

Private Sub Back_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Back.Click
If idx > 0 Then
idx = idx - 1
ShowImage(myLink(idx))
End If
End Sub

Public Sub ShowImage(ByVal currImage As String)
PictureBox1.Image = Nothing
Dim MyImage As Bitmap
MyImage = New Bitmap(currImage)
setLink(currImage)
PictureBox1.Image = CType(MyImage, Image)
PictureBox1.Show()
End Sub

In the key/value pair collection, the index seems to return a
number(value of idx) of characters for an item in the collection:
Private myLink As New List(Of KeyValuePair(Of String, String))

Why does the index work for List(Of T) and not for List(Of
KeyValuePair)?
Thanks for your input,
Matt
 
Cor-
Thanks for the prompt advice.

I have implemented a generic dictionary as you have suggested. For
whatever reason my index didn't work with the generic dictionary
either, giving me the dreaded "The given key was not present in the
dictionary" while using my index.

Instead, I am using 2 separate lists (Of T) and am able to achieve the
desired results, though I'm still testing.

My question remains however. Why doesn't the index work for
KeyValuePair or dictionary as it does for List(Of T)? I'm still
looking for the answer.

Thanks again for your help.
Matt
 
Am 13.10.2010 23:27, schrieb JAG:
In the key/value pair collection, the index seems to return a

Can you show us an example of how you try to use the index?

In the following example, the msgbox shows "4" as expected:

Dim Links As New List(Of KeyValuePair(Of String, String))

Links.Add(New KeyValuePair(Of String, String)("key 0", "value 0"))
Links.Add(New KeyValuePair(Of String, String)("key 1", "value 1"))
Links.Add(New KeyValuePair(Of String, String)("key 2", "value 2"))
Links.Add(New KeyValuePair(Of String, String)("key 3", "value 3"))
Links.Add(New KeyValuePair(Of String, String)("key 4", "value 4"))
Links.Add(New KeyValuePair(Of String, String)("key 5", "value 5"))
Links.Add(New KeyValuePair(Of String, String)("key 6", "value 6"))

MsgBox(Links(4).Value)
 
Armin-
Have a look at my original post for my usage of index. If you need
further context let me know. I used the same implementation for both
dictionary and KeyValuePair.

I'd rather maintain a single collection than my current approach of
using multiple collections, and I have to believe I'm either doing
something wrong or missing something.

I am a VB.Net beginner and welcome any criticism.

Thanks,
Matt
 
Am 14.10.2010 16:45, schrieb JAG:
Armin-
Have a look at my original post for my usage of index. If you need
further context let me know. I used the same implementation for both
dictionary and KeyValuePair.

I did look at your first post, but there you only use a List(Of String),
and you mention a List(Of KeyValuePair(Of String, String)), but I still
don't know why an index doesn't work with the latter.
 
I use this,

Private ListOfImages as New Dictionary(of Integer,Bitmap)

Works great

if it is filled it returns a bitmap like this

PictureBox1.Image = ListOfImages(100) 'which returns the value of the value
pair with the key 100

So I don't know why it is not working for you, you give as only information
that it didn't work.

I had not any idea what I should answer on that, I had another experience.

Cor

"JAG" wrote in message

Cor-
Thanks for the prompt advice.

I have implemented a generic dictionary as you have suggested. For
whatever reason my index didn't work with the generic dictionary
either, giving me the dreaded "The given key was not present in the
dictionary" while using my index.

Instead, I am using 2 separate lists (Of T) and am able to achieve the
desired results, though I'm still testing.

My question remains however. Why doesn't the index work for
KeyValuePair or dictionary as it does for List(Of T)? I'm still
looking for the answer.

Thanks again for your help.
Matt
 
Thanks for the continued help.

Cor, I appreciate the suggestion and will remember that when I'm
dealing only with bitmaps. I'm presently handling multiple file types.

Following is an example using dictionary that doesn't return key/value
by index. Apologize for not providing it sooner. Assuming I've got a
"good" collection, I'm not asking for the data properly. Right?

Public Class Form1
Dim myLink As New Dictionary(Of String, String)
Private idx As Integer = -1
Private count As Integer = 0
Private Function setLink(ByVal currDoc As String, ByVal myNodeTag
As String)
myLink.Add(currDoc, myNodeTag)
count = count + 1
idx = idx + 1
End Function

Private Sub Back_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Back.Click
If idx > 0 Then
idx = idx - 1
MsgBox(myLink(idx))
End If
End Sub

Public Sub ShowImage(ByVal currImage As String, ByVal myNodeTag As
String)
PictureBox1.Image = Nothing
Dim MyImage As Bitmap
MyImage = New Bitmap(currImage)
setLink(currImage, myNodeTag)
PictureBox1.Image = CType(MyImage, Image)
PictureBox1.Show()
End Sub

Private Sub pic1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic1.Click
ShowImage("image1.jpg", "Image 1")
End Sub

Private Sub pic2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic2.Click
ShowImage("image2.jpg", "Image 2")
End Sub

Private Sub pic3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic3.Click
ShowImage("image3.jpg", "Image 3")
End Sub
End Class



Matt
 
Am 15.10.2010 16:30, schrieb JAG:
Following is an example using dictionary that doesn't return key/value
by index. Apologize for not providing it sooner. Assuming I've got a
"good" collection, I'm not asking for the data properly. Right?

Enable Option Strict. After fixing the compile errors, we'll have
a look again.
 
I have enabled Option Strict and have fixed all but one compile error.
I have a feeling that's the one most needing attention:
MsgBox(myLink(idx))

Is this a type conversion issue?

Thanks for your continued suggestions. I'm anxious to get to the
bottom of this.

Matt
 
Am 18.10.2010 00:54, schrieb JAG:
I have enabled Option Strict and have fixed all but one compile error.
I have a feeling that's the one most needing attention:
MsgBox(myLink(idx))

Is this a type conversion issue?

Thanks for your continued suggestions. I'm anxious to get to the
bottom of this.

The solution is not to access the item by index (Integer). You must access
the item by String because String is the type of the key.
 
I show you
Dim myLink as new Dictonary(of int, of Bitmap)
And you tell that
Dim mylingk as new Dictionary(of String, of String)
does not go.

I put simple the references of images from the resources in that dictionary,
the reason is that that one can not be used as a collection.

Then I can use that dictionary in any way.

In my idea are you to much looking in a way like.

I read an image, I put that in a treeView, than I want those images from my
treeview in a dictionary.

Why don't you try to think more in what data you use.

Be aware it is all about references, the use of memory is ignorable which
ever way you use.
(Beside that if you set the images in the resources it takes more memory
direct at the start of the assembly than when you load them from disk, but
that does not change the idea)

Success

Cor

"JAG" wrote in message

Thanks for the continued help.

Cor, I appreciate the suggestion and will remember that when I'm
dealing only with bitmaps. I'm presently handling multiple file types.

Following is an example using dictionary that doesn't return key/value
by index. Apologize for not providing it sooner. Assuming I've got a
"good" collection, I'm not asking for the data properly. Right?

Public Class Form1
Dim myLink As New Dictionary(Of String, String)
Private idx As Integer = -1
Private count As Integer = 0
Private Function setLink(ByVal currDoc As String, ByVal myNodeTag
As String)
myLink.Add(currDoc, myNodeTag)
count = count + 1
idx = idx + 1
End Function

Private Sub Back_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Back.Click
If idx > 0 Then
idx = idx - 1
MsgBox(myLink(idx))
End If
End Sub

Public Sub ShowImage(ByVal currImage As String, ByVal myNodeTag As
String)
PictureBox1.Image = Nothing
Dim MyImage As Bitmap
MyImage = New Bitmap(currImage)
setLink(currImage, myNodeTag)
PictureBox1.Image = CType(MyImage, Image)
PictureBox1.Show()
End Sub

Private Sub pic1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic1.Click
ShowImage("image1.jpg", "Image 1")
End Sub

Private Sub pic2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic2.Click
ShowImage("image2.jpg", "Image 2")
End Sub

Private Sub pic3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles pic3.Click
ShowImage("image3.jpg", "Image 3")
End Sub
End Class



Matt
 
The solution is not to access the item by index (Integer). You must access
the item by String because String is the type of the key.

Whereas with List(Of T) Of String, the code from my first post, I am
accessing the item by index?

Is it bad practice to use 2 Lists(Of T) with a shared index?
 
Am 18.10.2010 19:00, schrieb JAG:
Whereas with List(Of T) Of String, the code from my first post, I am
accessing the item by index?

Yes.

myLink(idx)

is short for

myLink.Item(idx)

because Item is the default property, and it's parameter is the index
(of type Integer).
Is it bad practice to use 2 Lists(Of T) with a shared index?

Pardon?
 
  myLink(idx)
is short for

  myLink.Item(idx)

because Item is the default property, and it's parameter is the index
(of type Integer).
Thanks for the clarification. This makes sense now.
Sorry about the vague/odd question. I am able to accomplish my
ultimate goal, retrieving caption text and a file path for a given
file, by using 1 List(Of T) for the caption text and another List(Of
T) for the file path. I'm iterating through the lists using the
current value of idx. I guess I'm wondering, is there a more efficient
way to do this? Like for instance, using [a single] dictionary
instead. The amount of data that would potentially be stored in the 2
lists would be less than 100 entries at any time.
 
Am 19.10.2010 01:27, schrieb JAG:
myLink(idx)

is short for

myLink.Item(idx)

because Item is the default property, and it's parameter is the index
(of type Integer).
Thanks for the clarification. This makes sense now.
Sorry about the vague/odd question. I am able to accomplish my
ultimate goal, retrieving caption text and a file path for a given
file, by using 1 List(Of T) for the caption text and another List(Of
T) for the file path. I'm iterating through the lists using the
current value of idx. I guess I'm wondering, is there a more efficient
way to do this? Like for instance, using [a single] dictionary
instead. The amount of data that would potentially be stored in the 2
lists would be less than 100 entries at any time.


I've looked through the thread again, but I don't see where you need
to access the item by using a String as the key.

First thing I'd probably do is group the file path an the caption text
into one new class. Then you can store instances of this class in a
List (Of MyNewClass). You can access the items by index.
Would this be sufficient?
 
I've looked through the thread again, but I don't see where you need
to access the item by using a String as the key.

I don't believe I necessarily need to use a String as the key, I just
need to be able to iterate through the entries/items one at a time in
reverse order. I'm attempting to integrate history functionality in my
app.
First thing I'd probably do is group the file path an the caption text
into one new class. Then you can store instances of this class in a
List (Of MyNewClass). You can access the items by index.
Would this be sufficient?

Thanks for the suggestion. This would definitely work. I would need to
use For/Each to iterate through items in the List correct? Because of
my confusion in retrieving the items (where each item consisted of 2
strings) from the List I believe I abandoned the List approach. Care
to provide a generic sample?

Failing the above and since I've seen nor heard anything that's
technically wrong with using 2 Lists to get the job done, I may settle
on this for now because of a time constraint.

Matt
 
Am 19.10.2010 19:30, schrieb JAG:
I don't believe I necessarily need to use a String as the key, I just
need to be able to iterate through the entries/items one at a time in
reverse order. I'm attempting to integrate history functionality in my
app.


Thanks for the suggestion. This would definitely work. I would need to
use For/Each to iterate through items in the List correct? Because of
my confusion in retrieving the items (where each item consisted of 2
strings) from the List I believe I abandoned the List approach. Care
to provide a generic sample?

Failing the above and since I've seen nor heard anything that's
technically wrong with using 2 Lists to get the job done, I may settle
on this for now because of a time constraint.

It's just....:

Class ImageDescription
Public Path As String
Public Caption As String

Public Sub New(ByVal Path As String, ByVal Caption As String)
Me.Path = Path
Me.Caption = Caption
End Sub

End Class

'...

Dim Images As New List(Of ImageDescription)

Images.Add(New ImageDescription("c:\ugly.png", "wholy..."))
Images.Add(New ImageDescription("c:\more ugly.png", "that's me?"))

'...
For Each Image In Images
Debug.Print(Image.Path)
Debug.Print(Image.Caption)
Next

'...or by index:

Dim img = Images(1)

Not a big deal.
 
It's just....:
   Class ImageDescription
      Public Path As String
      Public Caption As String

      Public Sub New(ByVal Path As String, ByVal Caption As String)
         Me.Path = Path
         Me.Caption = Caption
      End Sub

   End Class

'...

      Dim Images As New List(Of ImageDescription)

      Images.Add(New ImageDescription("c:\ugly.png", "wholy..."))
      Images.Add(New ImageDescription("c:\more ugly.png", "that's me?"))

'...
      For Each Image In Images
         Debug.Print(Image.Path)
         Debug.Print(Image.Caption)
      Next

'...or by index:

      Dim img = Images(1)

Not a big deal.

Wow.. That works. Not sure if my implementation is "proper", but it
works. My eyes have been opened (with regard to classes). I have much
[more] to learn. If you'd like to see what I ended up with I can post
the resulting code.

Thanks a million for providing me with a sample, and for helping me
along the way.
Matt
 
Back
Top