School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,003 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,950 people online right now. Registration is fast and FREE... Join Now!




Class Indexing

 
Reply to this topicStart new topic

> Class Indexing, How to give your class the index ability.

AdamSpeight2008
Group Icon



post 11 Jun, 2009 - 06:08 PM
Post #1


Class Indexing

Example of a class indexing in use.
CODE

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.
CODE

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.
CODE

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.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

astrodon
*



post 5 Aug, 2009 - 03:49 AM
Post #2
CODE

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?
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 5 Aug, 2009 - 05:35 AM
Post #3
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: 5 Aug, 2009 - 06:15 AM
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 06:38AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month