Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am working here with FileSystemWatcher, and i want to keep an arraylist of all the files created during the session of the application.
So after and event on the filesystem i want to add the file to the array.
Now this doesn't work. But how to implement it the right way.

snippit
class Import
{
   private ArrayList a;
   private FileSystemWatcher f;
   public Import()
   {
      // create and configure Arraylist and FileWatcher here
      
      f.Changed += new FileSystemEventHandler(OnChanged);
   }
   private static void OnChanged(object source, FileSystemEventArgs args)
   {
      // Here i cannot access the Arraylist a to add the args. to it.
   }
}

Sorry for the newbie level
Posted

Adding to John's answer:

Two problems in that code (although this may not be your actual code)

1. You are not instantiating your FileSystemWatcher anywhere.

2. Make OnChanged a non-static method and it will be able to access the a member.

Suggestions:

1. Make a a List<> instead of an ArrayList.

2. Rename a to something more meaningful.
 
Share this answer
 
Comments
#realJSOP 18-Feb-11 10:58am    
As to item 1, he may be showing us just part of the code.
Nish Nishant 18-Feb-11 11:00am    
Good point. But his class does not seem like it was designer generated (you never know though). Thanks.
Vincent Beek 18-Feb-11 10:59am    
No, its not the actual code, was just to explain.
But point 2 was my problem. So solved.

Many many thanks.
I have much to learn
Nish Nishant 18-Feb-11 11:00am    
Ok, good to hear that.
Sergey Alexandrovich Kryukov 18-Feb-11 11:48am    
Correct, my 5.
--SA
You might want to check out my two-part article series. It won't precisely answer your question, but it should provide enlightenment and a better way to use the FileSystemWatcher object:

FileSystemWatcher - Pure Chaos (Part 1 of 2)[^]

FileSystemWatcher - Pure Chaos (Part 2 of 2)[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 11:47am    
Yes, another reference to your article.
It's interesting, my 5.
--SA
Since your method is static you have to make your members static as well if you want to use them from that method.

static private ArrayList a;
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Feb-11 11:49am    
Olivier, you should better explain why not using static here.
--SA
Olivier Levrey 19-Feb-11 7:47am    
You're right. Sometimes answering is not enough: teaching good habits should be the rule.
Sergey Alexandrovich Kryukov 19-Feb-11 16:05pm    
I up-voted this answer because I don't agree with someone's vote of 3.
--SA
Olivier Levrey 20-Feb-11 5:53am    
Thank you SA. Actually I thought it was you since I saw your first comment at the same time.
Sergey Alexandrovich Kryukov 20-Feb-11 14:04pm    
I usually argument my down-votes (frankly, sometime make exclusion for 100% apparent crap).
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900