probably a very simple question

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

I have two files:

public class Tester
{
public static void Main()
{
Program begin = new Program();
begin.StartCalculator();
}
}

AND

public class Program
{
public void StartCalculator()
{
System.Console.WriteLine("Hello world");
}
}

This works, but when I change the body of Main() to simply:

StartCalculator();

and make the StartCalculator() method static, I get:

The name 'StartCalculator' does not exist in the current context



What does that mean, and why must I call the method on an object for the
program to run?
 
Hi,

You just need to tell it what class contains the StartCalculator method.

....
public static void Main()
{
Program.StartCalculator();
}

With just StartCalculator(), the compiler was searching for the method in the
Tester class.

Best regards,

Rodger

Achieve Planner - Keep track of your projects/tasks and schedule them in the
calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>
 
Rodger said:
Hi,

You just need to tell it what class contains the StartCalculator method.

...
public static void Main()
{
Program.StartCalculator();
}

With just StartCalculator(), the compiler was searching for the method in the
Tester class.

Best regards,

Rodger

Achieve Planner - Keep track of your projects/tasks and schedule them in the
calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
<http://www.SequenceDiagramEditor.com>

Ack, so simple! Thanks!

Is there a preferred way to do this? (static or instance?)
 
Hi,

It depends on what you are trying to accomplish.

There is nothing wrong with calling a static method from Main and letting it be
the one initializing your app.

Best regards,

Rodger

Achieve Planner - Keep track of your projects/tasks and schedule them in the
calendar
<http://www.effexis.com/achieve/planner.htm>

Sequence Diagram Editor - Draw sequence diagrams faster
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top