2011-10-22 101 views
0

我想做一些類似於listbox.children屬性。我知道我可以這樣寫返回列表框的孩子:功能屬性

Public ReadOnly Property Children(index as integer) 
    Get 
     'Return the children at the specified index 
    End Get  
End Property 

但我怎麼能做出Children屬性的功能是什麼?例如,List.Children.Count將返回列表條目的計數,但List.Children(0)將返回第一個條目。

+0

將'Option Strict On'放在源代碼文件的頂部。您必須進行的更改才能使此代碼進行編譯,這有助於您找到正確的答案。 –

回答

0

答案其實很簡單。我只是做了這個功能:

Public Function GetItem(Of T)(index as integer) as T 
Return _ListOfT(index) 
End Functions 

然後只是修改T類以適應我的需要。

+0

不錯的功能修改器。 – N0Alias

+0

@NoAlias更好? – Cobold

0

您將不得不將您的Children屬性的類型更改爲實現IEnumerable Interface的東西,例如List(兒童)以輕鬆獲取Count,For Each迭代等等。目前您正在返回Integer,但是我的我們猜測對於一個「孩子」實體還有更多的不僅僅是數值。

-1

我認爲'list.children(0)'不是一個函數,而是它是一個帶重載構造函數或類似的派生類。除此之外,您還可以編寫一個函數,該函數返回包含所有子信息的列表類型的數組。

一樣,

public function ReturnChild(byval listbox as listbox, byref NewlistBox as listbox) 

     your code goes here 

end function 
0

我不知道我理解你的要求,但如果你是問你怎麼辦除了返回一個簡單的備份存儲的東西,這裏有雲:

Public ReadOnly Property Children(index as integer) as List(Of WhateverObject) 
    Get 
    return GetChildren(index) 
    End Get  
End Property 

Private Function GetChildren(index as integer) as List(Of WhateverObject) 

'Code that actually gets a list of the object type of the children at that index 

End Function 

你也可以把你想要的任何代碼放在屬性本身,儘管我建議調用一個方法。