I want to have the user input length
and width
so that we can calculate the area of the rectangle. I have two classes which are Program
and Rectangle
. The problem is when I try to run this code there is an error:
Program has more than one entry point
How to fix this problem?
Here is my code:
using System;
using System.Collections.Generic;
namespace rec_oop_20211025_E3
{
class Program
{
static void Main(string[] args)
{
List<Rectangle> recs = new List<Rectangle>();
ConsoleColor errColor = ConsoleColor.DarkRed; // to make the error part red in color
ConsoleColor defColor = Console.BackgroundColor; // defColor is default color
start: // use start to run many times
Console.Write("Input sides (eg. 5,6): ");
string line = Console.ReadLine();
Rectangle rec = new Rectangle();
try
{
rec.SetSides(line, ",");
Console.WriteLine($"{rec.GetInfo()}");
recs.Add(rec);
}
catch(Exception ex)
{
Console.BackgroundColor = errColor;
Console.WriteLine($"Error > {ex.Message}");
Console.BackgroundColor = defColor;
}
Console.WriteLine("\nEsc to stop, any key for another rectangle...");
char key = Console.ReadKey(true).KeyChar; // with start we need this
if (key != (char)ConsoleKey.Escape)
goto start;
Console.WriteLine();
Console.BackgroundColor = ConsoleColor.DarkGreen;
double total = 0;
foreach(var r in recs)
{
total = r.GetArea();
Console.WriteLine(r.GetInfo());
}
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine($"\nTotal = {total:n3}");
Console.BackgroundColor = defColor;
}
}
public class Rectangle
{
//static methods
public static void CheckSides(double width, double length)
{
if (width < 0 || length < 0)
throw new Exception($"Rectangle sides( {width}, {length}) are invalid.");
}
public static void TryParse(string data, string delimiter, out double width, out double length)
{
try
{
string[] arr = data.Split(delimiter);
width = double.Parse(arr[0]);
length = double.Parse(arr[1]);
}
catch (Exception)
{
throw new Exception($"Given data \"{data}\" are invalid.");
}
}
//intance fields
private double wd;
private double lng;
//instance methods
public void SetSides(string data, string delimiter)
{
double width, length;
Rectangle.TryParse(data, delimiter, out width, out length);
Rectangle.CheckSides(width, length);
wd = width;
lng = length;
}
public string GetInfo() => $"width={wd}, length={lng} > area={GetArea():n3}";
//public double GetArea() { return wd * lng; }
public double GetArea() => wd * lng;
}
}
}
CodePudding user response:
This is what is this - error is very descriptive. Try search (Ctrl shift F) for Main(
(just this text main
and then 1 round bracket) - you should find another function hidden somewhere that define void Main
- this could not be. Only 1 static void Main()
could exist. Delete the other one and now you app could compile. Does not matter if you have other classes - the only thing is matter is that function named Main
could be only one
P.S. If my answer is not enough - and you still have some question - please make a bare minimum clone of your code and make it Github repo - so we could look at the code
CodePudding user response:
You can check the official documentation on this error:
To resolve this error, you can either delete all Main methods in your code, except one, or you can use the StartupObject compiler option to specify which Main method you want to use.
You can alternatively use the /main
switch on the command line to indicate the type containing the appropriate entry point.