closet geek Posted April 19, 2006 Posted April 19, 2006 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 aren't 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 FileSystemWatcher's 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. Quote
closet geek Posted April 19, 2006 Author Posted April 19, 2006 Perhaps? I'm not sure on that. How would that ensure uniqueness? Quote
mskeel Posted April 19, 2006 Posted April 19, 2006 Well, I assume each listener is going to be listening to something unique, right? You can use the filter to determine uniqueness. Meanwhile, because you're using an array, you'll 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... Something like a simple counter that gets increased every time the code is run' date=' the FileSystemWatcher's name could then be set to the counters current total.[/quote']That's not going to work, right? So using an array is the next best thing. // You want: listener1 = new listener() listener2 = new listener() listener3 = new listener() //But you don't know how many you need and you can't change names on the fly like that. // so instead you get: listeners = new listener[3] //and really you'll 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 I'm going with this? I'm 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. Quote
closet geek Posted April 19, 2006 Author Posted April 19, 2006 That's really all over my head. I've never used an ArrayList before I can't 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. Quote
Cags Posted April 19, 2006 Posted April 19, 2006 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. ArrayList myArray = new ArrayList(); int item = 10; myArray.Add(item); // to access the item item = (int)myArray[0]; Quote Anybody looking for a graduate programmer (Midlands, England)?
closet geek Posted April 19, 2006 Author Posted April 19, 2006 Thanks Cags. I wonder how do I add an item to a specific place in the ArrayList? Quote
Cags Posted April 19, 2006 Posted April 19, 2006 If you wish to insert it at a specfic point you will need to use the myArray.Insert() method. However I'm 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. Quote Anybody looking for a graduate programmer (Midlands, England)?
closet geek Posted April 19, 2006 Author Posted April 19, 2006 myArray.Insert() doesn't exist as far as I can see (maybe a C# only thing?) This is proving quite tricky for me! Quote
Cags Posted April 19, 2006 Posted April 19, 2006 Yes Insert does exist in VB. Dim index As Single = 0 Dim value As String = "test" Dim myArray As ArrayList myArray.Insert(index, value) Quote Anybody looking for a graduate programmer (Midlands, England)?
closet geek Posted April 19, 2006 Author Posted April 19, 2006 I've triple checked and can't see it in J# but I have found out that you're supposed to use something like this: // Change the value of the second element in the ArrayList. myArray.set(1, new Integer(4)); instead. I'll give it a whirl. Thanks! Quote
mskeel Posted April 19, 2006 Posted April 19, 2006 ArrayList implements the IList interface so it must have the Insert method for all .Net languages including J#. Quote
Cags Posted April 19, 2006 Posted April 19, 2006 I just checked J#, the syntax is exactly the same as C#. It does however also support set_Item(), which appears to be the same method. Quote Anybody looking for a graduate programmer (Midlands, England)?
Leaders snarfblam Posted April 19, 2006 Leaders Posted April 19, 2006 set_Item() is actually an accessor for the Item property. In VB and C# you can't 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 can't even be accessed explicitly in C#, but code like this: ArrayList X = new ArrayList(); X[0] = "Text"; Object Y = X[0]; would compile into this: ArrayList X = new ArrayList(); X.set_Item(0, "Text"); Object Y = X.get_Item(0); I'm 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 didn't check). Quote [sIGPIC]e[/sIGPIC]
Cags Posted April 20, 2006 Posted April 20, 2006 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 I'm mistaken, the Insert method will shift each value down incrementing the count by one. Quote Anybody looking for a graduate programmer (Midlands, England)?
mskeel Posted April 20, 2006 Posted April 20, 2006 I'm just guessing' date=' 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).[/quote']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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.