Search:
Namespaces
Discussions
.NET v1.1
Feedback
Array.IndexOf() and an array of structs
Messages
Related Types
This message was discovered on
microsoft.public.dotnet.framework
.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...
Arkion
Hi ng,
I would like to search an array of structs for an object whose myName
field (a string) equals the given input search string. Is this
possible to do with IndexOf? Here's an example:
---------------------
public struct SomeRecord
{
string myName;
int idata;
float fdata;
}
SomeRecord[] records = new SomeRecord[10];
// Load array with some data...
// Look for first SomeRecord object whose myName field equals "John"
int index = records.IndexOf("John");
---------------------
TIA.
Reply to this message...
Sijin Joseph
No this is not possible using IndexOf, IndexOf uses compares using
object references and not the actual values of the object.
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Arkion wrote:
[Original message clipped]
Reply to this message...
Jon Skeet [C# MVP] (VIP)
Sijin Joseph <
Click here to reveal e-mail address
> wrote:
[Original message clipped]
Not true.
Admittedly the syntax given doesn't quite work due to there not being
such a method, but
Array
.IndexOf(
Array
, object) uses Equals:
using System;
public class Test
{
public static void Main()
{
Test[] t = new Test[1];
t[0] = new Test();
Console
.WriteLine (
Array
.IndexOf(t, new Test()));
}
public override bool Equals(object o)
{
Console
.WriteLine ("Equals being called");
return true;
}
}
Output:
Equals being called
0
Here's a version using structs with the default value type
implementation of Equals:
using System;
struct Foo
{
public int x;
}
public class Test
{
public static void Main()
{
Foo[] array = new Foo[3];
array[0].x=3;
array[1].x=6;
array[2].x=9;
Foo f = new Foo();
f.x = 6;
Console
.WriteLine (
Array
.IndexOf(array, f));
}
}
--
Jon Skeet - <
Click here to reveal e-mail address
>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
Sijin Joseph
Ahhh...I knew i should have used Reflector to confirm before posting that :)
Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
Jon Skeet [C# MVP] wrote:
[Original message clipped]
Reply to this message...
Peter Huang (VIP)
Hi Arkion,
Do you mean two structures will be equal as long as the two stuctures'
myName field is equal?
If so, I think we may just try to override the Equals method.
public struct SomeRecord
{
public string myName;
public int idata;
public float fdata;
public override bool Equals(object obj)
{
//return base.Equals (obj);
if (this.myName == ((SomeRecord)obj).myName)
return true;
return false;
}
}
static void Main(string[] args)
{
SomeRecord[] records = new SomeRecord[10];
records[4].myName = "hello";
records[4].idata = 10;
SomeRecord rd = new SomeRecord();;
rd.myName = "hello";
rd.idata = 1;
int index =
Array
.IndexOf(records,rd);
Console.WriteLine(index);
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
Arkion
On Wed, 08 Sep 2004 02:07:21 GMT,
Click here to reveal e-mail address
("Peter Huang") wrote:
[Original message clipped]
Hmm, so you need to construct an instance of SomeRecord for it to
work. I guess I'm better off just to write a member function that
simply loops through the given array of SomeRecords.
Reply to this message...
Peter Huang (VIP)
Hi Arkion,
Your approach is an workaround too. If you still have any concern, please
feel free to post here.
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Reply to this message...
System.Array
System.Console
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com