473,761 Members | 9,284 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1828
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.co m
"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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
"derivedEventAr gs" which derives from System.EventArg s.

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.co m
"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:8D******** *************** ***********@mic rosoft.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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:54******** *************** ***********@mic rosoft.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
"derivedEventAr gs" which derives from System.EventArg s.

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.co m
"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
message
news:8D******** *************** ***********@mic rosoft.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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:54******** *************** ***********@mic rosoft.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
"derivedEventAr gs" which derives from System.EventArg s.

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.co m
"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
message
news:8D******** *************** ***********@mic rosoft.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.Le ngth && sender != controlArray[i];
i++)
{ }
if (i >= controlArray.Le ngth)
{
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.Le ngth && sender != controlArray[i];
i++)
{ }
if (i >= controlArray.Le ngth)
{
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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:9F******** *************** ***********@mic rosoft.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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
message
news:54******** *************** ***********@mic rosoft.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
> "derivedEventAr gs" which derives from System.EventArg s.
>
> 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.co m
>>
>>
>> "Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
>> message
>> news:8D******** *************** ***********@mic rosoft.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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in message
news:9F******** *************** ***********@mic rosoft.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.co m

"Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
message
news:54******** *************** ***********@mic rosoft.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
> "derivedEventAr gs" which derives from System.EventArg s.
>
> 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.co m
>>
>>
>> "Peter Krikelis" <Pe***********@ discussions.mic rosoft.com> wrote in
>> message
>> news:8D******** *************** ***********@mic rosoft.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
11778
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
3221
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 column. Once the array elements are split, what is the best way to sort them? Thank you. //populate data object with data from xml file. //Data is a comma delimited list of values var jsData = new Array(); jsData = {lib: "#field...
5
2516
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: System.Runtime.InteropServices.Marshal.ReleaseComObject(Obj);
24
3458
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 declared an array as: char *stringArray = {"one","two","three","a"}; When I pass the array using:
7
6442
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 now thown out of the array released properly by the CLI?
14
1776
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, but I purposely had this array filled unsequentially because the key for the array is meant to act as an ID which has a contextual meaning in my script. The problem, of course, is that for...in also returns my method 'contains' as one of the...
31
1910
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 1-dim array, each of whose elements is an array. Looking at the debugger I use, arr is shown as an initial value of "2", but when expanded, there are 2 consecutive arrays of 13 elements. So, may I ask this? Is there anything special that marks the...
4
237
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: this.TextBox2.TextChanged += new EventHandler(TextBox2_TextChanged); and then have the function. void TextBox2_TextChanged(object sender, EventArgs e)
2
1821
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 "Cproject" When i add items to the lists of "Csystem" i also add eventhandlers for the above events. When the object structure is serialized to an xml-file, and then deserialized, my event handlers are lost, since at the deserialization of "Cproject",...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9376
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9988
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9923
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.