Sam,
In addition to Nigel's suggestion. I would define a Coordinate structure
that contained the X & Y components. Then I would either use this structure
as the key to the hashtable or use it ArrayList.IndexOf method. I would
favor the HashTable.
Something like:
Public Structure Coordinate
Public ReadOnly X, Y As Integer
Public Sub New(ByVal x As Integer, ByVal y As Integer)
Me.X = x
Me.Y = y
End Sub
#Region " Hashtable sample code "
Public Overrides Function GetHashCode() As Integer
Return Me.X.GetHashCode() Xor Me.Y.GetHashCode()
End Function
Public Overloads Function Equals(ByVal other As Coordinate) As
Boolean
Return Me.X = other.X AndAlso Me.Y = other.Y
End Function
Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Coordinate Then
Return Equals(DirectCast(obj, Coordinate))
Else
Return False
End If
End Function
#End Region
End Structure
Public Structure Node
ReadOnly Coordinate As Coordinate
Dim Label As String
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal label
As String)
Me.Coordinate = New Coordinate(x, y)
Me.Label = label
End Sub
#Region " Arraylist sample code "
Public Overloads Function Equals(ByVal other As Node) As Boolean
Return Me.Coordinate.Equals(other.Coordinate)
End Function
Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Node Then
Return Equals(DirectCast(obj, Node))
Else
Return False
End If
End Function
#End Region
End Structure
Public Shared Sub Main()
' Hashtable sample code
Dim table As New Hashtable
table.Add(New Coordinate(1, 1), New Node(1, 1, "a"))
table.Add(New Coordinate(2, 3), New Node(2, 3, "b"))
table.Add(New Coordinate(5, 4), New Node(5, 4, "c"))
Dim value As Node = table.Item(New Coordinate(2, 3))
' Arraylist sample code
Dim list As New ArrayList
list.Add(New Node(1, 1, "a"))
list.Add(New Node(2, 3, "b"))
list.Add(New Node(5, 4, "c"))
Dim index As Integer = list.IndexOf(New Node(2, 3, String.Empty))
End Sub
I would favor the HashTable, as you can do the lookup via Coordinate itself,
you don't need to create an entire Node just to do the lookup...
Rather then using a HashTable directly I would create a NodeCollection class
that inherits DictionaryBase that encapsulates the creation of the
Coordinate class.
Something like:
Public Structure
Dim x, y as integer
Dim label as string
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal label
As String)
Me.x = x
Me.y = y
Me.Label = label
End Sub
End Structure
Public Class NodeCollection
Inherits DictionaryBase
Public Sub Add(ByVal value As Node)
Dim key As New Coordinate(value.x, value.y)
Me.InnerHashtable.Add(key, value)
End Sub
Default Public Property Item(ByVal x As Integer, ByVal y As Integer)
As Node
Get
Dim key As New Coordinate(x, y)
Return DirectCast(Me.InnerHashtable.Item(key), Node)
End Get
Set(ByVal value As Node)
Dim key As New Coordinate(x, y)
Me.InnerHashtable.Item(key) = value
End Set
End Property
End Class
Dim collection As New NodeCollection
collection.Add(New Node(1, 1, "a"))
collection.Add(New Node(2, 3, "b"))
collection.Add(New Node(5, 4, "c"))
value = collection.Item(2, 3)
Hope this helps
Jay
"Sam" <qd*@datawave.ca> wrote in message
news:en***************@TK2MSFTNGP11.phx.gbl... Hi Everyone,
I have a stucture below stored in an arraylist and I want to check user's
input (point x,y) to make sure there is no duplicate point x,y entered
(string label can be duplicated). Is there a way to compare the new input
point x,y without having to a loop and compare the whole object in the
array
list. I can't use the indexof method because it would compare the whole
object.
Public Structure
Dim x, y as integer
Dim label as string
End Structure
Sam