FileSystemWatcher and multiple tabs

closet geek

Member
Joined
Apr 2, 2006
Messages
21
Hi,

I have code that creates a new tab and adds it to a Form. Every time a new tab is created I would like a FileSystemWatcher event to be created to monitor the file that is loaded into a RTB on that tab.

The issue I have is the Watchers arent unique. Everytime a new tab is opened the old Watcher gets overwritten by the new one. How do I define a FileSystemWatcher which has a name which can be increased or changed everytime my "create a new tab" code is run?

Something like a simple counter that gets increased everytime the code is run, the FileSystemWatchers name could then be set to the counters current total. How would I do that? Or does anyone have a neater solution.

J# would be best, but I can probably understand C# too.

Thanks.
 
Well, I assume each listener is going to be listening to something unique, right? You can use the filter to determine uniqueness. Meanwhile, because youre using an array, youll instantiate a new listener for each file and hold it in the array. So, every time you create a new tab, you increment your counter by one, and add a new listener to your array...

closet geek said:
Something like a simple counter that gets increased every time the code is run, the FileSystemWatchers name could then be set to the counters current total.
Thats not going to work, right? So using an array is the next best thing.

Code:
// You want:
listener1 = new listener()
listener2 = new listener()
listener3 = new listener()
//But you dont know how many you need and you cant change names on the fly like that.

// so instead you get:
listeners = new listener[3]  //and really youll probably want to use a dynamic list like an ArrayList or List<T>
listener[1] = new listener
listener[2] = new listener
listener[3] = new listener

See where Im going with this? Im leaving some blanks for you to fill in, but I think this answers the root problem of how to get a unique listener for each tab. Then, when an event is thrown, you can tell the difference between the various listeners by checking the filter property.
 
Thats really all over my head. Ive never used an ArrayList before I cant seem to make head nor tail of the helpfile from Visual Studio either. How would I create an ArrayList to hold say Strings for instance? How would I implement something like this:

array[intcounter] = string;

using ArrayLists instead of a normal Array.
 
An ArrayList can store any objects, but is not strongly typed so it stores everything as an Object. If you wish to use one to store only items of a specific kind you can either create a strongly typed array or you can cast objects to access them.
C#:
ArrayList myArray = new ArrayList();
int item = 10;
myArray.Add(item);
// to access the item
item = (int)myArray[0];
 
If you wish to insert it at a specfic point you will need to use the myArray.Insert() method. However Im not entirely sure what this would do if you called it and passed a number larger than the count. Assumably it would simply add it to the end at the latest avialable index.
 
Ive triple checked and cant see it in J# but I have found out that youre supposed to use something like this:

Code:
// Change the value of the second element in the ArrayList.
myArray.set(1, new Integer(4));

instead. Ill give it a whirl.

Thanks!
 
set_Item() is actually an accessor for the Item property. In VB and C# you cant directly access these accessor functions, but behind the scenes each property get/property set is compiled into a pair of accessor functions (if the property is "Item", the compiled function pair would be "get_Item" and "set_Item"). The ArrayList.Item property is actually an indexer (the default property in VB), which means that it cant even be accessed explicitly in C#, but code like this:
C#:
ArrayList X = new ArrayList();
X[0] = "Text";
Object Y = X[0];
would compile into this:
C#:
ArrayList X = new ArrayList();
X.set_Item(0, "Text");
Object Y = X.get_Item(0);

Im just guessing, but J# probably allows (if not requires) the get_/set_ syntax to be consistent with Java, which does not support properties (most Java programmers explicitly write get/set function pairs to attain the same functionality).

So... myArray.set_Item(1, new Integer(4)) translates into myArray[1] = 4, which is not the same as myArray.Insert(1, 4) (although it may behave the same, I didnt check).
 
I see your point, it does seem likely that the set method would override whatever value is in that index and therefore keep the same count. Wheras, unless Im mistaken, the Insert method will shift each value down incrementing the count by one.
 
marble_eater said:
Im just guessing, but J# probably allows (if not requires) the get_/set_ syntax to be consistent with Java, which does not support properties (most Java programmers explicitly write get/set function pairs to attain the same functionality).
That is correct. Properties are not in the Java/Sun API. This article explains J# properties quite well and how they relate to C#. It was quite informative.

According to the documentation, Insert() is going to slide the object into the ArrayList at the specified index (O(N) operation) while set_Item() will overwrite the object at index with the new object (O(1) operation). Insert() and set_Item() are not the same. From what you described it sounded like you wanted Insert(), and set_Item() is not going to do what you want it to.
 
Back
Top