473,612 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare part of object in an arraylist

Sam
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
Nov 21 '05 #1
3 2293
Hi Sam

Why don't you use a Hashtable, with a String key formed by concatenating x ,
y, that way you can test with ContainsKey?

HTH

Nigel Armstrsong

"Sam" wrote:
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

Nov 21 '05 #2
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.Index Of 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.GetHashCod e() Xor Me.Y.GetHashCod e()
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(DirectCa st(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.E quals(other.Coo rdinate)
End Function

Public Overloads Overrides Function Equals(ByVal obj As Object) As
Boolean
If TypeOf obj Is Node Then
Return Equals(DirectCa st(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(Ne w 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(valu e.x, value.y)
Me.InnerHashtab le.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.I nnerHashtable.I tem(key), Node)
End Get
Set(ByVal value As Node)
Dim key As New Coordinate(x, y)
Me.InnerHashtab le.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.c a> wrote in message
news:en******** *******@TK2MSFT NGP11.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

Nov 21 '05 #3
Sam
Thanks so lots guys. This solves my problem

Sam

"Sam" <qd*@datawave.c a> wrote in message
news:en******** *******@TK2MSFT NGP11.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

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
9506
by: David zhu | last post by:
I've got different result when comparing two strings using "==" and string.Compare(). The two strings seems to have same value "1202002" in the quick watch, and both have the same length 7 which I have tried to print out by debug.writeline(). But the "==" operator results false, and string.Compare() results true. Somebody helps me!
0
1232
by: Ivan | last post by:
Hi there My work on threads continues with more or less success. Here is what I'm trying to do: Class JobAgent is incharged for some tasks and when it's called it starts thread which performs the job. Application contains one list of agents that are idle at the moment and list of busy agents. In loop it checks if there are agents in idle list and if there are some, it starts them.
17
8220
by: bengamin | last post by:
Hi, I have a C# class and two instance of the class; the class have some property. I want to compare the property value of the two instance How should i do? override == ? use delegate ?
1
29842
by: bengamin | last post by:
Hi, I declare two ArrayList variables.How can I compare if the two ArrayList are Value Equal. Thanks! Ben
3
5874
by: Frank | last post by:
Hi! I'm kinda stuck here. I have an Hashtable with with my custom "Item" Object. The key used in the Hashtable is the "Item.Id", i have selected this key because with a lot of functions i can get the right object from the hashtable really fast. But now i need a function whom can get an object from the hashtable based on the values each "Item" object holds. ("Item.ColorId","Item.SizeId","Item.OtherId"), but i can't identify the object...
4
7579
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
3
9687
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList atemp = new ArrayList(); // THE ARRAY StreamWriter sw = new StreamWriter(@"C:\temp\fred.html");
3
10548
by: raylopez99 | last post by:
This is an example of using multiple comparison criteria for IComparer/ Compare/CompareTo for List<and Array. Adapted from David Hayden's tutorial found on the net, but he used ArrayList so the format was different. Basically you can sort a class having members LastName (string), FirstName (string) and Age (int) according to whether you want to sort by Last Name, First Name or Age, using the .Sort function
5
2476
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I have a Container that is an an Array List of Class Each ArrayList element can be the class or a another ArrayList of Class So there the ArrayList could look like Element 1 - Class Element 2 - Class Element 1 - ArrayList Of Class Element 1 - Class
0
8105
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8565
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6076
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5532
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4045
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4109
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1695
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1413
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.