2009-12-23 68 views
0

类像列表和字典.NET可以直接编入索引,就不能不提一员,像这样:如何创建一个类似于.net中的列表的索引类?

Dim MyList as New List (of integer) 
... 
MyList(5) 'get the sixth element 
MyList.Items(5) 'same thing 

我如何能够被索引这样的一类?

Dim c as New MyClass 
... 
c(5) 'get the sixth whatever from c 

回答

8

您需要提供索引(C#术语)或默认属性(VB术语)。

VB::从实施例MSDN docsmyStrings是一个字符串数组)

Default Property myProperty(ByVal index As Integer) As String 
    Get 
     ' The Get property procedure is called when the value 
     ' of the property is retrieved. 
     Return myStrings(index) 
    End Get 
    Set(ByVal Value As String) 
     ' The Set property procedure is called when the value 
     ' of the property is modified. 
     ' The value to be assigned is passed in the argument 
     ' to Set. 
     myStrings(index) = Value 
    End Set 
End Property  

和C#的语法:

public string this[int index] 
{ 
    get { return myStrings[index]; } 
    set { myStrings[index] = vaue; } 
} 
相关问题