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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18308608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:43:06  来源:igfitidea点击:

How to create your own type and use it to store data

vb.netclasstypesdictionary

提问by Uriel Katz

I've recently ran into the problem that dictionary only allows 1 value per key. Reading around I have seen multiple answers suggesting creating a type through a classes. Now granted I don't know much about classes, I have always though that classes were just a collection of functions and subs. How come they can create data types and how do you use them to do so?

我最近遇到了字典只允许每个键 1 个值的问题。阅读周围,我看到多个答案建议通过类创建类型。现在虽然我对类不太了解,但我一直认为类只是函数和子函数的集合。为什么他们可以创建数据类型,您如何使用它们来创建数据类型?

回答by varocarbas

The basic definition of a Dictionaryis given by Dictionary(Of type1, type2), where types can be anything, that is, primitive types (String, Double, etc.) or ones you create (via Class, for example). Also you can account for them as "individual variables" or inside collections (Lists, Arrays, etc.). Some examples:

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

 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)

Where type2is defined by the following Class:

其中type2由以下类定义:

Public Class type2
    Public prop1 As String
    Public prop2 As String
    Public prop3 As String
End Class

NOTE: you can change the types in the examples above as much as you wish; also put anything (List, custom types, etc.) in both Valuesand Keys.

注意:您可以根据需要更改上述示例中的类型;还将任何内容(列表、自定义类型等)放在Values和 中Keys

NOTE2: the primitive types in VB.NET (for example: Double) are basically a bunch of variables (declared globally inside the given framework) and functions: Double.IsInfinity(function), Double.MaxValue(variable), etc.; thus a type can be understood as an in-built Class, that is, a general name for a group of functions and variables, which can be used to define another variable in a different Class. I think that the proposed example is pretty descriptive.

NOTE2:VB.NET中的原始类型(例如:)Double基本上是一堆变量(在给定的框架内全局声明)和函数:Double.IsInfinity(function),Double.MaxValue(variable)等;因此一个类型可以理解为一个内置的类,即一组函数和变量的总称,可以用来在不同的类中定义另一个变量。我认为提议的示例非常具有描述性。

回答by the_lotus

Classes aren't just about functions and subs, they also contains variable and properties. Which can be used to store a bunch of values.

类不仅仅是函数和子类,它们还包含变量和属性。可用于存储一堆值。

Lets say you want to store the first name and last name of a person in the dictionnary by person number.

假设您想按人员编号在字典中存储一个人的名字和姓氏。

Public Class Person
    Public Property Number As String
    Public Property FirstName As String
    Public Property LastName As String
End Class

Dim dict = New Dictionary(Of String, Person)
Dim p = New Person

p.Number = "A123"
p.FirstName = "John"
p.LastName = "Doe"

dict.Add(p.Number, p)

And then to fetch the person back

然后把人带回来

p = dict("A123")

Console.WriteLine(p.FirstName)
Console.WriteLine(p.LastName)

回答by Uriel Katz

After combining knowledge from this and other sources, here is my final solution:

结合来自这个和其他来源的知识后,这是我的最终解决方案:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim dictionary = New Dictionary(Of String, Pair)
        Dim p As New Pair("A", "B")

        MsgBox(p.First)
        MsgBox(p.Second)
    End Sub
End Class

Public Class Pair
    Private ReadOnly value1 As String
    Private ReadOnly value2 As String

    Sub New(first As String, second As String)
        value1 = first
        value2 = second
    End Sub

    Public ReadOnly Property First() As String
        Get
            Return value1
        End Get
    End Property

    Public ReadOnly Property Second() As String
        Get
            Return value2
        End Get
    End Property
End Class