2

//Declaration

Dim values As New List(Of Dictionary(Of String, String))()

I am trying to create a multidimensional array. this is my code:

con.Open()
ccsfreader = ccsfcomm.ExecuteReader
ccsfreader.Read()
If ccsfreader.HasRows Then
    Do
        values.Add(New Dictionary(Of String, String)() From {{"CostCentre", ccsfreader.Item("CostCentre")}})
        values.Add(New Dictionary(Of String, String)() From {{"ProcessDescription", ccsfreader.Item("ProcessDescription")}})
    Loop While ccsfreader.Read()
End If

con.Close()

For Each value As Dictionary(Of String, String) In values
    Dim CostCentre As String = value("CostCentre")
    Dim ProcessDescription As String = value("ProcessDescription")
    cmblaborcost.Items.Add(CostCentre)
Next

My error is: The given key was not present in the dictionary.

i want an output like this:

1 => array(
CostCentre=>10.00
ProcessDescription=>"up"
)
2 => array(
CostCentre=>20.00
ProcessDescription=>"sided"
)
3 => array(
CostCentre=>110.00
ProcessDescription=>"cutted"
)
5
  • Can you show us your declaration of values, and cmblabourcost. Thank you Commented Apr 21, 2014 at 0:21
  • see the edited questions Commented Apr 21, 2014 at 0:24
  • cmblaborcost is a combo box Commented Apr 21, 2014 at 0:25
  • What line is the error on? Commented Apr 21, 2014 at 0:26
  • the statement after the for each Commented Apr 21, 2014 at 0:30

1 Answer 1

2

You have two dictionaries in the list. One of which contains only "CostCentre" key, and the other contains only "ProcessDescription" key. So when you try to access both keys from a dictionary, one key must be missing.

You may want to use List(Of Tuple(Of String, String)) instead of List(Of Dictionary(Of String, String)). This example works for me :

Dim values As New List(Of Tuple(Of String, String))
values.Add(Tuple.Create("a1", "a2"))
values.Add(Tuple.Create("b1", "b2"))
values.Add(Tuple.Create("c1", "c2"))

'generate array from List of Tuple'
Dim result = values.Select(Function(x) New String() {x.Item1, x.Item2}).ToArray()
For Each s As String() In result
    Console.WriteLine(s(0) & ", " & s(1))
Next

And for your case, it could be something like this :

'example to add item to list'
Dim values As New List(Of Tuple(Of String, String))
......
If ccsfreader.HasRows Then
    Do
        'values.Add(Tuple.Create(ccsfreader.Item("CostCentre"), ccsfreader.Item("ProcessDescription"))'
        values.Add(New Tuple(Of String, String)(ccsfreader.Item("CostCentre"), ccsfreader.Item("ProcessDescription")))
    Loop While ccsfreader.Read()
End If

.......
'example to access item from list'
For Each value As Tuple(Of String, String) In values
    Dim CostCentre As String = value.Item1
    Dim ProcessDescription As String = value.Item2
    cmblaborcost.Items.Add(CostCentre)
Next
Sign up to request clarification or add additional context in comments.

9 Comments

im using vb 2010. .NET 4.5.1
okay, I'm not sure how it was working in the post you linked to. Anyway, since Tuple if already available in .NET version you're using, I post an alternative using list of Tuple, this approach works fine for me here.
thank you har07 but i have an error of this :Value of type 'System.Tuple(Of Object, Object)' cannot be converted to 'System.Tuple(Of String, String)'
hmm.. Seems that it is inferred wrongly as Tuple(Of Object, Object). Try to create the tuple using explicit type declaration : New Tuple(Of String, String)(value1, value2)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.