Re: Question about events and object arrays

From: Peter Krikelis (PeterKrikelis_at_discussions.microsoft.com)
Date: 12/07/04


Date: Tue, 7 Dec 2004 11:51:02 -0800

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]
> - mvp@spam.guard.caspershouse.com
>
> "Peter Krikelis" <PeterKrikelis@discussions.microsoft.com> wrote in message
> news:9F9A9622-54B8-4C93-B500-F0ADB6CC67E6@microsoft.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]
> >> - mvp@spam.guard.caspershouse.com
> >>
> >> "Peter Krikelis" <PeterKrikelis@discussions.microsoft.com> wrote in
> >> message
> >> news:5412E712-02B6-4099-8446-B0D579F791F6@microsoft.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]
> >> >> - mvp@spam.guard.caspershouse.com
> >> >>
> >> >>
> >> >> "Peter Krikelis" <PeterKrikelis@discussions.microsoft.com> wrote in
> >> >> message
> >> >> news:8DAC6909-8D89-4029-9430-68FEF6392645@microsoft.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.
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>
>



Relevant Pages

  • Re: About Thread Completion Notify.
    ... This is the reason that i must keep track of the threads and they have to ... i remove it from the array. ... > (which you probably don't since you are servicing requests in separate ... you shouldn't have a need for waiting on events. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: please confirm something (jdbc)
    ... of its internal buffer so that will double the amount of memory that you need. ... That's the reason I wrote my own dynamic byte array ... Given all the memory re-allocation that goes on inside the ByteArrayOutputStream anyway, I'm not sure that one more allocation is all that much extra overhead. ... If I have two ways of coding something, and I know that one will definitely use less memory (or be quicker for whatever reason) then I will choose the one "better" one. ...
    (comp.lang.java.programmer)
  • Re: Help needed on Arrays
    ... What does the SQL Stored Procedure do? ... Is there a specific reason you want to create an extra ... be in an array or arraylist. ... The language is vb.net 2005. ...
    (microsoft.public.vsnet.general)
  • Re: what is the best datatype for..
    ... array that will be scanned searching for the index value, ... It also won't be flexible if the input data changes for any reason, since the implementation itself is so dependent on the input data. ... Calling the dictionary solution "inefficient" is just plain dumb; it might not be quite as fast as a straight array lookup, but it's not like it's a _slow_ design. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Linux 2.6.29
    ... thats changed by Ingo Molnar back in september. ... of the array at the time of testing.. ... disk when your system suffers from large write latency? ...
    (Linux-Kernel)