user153923
user153923

Reputation:

C# this Indexer to VB.NET Me Indexer Conversion

How would this be written in 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):

Keyword is not valid as an identifier.

What do you VB coders use here?

Upvotes: 0

Views: 214

Answers (2)

D Stanley
D Stanley

Reputation: 152596

Use Default and something other than the reserved name 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. 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.

Upvotes: 7

HaveNoDisplayName
HaveNoDisplayName

Reputation: 8497

You can use the Converter tool to Convert C# to VB.NET.

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

Upvotes: 3

Related Questions