Reputation: 980
I really need your help and to figure this out.
My Project:
AccessProjectMap
-- MainClass.cs
-- ErrorLog.cs (public)
ThreadProjectMap
-- StartThread.cs
I'd like to make StartThread
my default project when the project starts. Now I need the ErrorLog.cs file in my ThreadProjectMap. I made a reference and I can actually say ErrorLog log = new ErrorLog();
which does also work. When I try to use the ErrorLog in MainClass.cs
it's working too.
However I cannot use log
inside the main or DoThreading function.
class StartThread {
static string threadRefresh = ConfigurationManager.AppSettings.Get("refreshTime").ToString();
Access ac = new Access();
ErrorLog log = new ErrorLog();
static void Main(String[] args) {
log.LogMessageToFile("== Start Main() ==");
Thread t = new Thread(new ThreadStart(DoThreading));
t.Start();
Console.Read();
}
static void DoThreading() {
int refresh = 1;
while (true) {
Console.WriteLine("Test");
log.LogMessageToFile("== Test - inside Thread ==");
Thread.Sleep(1000);
}
}
}
Upvotes: 2
Views: 613
Reputation: 548
The instance you create of ErrorLog on this line:
ErrorLog log = new ErrorLog();
Is an instance variable. The Main
and DoThreading
methods are static.
You have two options: Either make the ErrorLog static as well, like so:
static ErrorLog log = new ErrorLog();
Or, just instantiate it inside your static methods.
Upvotes: 2
Reputation: 223402
The issue is not because of different projects/namespaces, instead you are trying to access an instance member in a static method.
Make your log
field static
and it should compile fine.
static ErrorLog log = new ErrorLog(); //Here, make it static
static void Main(String[] args) {
log.LogMessageToFile("== Start Main() ==");
Thread t = new Thread(new ThreadStart(DoThreading));
t.Start();
Console.Read();
}
Upvotes: 3