Solved

C# Windows console application

Posted on 2013-05-13
6
397 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?

726 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