字典(整数,类)类型

时间:2016-02-12 01:46:09

标签: vb.net class dictionary

我想在添加到数据表之前以简单的方法保存变量。是否可以使用dictionary (of integer,class)类型,如下面的代码?然后我可以遍历键来获取变量。请问有什么想法吗?

     Public Class CONTACTS
        Dim FIRST_NAME As String
        Dim SURNAME As String
        Dim DOB As Date
        Dim AGE As Integer
    End Class


    Sub TEST_DICT()

        Dim dctCONTACTS As Dictionary(Of Integer, List OF Class(CONTACTS))  'not sure how to declare this

        dctCONTACTS.Add(1, "MARK").FIRST_NAME  'not sure how to savevariable
        dctCONTACTS.Add(1, "SMITH").SURNAME
        dctCONTACTS.Add(1, "5 May 1995").DOB
        dctCONTACTS.Add(1, 31).AGE

        dctCONTACTS.Add(2, "ANNE").FIRST_NAME
        dctCONTACTS.Add(2, "MOORE").SURNAME
        dctCONTACTS.Add(3, "5 April 1990").DOB
        dctCONTACTS.Add(4, 26).AGE

        Dim X = dctCONTACTS(1).AGE
    End Sub

1 个答案:

答案 0 :(得分:1)

你真的要去网上看很多“如何编码VB.NET”的资源,而不是在这里问基本的语法问题。

以下是有效VB.NET中的代码:

 Public Class CONTACTS
    Public FIRST_NAME As String
    Public SURNAME As String
    Public DOB As Date
    Public AGE As Integer
End Class

Sub TEST_DICT()

    Dim dctCONTACTS As New Dictionary(Of Integer, List(Of CONTACTS)) 

    dctCONTACTS.Add(1, New List(Of CONTACTS) From
    {
        New CONTACTS() With { .FIRST_NAME = "MARK", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
        New CONTACTS() With { .FIRST_NAME = "JOE", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
        New CONTACTS() With { .FIRST_NAME = "SARAH", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 }
    })

    dctCONTACTS.Add(2, New List(Of CONTACTS) From
    {
        New CONTACTS() With { .FIRST_NAME = "BOB", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
        New CONTACTS() With { .FIRST_NAME = "SUE", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 },
        New CONTACTS() With { .FIRST_NAME = "ABIGAIL", .SURNAME = "SMITH", .DOB = New DateTime(1995, 5, 5), .AGE = 31 }
    })

    Console.WriteLine(dctCONTACTS(2)(0).FIRST_NAME)

End Sub

在这种情况下,运行TEST_DICT会提供BOB