1

I am building a file watcher that loads data from a file that is dropped to a specified location in SQL. All my code is fine, except the passing of variables typed in TextBoxes in the UI to another class outside of MainWindow.

First I initialize a FileWatcher:

public MainWindow()
{
    InitializeComponent();
    FileWatch.FileWatcherLoad();
}

Which calls the file watcher to initialize:

public static void FileWatcherLoad()
{
    FileSystemWatcher watcher = new FileSystemWatcher();
    watcher.Path = //some path;
    watcher.NotifyFilter = NotifyFilters.LastWrite;
    watcher.Filter = "*.*";
    watcher.Changed += new FileSystemEventHandler(OnChanged);
    watcher.Created += new FileSystemEventHandler(OnChanged);
    watcher.EnableRaisingEvents = true;
}

This issue is arising because the MainWindow is forcing me to declare FileWatcherLoad as a static type. When I remove the static type from File WatcherLoad() I get error:

an object reference is required for the non-static field, method or property 'FileWatch.FileWatcherLoad()'

Note that FileWatcherLoad() is in another class, FileWatch, which is a child class of MainWindow.

This is an issue for me because I wish to use user-entered data in a textbox in my UI, but this is NON STATIC, and is therefore causing a problem when I declare everything else static.

Salih Karagoz
  • 2,189
  • 2
  • 22
  • 35
DanG
  • 83
  • 2
  • 10
  • 2
    You need to create an instance of FileWatch and hold it in a variable in your window. You don't seem to understand the concept of static vs. instance, and how garbage collection and variable scope works. I'd strongly suggest you grab a copy of CLR Via C# and read it. It won't take too long, and you'll be 1000% better off. –  Jul 12 '17 at 13:15
  • Coming from Visual Basic, aren't you? There's no default instance in C# - if you want an instance of something, you need to create it and store the reference somewhere. – Luaan Jul 12 '17 at 13:15
  • Your example is confusing to read, as you posted the static version of FileWatch. – Trey Jul 12 '17 at 13:16
  • Declare a field in your mainform like: `private FileWatch fileWatch = new FileWatch();`. Then use `fileWatch.FileWatcherLoad();` in the constructor. – Tim Schmelter Jul 12 '17 at 13:23

3 Answers3

2

When you call FileWatch.FileWatcherLoad() you tell the compiler that you want to call a static method. If you want to call a non static method you need to call the method on an object.

If you want to call the method on the current object you can either call this.FileWatcherLoad() or just FileWatcherLoad().

If the method isn't in the same class as the one you are calling the method from you need to initialise an object first.

This can be done by using the new keyword.

var fileWatch = new FileWatch();
fileWatch.FileWatcherLoad();
NtFreX
  • 10,379
  • 2
  • 43
  • 63
  • This does work (removes the reference issue) but it causes another error. The InitializeComponent() now breaks with System.StackOverFlowException – DanG Jul 12 '17 at 13:49
  • @DanG A `StackOverflowException` mostly means that you have infinite recursion (example => an method calling itself forever). Maybe [this](https://stackoverflow.com/questions/206820/how-do-i-prevent-and-or-handle-a-stackoverflowexception) link helps you resolve the issue. If not I suggest you ask a new question. – NtFreX Jul 12 '17 at 13:54
  • It's weird because if I leave it as static, then it calls it fine and the filewatcher works perfectly. Something about declaring the variable breaks it. I am pretty new to all this so I am not surprised I've broken it – DanG Jul 12 '17 at 13:59
  • @DanG It can't be about the signature of the method. I would step through you code. I should be easy to spot the place which keeps looping. If you the only thing you have added else is `new FileWatch` it must be something with this class. – NtFreX Jul 12 '17 at 14:02
  • It seemed to do with the fact that FileWatch was a child class of MainWindow - when I removed this it now works. Thanks – DanG Jul 12 '17 at 14:09
  • @DanG Hm interessting. And no problem – NtFreX Jul 12 '17 at 14:10
0

You would have to do something like:

FileWatch myWatch=new FileWatch(); 

Then call your methods etc...

Steve
  • 2,950
  • 3
  • 21
  • 32
Trey
  • 413
  • 4
  • 12
0

That's cause the way you are calling it which the invocation way of static member function. If you want that method to be instance method then create an instance and call it like

FileWatch watcher = new FileWatch();
watcher.FileWatcherLoad(); 

(or) just new FileWatch().FileWatcherLoad()

Now you can declare your method to be non-static one

public void FileWatcherLoad()
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = //some path;
        watcher.NotifyFilter = NotifyFilters.LastWrite;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }
Rahul
  • 76,197
  • 13
  • 71
  • 125