none
Declare and Instantiate List(Of KeyValuePair(Of String, String)) RRS feed

  • Question

  • I am trying to code my own Enigma machine for no reason other than I watched a video on how they work and now I'm intrigued.


    I'm trying to make an emulation of the wiring of the rotors and the best thing to do looks like the List(Of KeyValuePair(Of String, String)). I know how to add to it, but I just want to fill it during the declaration instead, as the values won't ever change

    Now I know how to do it with a simple list, for example

    Public wiring as New List(Of String) ({"A", "B"})

    But if I do it like that for KeyValuePair it throws an error

    Public wiring as New List(Of KeyValuePair(Of String, String)) ({"A","B"} {"C","D"})

    I get an "Overload resolution failed because no accessible 'New' accepts this number of arguments"

    How do I do it?




    • Edited by Ste Wilko Wednesday, March 4, 2020 3:13 PM
    Wednesday, March 4, 2020 3:06 PM

Answers

  • Hi

    Don't know if this is of any help, but as you want keyvalue pairs, a Dictionary seems to suit.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private wiring As New Dictionary(Of String, String) From
        {{"A", "B"}, {"C", "D"}, {"E", "F"}, {"G", "H"}}
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim res1 As String = wiring("A")  ' B
            Dim res2 As String = wiring("C")  ' D
            Dim res3 As String = wiring("E")  ' F
            Dim res4 As String = wiring("G")  ' H
    
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Ste Wilko Wednesday, March 4, 2020 4:20 PM
    Wednesday, March 4, 2020 4:06 PM
  • Create a Dictionary e.g.

    Public wiring As New Dictionary(Of String, String) From
        {
            {"A", "B"},
            {"C", "D"}
        }
    
    For Each kvp As KeyValuePair(Of String, String) In wiring
        Console.WriteLine($"{kvp.Key} = {kvp.Value}")
    Next



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ste Wilko Wednesday, March 4, 2020 4:20 PM
    Wednesday, March 4, 2020 4:15 PM
    Moderator

All replies

  • Hi

    Don't know if this is of any help, but as you want keyvalue pairs, a Dictionary seems to suit.

    Option Strict On
    Option Explicit On
    Public Class Form1
        Private wiring As New Dictionary(Of String, String) From
        {{"A", "B"}, {"C", "D"}, {"E", "F"}, {"G", "H"}}
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim res1 As String = wiring("A")  ' B
            Dim res2 As String = wiring("C")  ' D
            Dim res3 As String = wiring("E")  ' F
            Dim res4 As String = wiring("G")  ' H
    
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Ste Wilko Wednesday, March 4, 2020 4:20 PM
    Wednesday, March 4, 2020 4:06 PM
  • Create a Dictionary e.g.

    Public wiring As New Dictionary(Of String, String) From
        {
            {"A", "B"},
            {"C", "D"}
        }
    
    For Each kvp As KeyValuePair(Of String, String) In wiring
        Console.WriteLine($"{kvp.Key} = {kvp.Value}")
    Next



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Ste Wilko Wednesday, March 4, 2020 4:20 PM
    Wednesday, March 4, 2020 4:15 PM
    Moderator