Array.IndexOf() and an array of structs

  • Thread starter Thread starter Arkion
  • Start date Start date
A

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");
 
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.
 
Sijin Joseph said:
No this is not possible using IndexOf, IndexOf uses compares using
object references and not the actual values of the object.

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));
}
}
 
SomeRecord rd = new SomeRecord();;
rd.myName = "hello";
rd.idata = 1;
int index = Array.IndexOf(records,rd);
Console.WriteLine(index);
}

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.
 
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.
 
Ahhh...I knew i should have used Reflector to confirm before posting that :)

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph


Sijin Joseph said:
No this is not possible using IndexOf, IndexOf uses compares using
object references and not the actual values of the object.


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));
}
}
 
Back
Top