Solved

C# Windows console application

Posted on 2013-05-13
6
386 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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

920 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

13 Experts available now in Live!

Get 1:1 Help Now