有关事件和对象数组的问题 [英] Question about events and object arrays

查看:72
本文介绍了有关事件和对象数组的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




终于想到了事件和代表们!感谢这个

社区的人们帮助我。


所以现在我有一个课程可以举办活动。

现在,如果我实例化该类的对象数组,并且事件触发,那么是否有任何方法可以获取引发该事件的对象的数组索引?


先谢谢。


Pete。

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.

推荐答案

Peter,


是的,有。由于您将为数组中的所有

项使用一个事件处理程序,因此您可以遍历数组,然后将对象

refrences与对象的发送方进行比较(这假设您遵循事件处理程序的

模式,其中第一个参数是

触发事件的对象,第二个参数是EventArgs类型或派生自EventArgs。

别担心,我会等你做出改变。完成?好的=))。一旦

你这样做,你就会得到你的索引。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse。 com

" Peter Krikelis" < Pe的*********** @ discussions.microsoft.com>在消息中写道

新闻:8D ********************************** @ microsof t.com ...
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...


终于想到事件和代表了!感谢这个
社区中的人帮助了我。

所以现在我有一个提升事件的课程。

现在如果我实例化一个对象数组那个类,并且事件触发,
是否有任何方法可以获得引发该事件的对象的数组索引?

提前致谢。

Pete。
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.



Nicholas,


我认为你是那个人首先,请指出

中事件处理程序的范例。我做到了。

我的事件处理程序现在接受sender的参数类型对象和

" derivedEventArgs"它来自System.EventArgs。


我在寻找的是如何比较对象引用或循环通过

该数组。你的意思是使用for循环吗?


谢谢,


Pete。


" Nicholas Paldino [.NET / C#MVP]"写道:
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:
彼得,

是的,有。由于您将为数组中的所有项目使用一个事件处理程序,您可以循环遍历数组,然后将对象
refrence与对象的发送者进行比较(假设您遵循
事件处理程序的模式,其中第一个参数是
触发事件的对象,第二个参数是EventArgs类型或派生自EventArgs。
别担心,我会等你做了改变。完成?好的=))。一旦你这样做,你就会得到你的索引。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP ]
- mv*@spam.guard.caspershouse.com

" Peter Krikelis" < Pe的*********** @ discussions.microsoft.com>在消息中写道
新闻:8D ********************************** @ microsof t.com。 ..
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...


终于想到事件和代表了!感谢这个
社区中的人帮助了我。

所以现在我有一个提升事件的课程。

现在如果我实例化一个对象数组那个类,并且事件触发,
是否有任何方法可以获得引发该事件的对象的数组索引?

提前致谢。

Pete。
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.




Peter,


是的,我知道。我假设事件处理程序可以访问原始对象

数组。在这种情况下,枚举数组中的每个元素,

比较对象引用(确保使用

对象上的静态ReferenceEquals方法)到发送方。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Peter Krikelis" < Pe的*********** @ discussions.microsoft.com>在消息中写道

新闻:54 ********************************** @ microsof t.com ...
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,

我认为你是第一个指向事件处理程序范例的人。我做到了。
我的事件处理程序现在接受发送者的参数。类型对象和
derivedEventArgs它来自System.EventArgs。

我想要的是如何比较对象引用或循环
到该数组。你的意思是使用for循环吗?

谢谢,

Pete。

Nicholas Paldino [.NET / C#MVP]"写道:
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:
彼得,

是的,有。由于您将为数组中的所有项目使用一个事件处理程序,因此您可以遍历数组,然后将对象
引用与对象的发送者进行比较(这假设您遵循事件处理程序的
模式,其中第一个参数是
触发事件的对象,第二个参数是EventArgs类型或派生自事件处理器。
别担心,我会等你做出改变。完成?好的=))。一旦你这样做,你就会得到你的索引。

希望这会有所帮助。

-
- Nicholas Paldino [.NET / C#MVP ]
- mv*@spam.guard.caspershouse.com

" Peter Krikelis" < Pe的*********** @ discussions.microsoft.com>写在
消息
新闻:8D ********************************** @ microsof t.com ...
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...
>
>
>终于想到了事件和代表们!感谢此人中的人们
>帮助我的社区。
>
>所以现在我有一个提升活动的课程。
>
>现在,如果我实例化该类的对象数组,并且事件
>火灾,
>是
>有任何方法可以获得引发的对象的数组索引
>活动?
>
>在此先感谢。
>
> Pete。
> 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.




这篇关于有关事件和对象数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆