dreber Posted July 15, 2003 Posted July 15, 2003 I am wrtining my first c# app that will monitor a perticular file location to do things. When I build the project I get : An object reference is required for the nonstatic field, method, or property 'JobMonitor.StartUp.watcher' at: watcher.Star ch(); Here is the code: using System; using System.Windows.Forms; namespace JobMonitor { /// <summary> /// Summary description for Main. /// </summary> public class StartUp { FileWatch watcher = new FileWatch(); static void Main() { watcher.Star ch(); // <-- error at this point Application.Run(new frmFortestingonly()); } } } using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace JobMonitor { /// <summary> /// Summary description for FileWatch. /// </summary> public class FileWatch { FileSystemWatcher watcher = new FileSystemWatcher(); public FileWatch() { } public void Star ch() { watcher.Path = "D:\\"; watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size; watcher.Changed += new FileSystemEventHandler(OnFileEvent); watcher.Created += new FileSystemEventHandler(OnFileEvent); watcher.EnableRaisingEvents = true; } //----------------------------------------------------------------------- public void OnFileEvent(object source, FileSystemEventArgs fsea) { DateTime dt = new DateTime(); dt = System.DateTime.UtcNow; MessageBox.Show("",dt.ToLocalTime() + " " + fsea.ChangeType.ToString() + " " + fsea.FullPath) ; } //---------------------------------------------------------------------- public void OnRenameEvent(Object source, RenamedEventArgs rea) { DateTime dt = new DateTime(); dt = System.DateTime.UtcNow; MessageBox.Show("",dt.ToLocalTime() + " " + rea.ChangeType.ToString() + rea.OldFullPath+ " to " +" " + rea.FullPath); } //------------------------------------------------------------------ } } Any help would be great. Thanks Quote
dreber Posted July 15, 2003 Author Posted July 15, 2003 Hmmm, "Star ch();" is being displayed as "Star ch();". I am not sure why. Quote
dreber Posted July 15, 2003 Author Posted July 15, 2003 I am not sure what is happing. I have a function named (each letter is spelled out) "S t a r t W a t c h()" but the newsgroup is displaying Star ch(). Quote
Administrators PlausiblyDamp Posted July 15, 2003 Administrators Posted July 15, 2003 Look at the missing letters - could be a profanity filter on the board Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders dynamic_sysop Posted July 15, 2003 Leaders Posted July 15, 2003 it certainly looks like profanity cuz it's got a t as well as the wat bit :cool: used to get that on irc with s c u n t h o r p e ( and thats only a town in the uk :-\ ) lol Quote
Administrators PlausiblyDamp Posted July 16, 2003 Administrators Posted July 16, 2003 Only noticed it 'cos it's one of my favorite swear words - handy in all situations Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
_SBradley_ Posted July 16, 2003 Posted July 16, 2003 'watcher' is an instance member of your StartUp class. However, you are trying to reference it from Main(), which is a static method. Since a static method does not have an implicit 'this' reference, you cannot reference instance members, only static members. You have three choices: 1. Declare and initialize 'watcher' inside your Main() method, instead of making it a member. 2. Make 'watcher' a static member of your StartUp class (just declare it "static FileWatch watcher"). 3. Create an instance of class StartUp and use that, as in "new StartUp().watcher.StartWàtch()". Quote
dreber Posted July 16, 2003 Author Posted July 16, 2003 Thanks for you help _SBradley_. I guess that I need to read some more. 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.