Link to home
Start Free TrialLog in
Avatar of kbuss
kbuss

asked on

C# Windows console application

I have a windows service that I have built and doesn't work properly. So I need to test it all again. I've bundled it all in to a new c# console application. I don't often use console applications and am having difficult getting started.

I don't seem to be able to call a standard method from the static main method that runs at the start of a console application.#

Like this:

        static void Main(string[] args)
        {
          Start();
        }

        private void Start
        {
          DoClass = myDoClass = new DoClass();
myDoClass.HelloWorld();
        }

Open in new window


It's been a while since I have used console applications. Can anyone tell me what I am doing wrong and what I need to do to execute a method within program.cs from the console main method.
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

change the Start function to be static as well:
private static void Start

Open in new window

the reason is that u can't call non-static function from a static function, and since Main is static
(the entry point of the project) you can't call Start.
Avatar of kbuss
kbuss

ASKER

Ok.

Now does this mean that all of my methods and classes in my console app now have to contain the static kewyword. For example I have a time in the Start method and it wont recognise this unless it is static. Then I have a Timer_Elapsed method which instantiates a new class. So does this new class and all classes instantiated within it all have to be static??
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just create an instance of Program (assuming "Program" is the name of your new class) in your Main method. Then you can call all of your methods against that instance rather than changing everything to static.

e.g.

static void Main(string[] args)
{
    Program p = new Program();

    p.Start();
}

private void Start()
{
    DoClass = myDoClass = new DoClass();
myDoClass.HelloWorld();
}

Open in new window

Avatar of kbuss

ASKER

cheers!