sayth
sayth

Reputation: 7048

Entry point in class file - More than 1 entry point

What I am trying to do is create a project as I learn C# and have a new class in that project for each Euler problem.

So I have a base Program.cs file which has its entry point.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Euler
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

However I am defining my entry point in my own class correctly and have an error clearly showing I have more than one entry point.

How should I set my entry correctly?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Euler
{
    //Problem 1
    //If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
    //Find the sum of all the multiples of 3 or 5 below 1000.

    class Problem1
    {
        public Problem1(int args)
        {
            int num = 0;
            int limit = 1000;
            int sum = 0;
            while (num < limit)
            {
                if ((num % 3 ==0 )|| (num % 5 == 0))
                {
                    sum = sum + num;
                    num++;
                }
                else
                {
                    num++;
                }
            }
            Console.WriteLine(sum);
        }
    }


}

I know this is a newbie thing but it doesn't seem obvious.

Error   1   Program 'c:\Users\Sayth\Documents\Visual Studio 2013\Projects\Euler\Euler\obj\Debug\Euler.exe' has more than one entry point defined: 'Euler.Program.Main(string[])'.  Compile with /main to specify the type that contains the entry point.    c:\users\sayth\documents\visual studio 2013\Projects\Euler\Euler\Program.cs 11  21  Euler
Error   2   Program 'c:\Users\Sayth\Documents\Visual Studio 2013\Projects\Euler\Euler\obj\Debug\Euler.exe' has more than one entry point defined: 'Euler.Problem1.Main(string[])'.  Compile with /main to specify the type that contains the entry point.   c:\users\sayth\documents\visual studio 2013\Projects\Euler\Euler\Problem1.cs    15  21  Euler

EDIT I have resolved the entry point errors, the code below doesn't work but for other reasons not related to this question.

class Problem1
    {
        public int Sum53(int args)
        {
            int num = 0;
            int limit = 1000;
            int sum = 0;
            while (num < limit)
            {
                if ((num % 3 == 0) || (num % 5 == 0))
                {
                    sum = sum + num;
                    num++;
                }
                else
                {
                    num++;
                }
            }
            return sum;
        }

        public string myValue(string args)
        {
            Console.WriteLine("This is the total :" + );
        }
    }
}

Upvotes: 0

Views: 2833

Answers (1)

mason
mason

Reputation: 32694

You only can have a single entry point in the program (unless you tell the compiler otherwise which one to use). But that entry point can call whatever you want. So if you wanted to have multiple different entry functions for experimenting with, you can simply uncomment the function you want to run.

static void Main(string[] args) //You can't have any other functions with this signature in your project
{
    Function1(args);
    //Function3(args);
    //Function2(args);
}

static void Function1(string[] args)
{
}

static void Function2(string[] args)
{
}

static void Function3(string[] args)
{
}

Upvotes: 2

Related Questions