473,326 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Question about events and object arrays

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.
Nov 16 '05 #1
9 1802
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]
- mv*@spam.guard.caspershouse.com
"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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.

Nov 16 '05 #2
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]
- mv*@spam.guard.caspershouse.com
"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:8D**********************************@microsof t.com...
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.


Nov 16 '05 #3
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com
"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
message
news:8D**********************************@microsof t.com...
> 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.


Nov 16 '05 #4
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com
"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
message
news:8D**********************************@microsof t.com...
> 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.


Nov 16 '05 #5
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!)

Nov 16 '05 #6
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!)

Nov 16 '05 #7
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.

Nov 16 '05 #8
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
message
news:54**********************************@microsof t.com...
> 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]
>> - mv*@spam.guard.caspershouse.com
>>
>>
>> "Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
>> message
>> news:8D**********************************@microsof t.com...
>> > 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.
>>
>>
>>


Nov 16 '05 #9
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in message
news:9F**********************************@microsof t.com...
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]
- mv*@spam.guard.caspershouse.com

"Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
message
news:54**********************************@microsof t.com...
> 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]
>> - mv*@spam.guard.caspershouse.com
>>
>>
>> "Peter Krikelis" <Pe***********@discussions.microsoft.com> wrote in
>> message
>> news:8D**********************************@microsof t.com...
>> > 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.
>>
>>
>>


Nov 16 '05 #10

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

Similar topics

7
by: csx | last post by:
Hi everyone! two quick questions relating to arrays. Q1, Is it possible to re-assign array elements? int array = {{2,4}, {4,5}}; array = {2,3}
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
5
by: Michael Moreno | last post by:
Hello, In a class I have this code: public object Obj; If Obj is a COM object I would like to call in the Dispose() method the following code: ...
24
by: Michael | last post by:
Hi, I am trying to pass a function an array of strings, but I am having trouble getting the indexing to index the strings rather than the individual characters of one of the strings. I have...
7
by: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
14
by: ablock | last post by:
I have an array to which i have a added a method called contains. I would like to transverse this array using for...in...I understand fully that for...in is really meant for Objects and not Arrays,...
31
by: mdh | last post by:
I am still having a problem understanding K&RII on p 112. I have looked at the FAQs --which I am sure answer it in a way that I have missed, so here goes. A 2-dim array, (per K&R) is really a...
4
by: tshad | last post by:
I am just getting started with events and had a couple of questions on why they do what they do. If you have a textbox and you want to handle an event you can just do: ...
2
by: Rigor69 | last post by:
Hi There. I'm working on a application with a multiple level object structure. Object type "Cproject" holds an arraylist of "Csystem". A "Csystem" contain custom events i listen for in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.