Solved

C# Windows console application

Posted on 2013-05-13
6
393 Views
Last Modified: 2013-05-13
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.
0
Comment
Question by:kbuss
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39161445
change the Start function to be static as well:
private static void Start

Open in new window

0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39161452
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.
0
 

Author Comment

by:kbuss
ID: 39161600
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??
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39161637
in the Main function create instance of the class:
static void Main(string[] args)
        {
          DoClass myDoClass = new DoClass();
myDoClass.Start();
        }

        private void Start
        {
//set the timer here
        }

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39161646
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

0
 

Author Closing Comment

by:kbuss
ID: 39161684
cheers!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question