static functions

This is a discussion on static functions within the C# Programming forums, part of the General Programming Boards category; Hello there,
i am forced to use a static function, unfortunately this means i cant call the variables which belong ...

static functions

Hello there,
i am forced to use a static function, unfortunately this means i cant call the variables which belong to this class from this function, what si the best way i can overcome this? The variable is gcroot<FileSystemWatcher^ > watcher;

Hello there,
i am forced to use a static function, unfortunately this means i cant call the variables which belong to this class from this function, what si the best way i can overcome this? The variable is gcroot<FileSystemWatcher^ > watcher;

br

Depends. Post a compilable example demonstrating the problem. Also, are you using straight C++ or some variant (as indicated by your usage of the '^' symbol)?

The best way is to make that variable static too. Really, trying to get a non-static member variable form a static function makes no sense at all. What value would you expect: of what instance of the object? Or is there only one instance? In that case, you might want to consider a singleton class, though I doubt if that's the best solution.
All in all, I think this question can't be answered as you went wrong somewhere earlier.

hi guys, here is what i have done so far:
i am using the filesystemwatcher function, the callback functions (or event handlers are static as so:
static void OnChanged(Object* /*source*/, FileSystemEventArgs* e)
{
// Specify what is done when a file is changed, created, or deleted.
Console::WriteLine(S"File: {0} {1}", e->FullPath, __box(e->ChangeType));
}

Now because i have given the absolute name for the filter, upon renaming the handlers wont be invoked
because it is still searching for the orignal file name, so what i need to do is update the watcher->filter inside the rename handler in order to keep the file being monitored :S

There is a way to get around this but it is not the best idea. Normally the fact that you have to get around this is either a product of bad design or of being forced to use an API which forces C style callbacks as opposed to the observer/listener paradigm via interfaces.

In C++ there is no other way than having static versions of those functions. The first parameter should hold a reference to your filesystemwatcher object so you can have a static lookup table to connect the filesystem watcher object to another class or subclass the watcher class so you can call one of it's new methods.

Please note that those difficulties are due to your use of C++/CLI. If you need .NET use a native .NET Language like C# and you won't have those problems.