简体   繁体   English

C#此索引器到VB.NET Me索引器转换

[英]C# this Indexer to VB.NET Me Indexer Conversion

How would this be written in VB.NET? 如何用VB.NET编写?

public class Foos : ICollection<Foo> {

  private List<Foo> list;

  public Foos() {
    list = new List<Foo>();
  }

  public Foo this[int index] {
    get {
      return list[index];
    }
  }

}

Here is what I tried: 这是我尝试过的:

Public Class Foos
  Implements ICollection(Of Foo)

  Private list as Generic.List(Of Foo)

  Public Sub New()
    list = New Generic.List(Of Foo)()
  End Sub

  Public ReadOnly Property Me(index As Integer) As Foo
    Get
      Return list(index)
    End Get
  End Property

End Class

Visual Studio gives a compile error at line with Me(index As Integer) (pointing at the Me keyword): Visual Studio给出与Me(index As Integer)一致的编译错误(指向Me关键字):

Keyword is not valid as an identifier. 关键字无效,不能用作标识符。

What do you VB coders use here? 您的VB编码人员在这里使用什么?

Use Default and something other than the reserved name Me : 使用Default和保留名称Me以外的其他名称:

  Default Public ReadOnly Property Item(index As Integer) As Foo
    Get
      Return list(index)
    End Get
  End Property

Default signifies that the property is the, well, default property for the class, meaning that if you don;t specify the property name it will implicitly use the default property. Default表示该属性是该类的默认属性,也就是说,如果您不指定属性名称,它将隐式使用默认属性。 So it looks like an array indexer but is really just a method call. 因此,它看起来像一个数组索引器,但实际上只是一个方法调用。

You can also name it anything you want - Item is a fairly common name used by framework classes, but there's no magic from using the name Item . 您也可以随意命名Item是框架类使用的相当通用的名称,但是使用Item名称并没有什么神奇之处。

You can use the Converter tool to Convert C# to VB.NET. 您可以使用转换器工具将C#转换为VB.NET。

http://codeconverter.sharpdevelop.net/SnippetConverter.aspx http://codeconverter.sharpdevelop.net/SnippetConverter.aspx

    Public Class Foos
     Implements ICollection(Of Foo)

     Private list As List(Of Foo)

     Public Sub New()
        list = New List(Of Foo)()
     End Sub

     Public Default ReadOnly Property Item(index As Integer) As Foo
        Get
            Return list(index)
        End Get
     End Property
   End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM