PC Review


Reply
Thread Tools Rate Thread

Array.IndexOf and structures

 
 
Zanna
Guest
Posts: n/a
 
      23rd May 2005
Hi all!

Problem: let's say I have a structure

Public Structure MyObject

Public Id As String

Public Description As String

Public Overrides Function Equals(ByVal obj As Object) As Boolean

If TypeOf obj Is String Then

Return DirectCast(obj, String).Equals(Me.Id)

Else

Return DirectCast(obj, Deposito).Id.Equals(Me.Id)

End If

End Function

End Structure

Now, have an array of MyObject:

Dim myObj(3) As MyObject

I fill the array and then I do

Dim idx As Int32 = Array.IndexOf(myObj, "MyId", 0, myObj.Length)

This last line give to me always -1, but I'm sure that the Equals() works.
MSDN says that the Array.IndexOf works on the override of Object.Equals(),
so I really don't know why this does not works for me

Some help?


 
Reply With Quote
 
 
 
 
Marius Bucur
Guest
Posts: n/a
 
      23rd May 2005
Hi,
change MyObject declaration to be a class instead of a structure. If the
program works than is a boxing problem. Structures are recommended for
immutable types.
Marius
"Zanna" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi all!
>
> Problem: let's say I have a structure
>
> Public Structure MyObject
>
> Public Id As String
>
> Public Description As String
>
> Public Overrides Function Equals(ByVal obj As Object) As Boolean
>
> If TypeOf obj Is String Then
>
> Return DirectCast(obj, String).Equals(Me.Id)
>
> Else
>
> Return DirectCast(obj, Deposito).Id.Equals(Me.Id)
>
> End If
>
> End Function
>
> End Structure
>
> Now, have an array of MyObject:
>
> Dim myObj(3) As MyObject
>
> I fill the array and then I do
>
> Dim idx As Int32 = Array.IndexOf(myObj, "MyId", 0, myObj.Length)
>
> This last line give to me always -1, but I'm sure that the Equals() works.
> MSDN says that the Array.IndexOf works on the override of Object.Equals(),
> so I really don't know why this does not works for me
>
> Some help?
>



 
Reply With Quote
 
Zanna
Guest
Posts: n/a
 
      24th May 2005
"Marius Bucur" <(E-Mail Removed)> ha scritto nel messaggio

> change MyObject declaration to be a class instead of a structure. If the
> program works than is a boxing problem. Structures are recommended for
> immutable types.


mmm... But the ToString() works i.e. I add the structure to a listbox...

I'd like to use a structure because it would be faster for few data.


 
Reply With Quote
 
Marius Bucur
Guest
Posts: n/a
 
      24th May 2005
ValueType[] arrays contain references to boxed valuetypes elements.
Consider the following code
struct MyStruct
{
public string id;
}

....
void foo()
{
MyStruct[] ar = new MyStruct[10];
ar[0].id = "Test";

MyStruct val = ar[0]; //unboxing is perform
val.id = "NewValue";
//now ar[0].id == "Test" and val.id = "NewValue"
//you want to modify the element in the collection you must either use
// ar[0].ID = "NewValue" (in this case unboxing is not performed) or put
the new value in
//the collection ar[0] = val'
}

I don't think that in a situation like this if you use a struct instead of a
class your code will be faster, consider making a test.

Struct Usage Guidelines (cat from msdn)
It is recommended that you use a struct for types that meet any of the
following criteria:
- Act like primitive types.
- Have an instance size under 16 bytes.
- Are immutable.
- Value semantics are desirable.

Marius

"Zanna" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Marius Bucur" <(E-Mail Removed)> ha scritto nel messaggio
>
>> change MyObject declaration to be a class instead of a structure. If the
>> program works than is a boxing problem. Structures are recommended for
>> immutable types.

>
> mmm... But the ToString() works i.e. I add the structure to a listbox...
>
> I'd like to use a structure because it would be faster for few data.
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IndexOf(string array) Can it be done? =?Utf-8?B?SlA=?= Microsoft Dot NET 6 31st Mar 2006 09:28 PM
Array.IndexOf - not working Al Microsoft VB .NET 11 7th Jul 2005 06:26 PM
how to marshal an array of unmanaged structure into a managed array of structures. amags kumar via .NET 247 Microsoft Dot NET Framework 0 4th Jun 2005 01:55 AM
Can we use indexof in a multidimernsional array? pp Microsoft VB .NET 0 6th May 2005 03:00 PM
Array.IndexOf() and an array of structs Arkion Microsoft Dot NET Framework 6 9th Sep 2004 06:53 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:36 PM.