Object Array Error

  • Thread starter Thread starter Ryan Rogers
  • Start date Start date
R

Ryan Rogers

I'm trying to create a FileSystemWatcher Array, however I am new at C#... I
know its not instanciating form somereason or not getting the reference
because I'm getting the
System.NullReferenceException and Here is bascially what I have for my code:

FileSystemWatcher[] watcher = new FileSystemWatcher[2];
watcher[0].Path = "\\\\pumptest-a\\pats\\results";

This throws the exception
 
Ryan,

When you create an array of reference types, it only creates space for
the references. By default, references are set to null. To fix this, you
have to create an instance and assign it to the array, like this:

watcher[0] = new FileSystemWatcher();

This will create an instance of the file system watcher and place the
reference into the array.

Hope this helps.
 
Nicholas,


Thank you, got it working now!.. You don't by chance know anything about the
FileSystemWatcher Class do you??

I am wondering if it can accept multiple filters in it's filter property.
Eg.. *.f**;*.p** my work around right now is just using an if statment in
the event that is triggered and dropping any files there.. I would rather
have the FilesystemWatcher do it if it is capable.

Cheers,

Nicholas Paldino said:
Ryan,

When you create an array of reference types, it only creates space for
the references. By default, references are set to null. To fix this, you
have to create an instance and assign it to the array, like this:

watcher[0] = new FileSystemWatcher();

This will create an instance of the file system watcher and place the
reference into the array.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan Rogers said:
I'm trying to create a FileSystemWatcher Array, however I am new at
C#...
I
know its not instanciating form somereason or not getting the reference
because I'm getting the
System.NullReferenceException and Here is bascially what I have for my code:

FileSystemWatcher[] watcher = new FileSystemWatcher[2];
watcher[0].Path = "\\\\pumptest-a\\pats\\results";

This throws the exception
 
Back
Top