Working with dynamically created objects

Hello,

  I am working with the FileSystemWatcher and need some help.  I am trying to monitor multiple files within a directory.  What I want to do is for each file that gets changed, create an object to monitor the changes and log those results.  My first question is how do I create an unknown number of objects dynamically without them falling out of scope?  Do I need an array of objects to do this?

My second question will probably be answered with the first, but I want each child object to be able to raise an event on the Parent object for logging the information.  How do I create an event that will be called by an unknown number of child objects?

Flow Chart so far:

1.  Monitor the directory with FileSystemWatcher
2.  If a file gets created, call the event CreateMonitorObject
3.  In CreateMonitorObject event, create a new object to monitor file that is being changed.  Multiple objects need to be created for each file being changed
4.  All files that get changed should have the MonitorObject raise an event of the Parent to log to a single file location.

I have steps 1 and 2 done, but 3 is the problem.
rmyhreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cupawntaeCommented:
You'll need a dynamic data structure, try ArrayList.

private ArrayList watchers=new ArrayList();

(depending on how your class is written, you may want this to be static).

When you create your new "monitor object" (any reason why this is not simply another FileSystemWatcher object watching the new file?), you add it to the list with:

watchers.Add(fileWatcher);

You can have multiple event handlers calling the same method, so each time you create a new wathcer, you simply use:

fileWatcher.Changed += new FileSystemEventHandler(parentObject.OnFileChanged);

You can use the FileSystemEventArgs parameter of your OnFileChanged(object source, FileSystemEventArgs e) method to determine which file has changed.

Does that answer your questions?

Regards,

CupawnTae

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rmyhreAuthor Commented:
Well... sometimes one just misses the obvious.  I don't even need another FileSystemWatcher object.  One object watches everything that needs to be watched.  Thanks for pointing out my tendancies to make things more difficult.  :)

cupawntaeCommented:
Aw well :)
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

rmyhreAuthor Commented:
Actually, if you still read this... I don't want to use the filewatcher to raise the event in the parent object, but rather a different class.  The real question is how to I create a child class (any class) and have multiple child objects raise the event located in the parent object?
cupawntaeCommented:
I guess I'm not really clear what you're trying to achieve here... What is your "child class" - what does it do? Does it watch the file? Or does a filewatcher raise an event on it? And then the child raises another event on the parent?

My first answer shows how multiple objects can raise a single event. E.g.

fileWatcher.Changed += new FileSystemEventHandler(parentObject.OnFileChanged);

You could do something simliar in your own child classes if that's what you're after. Or are you trying to do something else?
rmyhreAuthor Commented:
The child class processes the contents of the file.  It is created when the first FileWatcher Created or Changed event occurs.  I want my child objects to report back to the parent object the amount of data processed (as a starter) and to do this through an event.

I thought I had to use Delegate and Event statements.  True?
cupawntaeCommented:
I'm not very familiar with creating your own events like that in C# unfortunately. If you've any points left for use I'd suggest you open a new question on that one (maybe just point back to this thread) - you may get someone who's more familiar with it. Post a link to the new question here too and if I get a chance to look at it before someone else answers, I'll see what I can do. Right now I'm a little busy, sorry :(
rmyhreAuthor Commented:
No problem... thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.