שתף באמצעות


How access the index of a dictionary?

Question

Saturday, January 23, 2016 1:19 PM

I am looking for a way to access a dictionary from the index in a different way. This is what I have,

replacements.Keys.ElementAt(nextCheckIndex)

but it is bringing a stackoverflow. How did I know this? When I replace words with the following method, it is working

  

Dim education As New Dictionary(Of String, List(Of String))Dim newWordList As New List(Of String)
        ' Allocate and populate the field Dictionary.
        If education.ContainsKey("good") Then
            education("bad").Add("good")
        Else
            newWordList.Add("good")
            education.Add("bad", newWordList)

        End If

Kelvin Nyota From Researchpaperstobuy.com

All replies (12)

Saturday, January 23, 2016 1:47 PM

 Well,  i wont say that i understand what you mean or what you want to do exactly but,   Dictionaries don`t use an Index like a List or Array does.

 If you want to get some sort of index for a specific entry you can iterate through the Dictionary entries one at a time using a For Next loop.  When the entry you want is found,  the you exit the loop and the loop value would be the index.

        Dim indx As Integer = 0

        For indx = 0 To dict.Count - 1
            If dict.ElementAt(indx).Key = "(Something)" Then Exit For
        Next

        MessageBox.Show("(Something) was found at the index of " & indx.ToString)

If you say it can`t be done then i`ll try it


Saturday, January 23, 2016 1:54 PM

IronRazerz

Can it work like this

  Dim nextCheckIndex As Integer = 0

  For nextCheckIndex = 0 To replacements.Count - 1
 
            
If replacements.ElementAt(nextCheckIndex).Key =

RichTextBox1.Find(checkWord, start, RichTextBoxFinds.WholeWord) Then Exit For
        Next

Saturday, January 23, 2016 1:59 PM

IronRazerz

Alternatively, how would you implement it on this code?

checkWord = replacements.Keys.ElementAt(nextCheckIndex)

            foundIndex = RichTextBox1.Find(checkWord, 0, RichTextBoxFinds.WholeWord)
            If foundIndex > -1 Then
                ContextMenuStrip1.Items.Clear()
                For Each replacement In replacements(checkWord)

Kelvin Nyota From Researchpaperstobuy.com


Saturday, January 23, 2016 2:21 PM

Can somebody help me convert this into Vb.NET

public class ListWithDuplicates : List<KeyValuePair<string, string>>
{
    public void Add(string key, string value)
    {
        var element = new KeyValuePair<string, string>(key, value);
        this.Add(element);
    }
}

var list = new ListWithDuplicates();
list.Add("k1", "v1");
list.Add("k1", "v2");
list.Add("k1", "v3");

foreach(var item in list)
{
    string x = string.format("{0}={1}, ", item.Key, item.Value);
}

Kelvin Nyota From Researchpaperstobuy.com


Saturday, January 23, 2016 2:33 PM

Can somebody help me convert this into Vb.NET

public class ListWithDuplicates : List<KeyValuePair<string, string>>
{
    public void Add(string key, string value)
    {
        var element = new KeyValuePair<string, string>(key, value);
        this.Add(element);
    }
}

var list = new ListWithDuplicates();
list.Add("k1", "v1");
list.Add("k1", "v2");
list.Add("k1", "v3");

foreach(var item in list)
{
    string x = string.format("{0}={1}, ", item.Key, item.Value);
}

Kelvin Nyota From Researchpaperstobuy.com

 Use a free online C# to Vb code converter.

http://converter.telerik.com/

If you say it can`t be done then i`ll try it


Saturday, January 23, 2016 2:44 PM | 1 vote

As a guess, just line by line changed.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim vlist As New ListWithDuplicates()
        vlist.Add("k1", "v1")
        vlist.Add("k1", "v2")
        vlist.Add("k1", "v3")
        For Each item In vlist
            Dim x = String.Format("{0}={1}, ", item.Key, item.Value)
        Next
    End Sub
End Class
Public Class ListWithDuplicates
    Inherits List(Of KeyValuePair(Of String, String))
    Public Overloads Sub Add(key As String, value As String)
        Dim element As New KeyValuePair(Of String, String)(key, value)
        Me.Add(element)
    End Sub
End Class

Success
Cor


Saturday, January 23, 2016 2:49 PM

IronRazerz

It is saying line 26 has a problem. I don't know C#, I had already tried it.

Kelvin Nyota From Researchpaperstobuy.com


Saturday, January 23, 2016 2:54 PM

IronRazerz

How do you access a file from the dictionary using this code? It is my last question if your method could not work the one I converted into RichTextBox.Find (Method). Check above below your first answer.

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim vlist As New ListWithDuplicates()
        vlist.Add("k1", "v1")
        vlist.Add("k1", "v2")
        vlist.Add("k1", "v3")
        For Each item In vlist
            Dim x = String.Format("{0}={1}, ", item.Key, item.Value)
        Next
    End Sub
End Class
Public Class ListWithDuplicates
    Inherits List(Of KeyValuePair(Of String, String))
    Public Overloads Sub Add(key As String, value As String)
        Dim element As New KeyValuePair(Of String, String)(key, value)
        Me.Add(element)
    End Sub
End Class

Kelvin Nyota From Researchpaperstobuy.com


Saturday, January 23, 2016 2:59 PM

Look at Cor`s reply.  That is what it would look like in Vb.

If you say it can`t be done then i`ll try it


Saturday, January 23, 2016 3:02 PM

IronRazerz

No, am asking how to now set it up to look like something this

        Using reader As New StreamReader("path")
            Do Until reader.EndOfStream
                Dim parts = reader.ReadLine().Split("|"c)

                If replacements.ContainsKey(parts(0)) Then
                    replacements(parts(0)).Add(parts(1))
                Else
                    Dim newWordList As New List(Of String)
                    newWordList.Add(parts(1))
                    replacements.Add(parts(0), newWordList)
                End If

            Loop
        End Using

Kelvin Nyota From Researchpaperstobuy.com


Wednesday, February 3, 2016 7:39 AM

Hi Researchpapers to buy center,

>>Dim parts = reader.ReadLine().Split("|"c)

This line raise a error, you need to remove 'c' from your code.

Dim parts = reader.ReadLine().Split("|")

Best Regards,
Li Wang

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.


Friday, February 5, 2016 1:37 PM

IronRazerz

How do you use a double in a type string? This code of yours is not logical.

Your code will never work, I have tested it.

Dim nextCheckIndex As Integer = 0

  For nextCheckIndex = 0 To replacements.Count - 1
 
            
If replacements.ElementAt(nextCheckIndex).Key =

RichTextBox1.Find(checkWord, start, RichTextBoxFinds.WholeWord) Then Exit For
        Next

Kelvin Nyota From Researchpaperstobuy.com