Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted

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.

Posted

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.

Posted

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];

Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

Yes Insert does exist in VB.

        Dim index As Single = 0
       Dim value As String = "test"
       Dim myArray As ArrayList
       myArray.Insert(index, value)

Anybody looking for a graduate programmer (Midlands, England)?
Posted

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!

Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

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).

[sIGPIC]e[/sIGPIC]
Posted
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.
Anybody looking for a graduate programmer (Midlands, England)?
Posted
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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...