PC Review


Reply
Thread Tools Rate Thread

Question about events and object arrays

 
 
=?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?=
Guest
Posts: n/a
 
      7th Dec 2004
Hi,

Finally figured events and delegates whew! Thanks to the people in this
community that helped me out.

So now I have a class that raises an event.

Now if I instantiate an object array of that class, and the event fires, is
there any way of getting the array index of the object that raised that event?

Thanks in advance.

Pete.
 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      7th Dec 2004
Peter,

Yes, there is. Since you would be using one event handler for all the
items in the array, you can cycle through the array and then compare object
refrences with the sender of the object (this is assuming you followed the
pattern for event handlers, where the first parameter is the object that
fired the event, the second is of type EventArgs or derived from EventArgs.
Don't worry, I'll wait while you make the changes. Done? Ok =) ). Once
you do that, you will have your index.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)


"Peter Krikelis" <(E-Mail Removed)> wrote in message
news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
> Hi,
>
> Finally figured events and delegates whew! Thanks to the people in this
> community that helped me out.
>
> So now I have a class that raises an event.
>
> Now if I instantiate an object array of that class, and the event fires,
> is
> there any way of getting the array index of the object that raised that
> event?
>
> Thanks in advance.
>
> Pete.



 
Reply With Quote
 
 
 
 
=?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?=
Guest
Posts: n/a
 
      7th Dec 2004
Nicholas,

I think you were the one to point me to the paradigm of event handlers in
the first place. I did that.
My event handler now takes arguments of "sender" type object and
"derivedEventArgs" which derives from System.EventArgs.

What I was looking for is how to compare object references or cycle through
that array. Do you mean using a for loop?

Thanks,

Pete.



"Nicholas Paldino [.NET/C# MVP]" wrote:

> Peter,
>
> Yes, there is. Since you would be using one event handler for all the
> items in the array, you can cycle through the array and then compare object
> refrences with the sender of the object (this is assuming you followed the
> pattern for event handlers, where the first parameter is the object that
> fired the event, the second is of type EventArgs or derived from EventArgs.
> Don't worry, I'll wait while you make the changes. Done? Ok =) ). Once
> you do that, you will have your index.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
>
> "Peter Krikelis" <(E-Mail Removed)> wrote in message
> news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
> > Hi,
> >
> > Finally figured events and delegates whew! Thanks to the people in this
> > community that helped me out.
> >
> > So now I have a class that raises an event.
> >
> > Now if I instantiate an object array of that class, and the event fires,
> > is
> > there any way of getting the array index of the object that raised that
> > event?
> >
> > Thanks in advance.
> >
> > Pete.

>
>
>

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      7th Dec 2004
Peter,

Yes, I do. I assume the event handler has access to the original object
array. In this case, enumerate through each of these elements in the array,
comparing the object reference (use the static ReferenceEquals method on
object to be sure) to the sender.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Peter Krikelis" <(E-Mail Removed)> wrote in message
news:5412E712-02B6-4099-8446-(E-Mail Removed)...
> Nicholas,
>
> I think you were the one to point me to the paradigm of event handlers in
> the first place. I did that.
> My event handler now takes arguments of "sender" type object and
> "derivedEventArgs" which derives from System.EventArgs.
>
> What I was looking for is how to compare object references or cycle
> through
> that array. Do you mean using a for loop?
>
> Thanks,
>
> Pete.
>
>
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Peter,
>>
>> Yes, there is. Since you would be using one event handler for all
>> the
>> items in the array, you can cycle through the array and then compare
>> object
>> refrences with the sender of the object (this is assuming you followed
>> the
>> pattern for event handlers, where the first parameter is the object that
>> fired the event, the second is of type EventArgs or derived from
>> EventArgs.
>> Don't worry, I'll wait while you make the changes. Done? Ok =) ). Once
>> you do that, you will have your index.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - (E-Mail Removed)
>>
>>
>> "Peter Krikelis" <(E-Mail Removed)> wrote in
>> message
>> news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
>> > Hi,
>> >
>> > Finally figured events and delegates whew! Thanks to the people in this
>> > community that helped me out.
>> >
>> > So now I have a class that raises an event.
>> >
>> > Now if I instantiate an object array of that class, and the event
>> > fires,
>> > is
>> > there any way of getting the array index of the object that raised that
>> > event?
>> >
>> > Thanks in advance.
>> >
>> > Pete.

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?=
Guest
Posts: n/a
 
      7th Dec 2004
Nicholas,

Thanks for the clarification.

However, is there another way of doing that? The reason I ask is because if
you have an array of a 100,000 objects theoretically, then looping through
the entire array would be expensive. Compund it with the fact that the events
are happening fairly quickly and in repeated succession.

Is there a way of passing a handle of the specific array object to the class
that raises the event handler, and return that handle with the event data?

Thank you,

Pete.


"Nicholas Paldino [.NET/C# MVP]" wrote:

> Peter,
>
> Yes, I do. I assume the event handler has access to the original object
> array. In this case, enumerate through each of these elements in the array,
> comparing the object reference (use the static ReferenceEquals method on
> object to be sure) to the sender.
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Peter Krikelis" <(E-Mail Removed)> wrote in message
> news:5412E712-02B6-4099-8446-(E-Mail Removed)...
> > Nicholas,
> >
> > I think you were the one to point me to the paradigm of event handlers in
> > the first place. I did that.
> > My event handler now takes arguments of "sender" type object and
> > "derivedEventArgs" which derives from System.EventArgs.
> >
> > What I was looking for is how to compare object references or cycle
> > through
> > that array. Do you mean using a for loop?
> >
> > Thanks,
> >
> > Pete.
> >
> >
> >
> > "Nicholas Paldino [.NET/C# MVP]" wrote:
> >
> >> Peter,
> >>
> >> Yes, there is. Since you would be using one event handler for all
> >> the
> >> items in the array, you can cycle through the array and then compare
> >> object
> >> refrences with the sender of the object (this is assuming you followed
> >> the
> >> pattern for event handlers, where the first parameter is the object that
> >> fired the event, the second is of type EventArgs or derived from
> >> EventArgs.
> >> Don't worry, I'll wait while you make the changes. Done? Ok =) ). Once
> >> you do that, you will have your index.
> >>
> >> Hope this helps.
> >>
> >>
> >> --
> >> - Nicholas Paldino [.NET/C# MVP]
> >> - (E-Mail Removed)
> >>
> >>
> >> "Peter Krikelis" <(E-Mail Removed)> wrote in
> >> message
> >> news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
> >> > Hi,
> >> >
> >> > Finally figured events and delegates whew! Thanks to the people in this
> >> > community that helped me out.
> >> >
> >> > So now I have a class that raises an event.
> >> >
> >> > Now if I instantiate an object array of that class, and the event
> >> > fires,
> >> > is
> >> > there any way of getting the array index of the object that raised that
> >> > event?
> >> >
> >> > Thanks in advance.
> >> >
> >> > Pete.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      7th Dec 2004
If you're using a plain ol' array, you can do this:

Control[] controlArray = ... ;
for (int i = 0; i < controlArray.Length && sender != controlArray[i];
i++)
{ }
if (i >= controlArray.Length)
{
i = -1;
}

i will then contain the index of the control in the array, or will be
-1 if the control was not found.

However, if you don't need the controls to be stored in any sort of
order, I would suggest a Hashtable instead. For each new control, add
it to the Hashtable:

Hashtable controls = new Hashtable();
Control aControl = new ... ;
controls[aControl] = aControl;

then you can find out if the control is in the Hashtable in one line of
code:

if (controls[sender] != null)
{
.... control has been found...
}

Of course, as I said, it all depends upon whether you need the controls
to be stored in order. (Event then, look into SortedList and other
aggregate structures to see if one offers all of the features you need.
There's more to life than arrays!)

 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      7th Dec 2004
If you're using a plain ol' array, you can do this:

Control[] controlArray = ... ;
for (int i = 0; i < controlArray.Length && sender != controlArray[i];
i++)
{ }
if (i >= controlArray.Length)
{
i = -1;
}

i will then contain the index of the control in the array, or will be
-1 if the control was not found.

However, if you don't need the controls to be stored in any sort of
order, I would suggest a Hashtable instead. For each new control, add
it to the Hashtable:

Hashtable controls = new Hashtable();
Control aControl = new ... ;
controls[aControl] = aControl;

then you can find out if the control is in the Hashtable in one line of
code:

if (controls[sender] != null)
{
.... control has been found...
}

Of course, as I said, it all depends upon whether you need the controls
to be stored in order. (Event then, look into SortedList and other
aggregate structures to see if one offers all of the features you need.
There's more to life than arrays!)

 
Reply With Quote
 
john morales
Guest
Posts: n/a
 
      7th Dec 2004
Hello Peter,

I'm not sure why you would need the array index of the item, since you already
have a reference to the object in question. But if you could change your
array to an arraylist then you could find the index by using the BinarySearch
method.

But again if you already have a reference to the object (through the sender
param of the even) why get the index?

-john

> Hi,
>
> Finally figured events and delegates whew! Thanks to the people in
> this community that helped me out.
>
> So now I have a class that raises an event.
>
> Now if I instantiate an object array of that class, and the event
> fires, is there any way of getting the array index of the object that
> raised that event?
>
> Thanks in advance.
>
> Pete.
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      7th Dec 2004
Peter,

In this situation, I would recommend that you give the object some sense
of the index that it is at. For example, when you assign it in an array,
you would assign a property indicating the index in the array it is at.

However, I can see where people would cringe at this.

You might want to consider having a wrapper for the array which exposes
the event. The EventArgs-derived class would have an index on it,
indicating the index of the item that fired the event, and the sender would
have the actual object.

I'm curious though, what do you need the index of the item in the array
for if you have the sender?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Peter Krikelis" <(E-Mail Removed)> wrote in message
news:9F9A9622-54B8-4C93-B500-(E-Mail Removed)...
> Nicholas,
>
> Thanks for the clarification.
>
> However, is there another way of doing that? The reason I ask is because
> if
> you have an array of a 100,000 objects theoretically, then looping through
> the entire array would be expensive. Compund it with the fact that the
> events
> are happening fairly quickly and in repeated succession.
>
> Is there a way of passing a handle of the specific array object to the
> class
> that raises the event handler, and return that handle with the event data?
>
> Thank you,
>
> Pete.
>
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Peter,
>>
>> Yes, I do. I assume the event handler has access to the original
>> object
>> array. In this case, enumerate through each of these elements in the
>> array,
>> comparing the object reference (use the static ReferenceEquals method on
>> object to be sure) to the sender.
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - (E-Mail Removed)
>>
>> "Peter Krikelis" <(E-Mail Removed)> wrote in
>> message
>> news:5412E712-02B6-4099-8446-(E-Mail Removed)...
>> > Nicholas,
>> >
>> > I think you were the one to point me to the paradigm of event handlers
>> > in
>> > the first place. I did that.
>> > My event handler now takes arguments of "sender" type object and
>> > "derivedEventArgs" which derives from System.EventArgs.
>> >
>> > What I was looking for is how to compare object references or cycle
>> > through
>> > that array. Do you mean using a for loop?
>> >
>> > Thanks,
>> >
>> > Pete.
>> >
>> >
>> >
>> > "Nicholas Paldino [.NET/C# MVP]" wrote:
>> >
>> >> Peter,
>> >>
>> >> Yes, there is. Since you would be using one event handler for all
>> >> the
>> >> items in the array, you can cycle through the array and then compare
>> >> object
>> >> refrences with the sender of the object (this is assuming you followed
>> >> the
>> >> pattern for event handlers, where the first parameter is the object
>> >> that
>> >> fired the event, the second is of type EventArgs or derived from
>> >> EventArgs.
>> >> Don't worry, I'll wait while you make the changes. Done? Ok =) ).
>> >> Once
>> >> you do that, you will have your index.
>> >>
>> >> Hope this helps.
>> >>
>> >>
>> >> --
>> >> - Nicholas Paldino [.NET/C# MVP]
>> >> - (E-Mail Removed)
>> >>
>> >>
>> >> "Peter Krikelis" <(E-Mail Removed)> wrote in
>> >> message
>> >> news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
>> >> > Hi,
>> >> >
>> >> > Finally figured events and delegates whew! Thanks to the people in
>> >> > this
>> >> > community that helped me out.
>> >> >
>> >> > So now I have a class that raises an event.
>> >> >
>> >> > Now if I instantiate an object array of that class, and the event
>> >> > fires,
>> >> > is
>> >> > there any way of getting the array index of the object that raised
>> >> > that
>> >> > event?
>> >> >
>> >> > Thanks in advance.
>> >> >
>> >> > Pete.
>> >>
>> >>
>> >>

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?=
Guest
Posts: n/a
 
      7th Dec 2004
Nicholas,

Thank you for the info. The reason was that I was experimenting making as
dum linear data structure and thats why I needed the indices, but as Bruce
Wood suggested, I might be better off using the HashTable class.

I am new to C# and OOP and I am just getting my hands dirty, thats all.

Thanks,

Pete.

"Nicholas Paldino [.NET/C# MVP]" wrote:

> Peter,
>
> In this situation, I would recommend that you give the object some sense
> of the index that it is at. For example, when you assign it in an array,
> you would assign a property indicating the index in the array it is at.
>
> However, I can see where people would cringe at this.
>
> You might want to consider having a wrapper for the array which exposes
> the event. The EventArgs-derived class would have an index on it,
> indicating the index of the item that fired the event, and the sender would
> have the actual object.
>
> I'm curious though, what do you need the index of the item in the array
> for if you have the sender?
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - (E-Mail Removed)
>
> "Peter Krikelis" <(E-Mail Removed)> wrote in message
> news:9F9A9622-54B8-4C93-B500-(E-Mail Removed)...
> > Nicholas,
> >
> > Thanks for the clarification.
> >
> > However, is there another way of doing that? The reason I ask is because
> > if
> > you have an array of a 100,000 objects theoretically, then looping through
> > the entire array would be expensive. Compund it with the fact that the
> > events
> > are happening fairly quickly and in repeated succession.
> >
> > Is there a way of passing a handle of the specific array object to the
> > class
> > that raises the event handler, and return that handle with the event data?
> >
> > Thank you,
> >
> > Pete.
> >
> >
> > "Nicholas Paldino [.NET/C# MVP]" wrote:
> >
> >> Peter,
> >>
> >> Yes, I do. I assume the event handler has access to the original
> >> object
> >> array. In this case, enumerate through each of these elements in the
> >> array,
> >> comparing the object reference (use the static ReferenceEquals method on
> >> object to be sure) to the sender.
> >>
> >> --
> >> - Nicholas Paldino [.NET/C# MVP]
> >> - (E-Mail Removed)
> >>
> >> "Peter Krikelis" <(E-Mail Removed)> wrote in
> >> message
> >> news:5412E712-02B6-4099-8446-(E-Mail Removed)...
> >> > Nicholas,
> >> >
> >> > I think you were the one to point me to the paradigm of event handlers
> >> > in
> >> > the first place. I did that.
> >> > My event handler now takes arguments of "sender" type object and
> >> > "derivedEventArgs" which derives from System.EventArgs.
> >> >
> >> > What I was looking for is how to compare object references or cycle
> >> > through
> >> > that array. Do you mean using a for loop?
> >> >
> >> > Thanks,
> >> >
> >> > Pete.
> >> >
> >> >
> >> >
> >> > "Nicholas Paldino [.NET/C# MVP]" wrote:
> >> >
> >> >> Peter,
> >> >>
> >> >> Yes, there is. Since you would be using one event handler for all
> >> >> the
> >> >> items in the array, you can cycle through the array and then compare
> >> >> object
> >> >> refrences with the sender of the object (this is assuming you followed
> >> >> the
> >> >> pattern for event handlers, where the first parameter is the object
> >> >> that
> >> >> fired the event, the second is of type EventArgs or derived from
> >> >> EventArgs.
> >> >> Don't worry, I'll wait while you make the changes. Done? Ok =) ).
> >> >> Once
> >> >> you do that, you will have your index.
> >> >>
> >> >> Hope this helps.
> >> >>
> >> >>
> >> >> --
> >> >> - Nicholas Paldino [.NET/C# MVP]
> >> >> - (E-Mail Removed)
> >> >>
> >> >>
> >> >> "Peter Krikelis" <(E-Mail Removed)> wrote in
> >> >> message
> >> >> news:8DAC6909-8D89-4029-9430-(E-Mail Removed)...
> >> >> > Hi,
> >> >> >
> >> >> > Finally figured events and delegates whew! Thanks to the people in
> >> >> > this
> >> >> > community that helped me out.
> >> >> >
> >> >> > So now I have a class that raises an event.
> >> >> >
> >> >> > Now if I instantiate an object array of that class, and the event
> >> >> > fires,
> >> >> > is
> >> >> > there any way of getting the array index of the object that raised
> >> >> > that
> >> >> > event?
> >> >> >
> >> >> > Thanks in advance.
> >> >> >
> >> >> > Pete.
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>

>
>
>

 
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
events that fire events that fire events....a bad thing? Daniel Microsoft C# .NET 3 1st Feb 2007 02:55 AM
Arrays - declaration, adding values to arrays and calculation Maxi Microsoft Excel Programming 1 17th Aug 2006 04:13 PM
newbie question about object arrays and events =?Utf-8?B?UGV0ZXIgS3Jpa2VsaXM=?= Microsoft C# .NET 1 2nd Dec 2004 03:14 PM
arrays in arrays jacob Microsoft Excel Programming 4 29th Aug 2004 08:40 PM
Rectangular arrays Vs. multidimensional arrays. Nikhil Patel Microsoft C# .NET 2 21st Jul 2003 02:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:54 PM.