Page 1 of 1

Class Indexing How to give your class the index ability. Rate Topic: -----

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Posted 11 June 2009 - 07:08 PM

Class Indexing

Example of a class indexing in use.
Dim x As String="ABCDEF"
Console.Write(x(2))


See how it possible to access the 3rd character of the string with the use of x(3)

Have you wonder how to implement them?

In this sample class code I use and array of Integer but it could any type.
Public Class ExampleOfClassIndexer
 Inherits System.Collections.CollectionBase
 Private m_Item(19) As Integer

 ' The  important overloaded property the lets us do Class Indexing.
 Default Public Overloads Property Item(ByVal Index As Int32) As Integer
  Get
  ' Make sure the Index is within the bounds of the collection / array
   If Index < 0 Or Index > (m_Item.Count - 1) Then Throw New IndexOutOfRangeException
  ' Return the item at the specified index.
   Return CType(m_Item(Index), Integer)
  End Get
 '  Much the same as above but this time setting value at the specified index.
  Set(ByVal value As Integer)
   If Index < 0 Or Index > (m_Item.Count - 1) Then Throw New IndexOutOfRangeException
   m_Item(Index) = value
  End Set
 End Property

End Class



It can be extend into a higher number of dimensions.
For Example, Requires a lot more Bound Checks.
Public Class ExampleOf_2DClassIndexer
 Inherits System.Collections.CollectionBase

 Private m_Item(7, 7) As Integer
 Default Public Overloads Property Item(ByVal RowIndex As Int32, ByVal ColIndex As Int32) As Integer
  Get
   If (RowIndex < 0) Or (RowIndex > UBound(m_Item,1)) Then Throw New IndexOutOfRangeException("RowIndex out of bounds")
   If (ColIndex < 0) Or (ColIndex > UBound(m_Item,2)) Then Throw New IndexOutOfRangeException("ColIndex out of bounds")

   Return CType(m_Item(RowIndex, ColIndex), Integer)
  End Get
  Set(ByVal value As Integer)
   If (RowIndex < 0) Or (RowIndex > UBound(m_Item,1)) Then Throw New IndexOutOfRangeException("RowIndex out of bounds")
   If (ColIndex < 0) Or (ColIndex > UBound(m_Item,2)) Then Throw New IndexOutOfRangeException("ColIndex out of bounds")
   m_Item(RowIndex, ColIndex) = value
  End Set
 End Property

End Class



So now you'll be a Class Indexing Coding Ninja :ph34r:

Is This A Good Question/Topic? 0
  • +

Replies To: Class Indexing

#2 astrodon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 14-February 09

Posted 05 August 2009 - 04:49 AM

Public Class ExampleOfClassIndexer
 Inherits System.Collections.CollectionBase
 Private m_Item(19) As Integer

 ' The  important overloaded property the lets us do Class Indexing.
 Default Public Overloads Property Item(ByVal Index As Int32) As Integer
  Get
  ' Make sure the Index is within the bounds of the collection / array
   If Index < 0 Or Index > (m_Item.Count - 1) Then Throw New IndexOutOfRangeException
  ' Return the item at the specified index.
   Return CType(m_Item(Index), Integer)
  End Get
 '  Much the same as above but this time setting value at the specified index.
  Set(ByVal value As Integer)
   If Index < 0 Or Index > (m_Item.Count - 1) Then Throw New IndexOutOfRangeException
   m_Item(Index) = value
  End Set
 End Property

End Class



Why is the test necessary on the read portion of the property statements when the test was made on the write portion? If the property was written correctly why would there be a need to check a property that could only be good?
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Posted 05 August 2009 - 06:35 AM

What if the user uses Dim result = Example(-1) or Dim result = Example(200)?

They are called Sanity Checks. Check that input is valid before you use it, if not throw an exception.

This post has been edited by AdamSpeight2008: 05 August 2009 - 07:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Posted 03 August 2010 - 09:10 PM

An update on how to do this, it seem that the specific detail to implement this is the Default, as this simple class demonstrates.
 
 Public Class Tst
    Protected Friend mList As New List(Of Integer)
    Default Public Property Index(ByVal i As Integer) As Integer
      Get
        If i < 0 OrElse i >= mList.Count Then Throw New IndexOutOfRangeException
        Return mList(i)
      End Get
      Set(ByVal value As Integer)
        If i < 0 OrElse i >= mList.Count Then Throw New IndexOutOfRangeException
        mList(i) = value
      End Set
    End Property
  End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1