2013-08-19 65 views
0

我最近遇到了字典只允許每個鍵1個值的問題。閱讀周圍我看到了多個答案,建議通過類創建一個類型。現在,我對課程知之甚少,但我始終認爲這些課程只是功能和子系列的集合。他們如何創建數據類型以及如何使用它們來實現?如何創建自己的類型並用它來存儲數據

回答

5

一個Dictionary的基本定義由Dictionary(Of type1, type2),其中類型可以是任何東西,即,創建原始類型(StringDouble等)或酮(經由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) 

type2由下面的類定義:

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

注意:您可以更改類型上面,就像你想的例子;還在ValuesKeys中都放置了任何東西(列表,自定義類型等)。

注2:原始類型在VB.NET(例如:Double)基本上是一組變量(給定的框架內聲明全局)和功能:Double.IsInfinity(功能),Double.MaxValue(變量),等;因此一個類型可以被理解爲一個內置類,也就是一組函數和變量的通用名稱,它們可以用來定義另一個類中的另一個變量。我認爲這個提議的例子很具描述性。

1

類不僅僅是函數和子類,它們還包含變量和屬性。可以用來存儲一堆值。

可以說,你想存儲一個人的名字和姓氏在人物編號的字典中。

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) 

然後去取人回到

p = dict("A123") 

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

你應該把類的不同代碼塊來突出它與下面代碼的區別(一位非專家型讀者可能會認爲它可以將所有內容放在同一個函數中)。你的字典被錯誤地定義(或填充):你將它定義爲(字符串,人員列表),但用(字符串,人物)填充它(你的代碼甚至不能編譯)。你對OP做的事情甚至更加困惑(即使忽略錯誤):你正在將列表與列表混合在一起,沒有任何理由;我認爲這點在我的回答中已經很清楚了。 – varocarbas

+1

對不起昨天我說的話。我認爲你沒有最好的行爲,但@UrielKatz幫助我意識到我不應該這樣對待你。 – varocarbas

0

從這個和其他來源相結合的知識後,這裏是我的最終解決方案:

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 
+0

正如一個評論,以確保你得到一切正確的:你可以包括在任何你想要的類(你將從你聲明的變量中訪問所有這些信息)。你可以包含屬性(你在做什麼)或變量/字段(我所做的)或函數。相反,單個變量/屬性,您可以聲明數組/列表並自動從構造函數的子實例化它們。你的代碼基本上和the_lotus相同,雖然他依靠總結的方式來聲明屬性。 – varocarbas

+0

只要the_lotus代碼說基本上相同(屬性和變量實際上說相同)比我的答案的一部分(6小時前寫),你的答案說明與_lotus一樣(有更多的代碼行),我猜你會明白爲什麼我不喜歡你。我希望你的想法在這些評論後得到更清晰的表達。 – varocarbas

+0

@varocarbas我已經給你信貸給你答案,我以前偶然發現了錯誤的信息。既然你已經提供了更多的信息(6小時前),並花時間回覆我的回答,我覺得給你信用是公平的。也請避免發表令人反感的評論。 –

相關問題