Solved

C# Windows console application

Posted on 2013-05-13
6
384 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 74

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now