2011-10-22 80 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 

你也可以把你想要的任何代码放在属性本身,尽管我建议调用一个方法。