2017-05-30 23 views
0

Google讓我失敗了,所以我轉而使用Stack Overflow。你是我唯一的希望!獲取值在列表中的屬性值

我有一個有幾個屬性的類。其中一個屬性是雙打列表。我希望能夠讀取列表中的每個雙精度值並將它們打印到文本框中。

Public Class MyClass 
    Dim aValue As List(Of Double) 

    Public Property myProp As List(Of Double) 

     Get 
      Return aValue 'This very well could be my problem 
     End Get 
     Set(value As List(Of Double)) 
      'This is also likely the culprit, as I have no idea what to put here 
     End Set 

    End Property 

End Class 

所以,我有那個類。於是,我嘗試如下初始化的myProp值:

Dim c As New MyClass() 

Dim vals As New List(Of Double) 

    For i = 0 To 11 
     Dim rndVal = CInt(Math.Floor((10 - (-10) + 1) * Rnd())) + (-10) 
     vals.Add(rndVal) 
    Next 

c.myProp = vals 

然後,我希望能夠得到物業myProp並返回myProp所包含的值,然後把它們打印到一個文本框。這是我目前的:

Public Class Calc 

    Private Sub drpdwnBox_SelectedIndexChanged(sender As Object, e As EventARgs), Handles drpdwnBox.SelectedIndexChanged 

     For Each p As Reflection.PropertyInfo In myClass.GetType().GetProperties() 

      If p.CanRead Then 

       'This works to get the name of the Property, but... 
       txtBox.Text &= p.Name & " " 

       'This does nothing, basically. 
       If p.GetType Is GetType(List(Of Double)) Then 

        For Each k In p.GetValue(myClass, Nothing).Keys 
         txtBox.Text &= p.GetValue(myClass, Nothing).Keys.ToString() & " " 
         txtBox.Text &= vbNewLine 
        Next 

       Else 
        'This also works to get the value of properties that aren't lists 
        txtBox.Text &= p.GetValue(Nation, Nothing).ToString() & vbNewLine 

       End If 

      End If 

     Next 

    End Sub 

上面的代碼可能有幾個問題。儘管如此,我會接受任何幫助。如果沒有列表作爲屬性有更好的方法來做到這一點,那也是一個可喜的解決方案。我使用List作爲Property的一個主要原因是我使用XmlSerializer來快速輕鬆地將類和屬性轉換爲XML文件進行存儲。

反正輸出,當我運行此代碼,我得到如下:

myProp 

Aaaand就是這樣。列表中沒有添加任何值,但我至少得到了該屬性的名稱,所以至少部分是正確的。

無論如何,謝謝任何人誰可以提供任何解決方案!

+0

只看這個,我可以看到一個問題:p.GetType將始終返回PropertyInfo的類型。也許你想要p.Value.GetType。 – Mike

+0

是的,你是完全正確的。我結束了使用p.PropertyType來確定屬性的類型。謝謝! – ForgottenGlory

回答

0

你可能根本不想擁有該屬性的Set。像這樣做:

Public Class myClass 
    Private aValue As New List(Of Double) 

    Public Property myProp As List(Of Double) 
     Get 
      Return aValue 
     End Get  
    End Property 

End Class 

然後添加這樣的價值觀:

For i = 0 To 11 
    Dim rndVal = CInt(Math.Floor((10 - (-10) + 1) * Rnd())) + (-10) 
    'myProp.Add() only needs the Get accessor! 
    myClassInstance.myProp.Add(rndVal) 
Next 

通知我用myClassInstance,而不是僅僅myClass。爲了使用你的類,你必須在一個變量中創建一個實例。所有從Public Class myClassEnd Class的東西只是描述了一個類的對象實例在創建一個對象時的行爲,但它有而不是實際上在內存中創建了任何可以使用的東西。某個地方,你必須在你有任何可以使用的東西之前說出New myClass()

一旦你有一個變量,你最後的代碼段看起來更像是這樣的:

Private Sub drpdwnBox_SelectedIndexChanged(sender As Object, e As EventARgs), Handles drpdwnBox.SelectedIndexChanged 

    For Each k As Double In myClassInstance.myProp 
     txtBox.Text &= k 
    Next 
End Sub 

,直到你能夠了解一個類的實例,併爲一類定義之間的差異,如何將變量與對象引用一起用於您的類,您將很難編寫任何工作代碼。

+0

對不起,我並不想深入研究這些值,因爲我相當確定這是有效的,但在隨機函數發生器之前有'Dim c As myClass = New myClass'。我會更新我的原始問題以反映這一點,並感謝您指出了這一點。 另外,非常感謝您的幫助!我會給你一個機會,但從你過去的經驗來看,我相信這一切都會奏效。 ;) – ForgottenGlory

+0

在這種情況下,'myClassInstance'可以在我的大多數示例代碼中變成'c',但是你需要一種方法來引用兩節中的_same_'c'。 –