如何创建自己的类型并使用它来存储数据

Uri*_*atz 0 vb.net dictionary types class

我最近遇到的问题是字典只允许每个键1个值.阅读我已经看到多个答案建议通过类创建一个类型.现在授予我对类不太了解,我总是认为类只是函数和子函数的集合.为什么他们可以创建数据类型,你如何使用它们?

var*_*bas 5

的基本定义Dictionary由下式给出Dictionary(Of type1, type2),其中类型可以是任何东西,也就是原始类型(String,Double创建(通过等)或1 Class,例如).你也可以考虑他们作为"独立变量"或内部集合(Lists,Arrays,等).一些例子:

 Dim dict = New Dictionary(Of String, List(Of String))

 Dim tempList = New List(Of String)
 tempList.Add("val11")
 tempList.Add("val12")
 tempList.Add("val13")

 dict.Add("1", tempList)

 Dim dict2 = New Dictionary(Of String, type2)
 Dim tempProp = New type2
 With tempProp
     .prop1 = "11"
     .prop2 = "12"
     .prop2 = "13"
 End With
 dict2.Add("1", tempProp)

 Dim dict3 = New Dictionary(Of String, List(Of type2))
 Dim tempPropList = New List(Of type2)
 Dim tempProp2 = New type2
 With tempProp2
     .prop1 = "11"
     .prop2 = "12"
     .prop2 = "13"
 End With
 tempPropList.Add(tempProp2)

 dict3.Add("1", tempPropList)
Run Code Online (Sandbox Code Playgroud)

其中type2由下面的类中定义的:

Public Class type2
    Public prop1 As String
    Public prop2 As String
    Public prop3 As String
End Class
Run Code Online (Sandbox Code Playgroud)

注意:您可以根据需要更改上述示例中的类型; 也把两个东西(列表,自定义类型等)ValuesKeys.

注2:VB.NET中的原始类型(例如:) Double基本上是一堆变量(在给定框架内全局声明)和函数:( Double.IsInfinity函数),Double.MaxValue(变量)等; 因此,类型可以理解为内置类,即一组函数和变量的通用名称,可用于在另一个类中定义另一个变量.我认为提出的例子非常具有描述性.