I want to have someone input a value for length and width in my code, here is what I got so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Rectangle
{
double length;
double width;
double a;
static double Main(string[] args)
{
length = Console.Read();
width = Console.Read();
}
public void Acceptdetails()
{
}
public double GetArea()
{
return length * width;
}
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}
class ExecuteRectangle
{
public void Main()
{
Rectangle r = new Rectangle();
r.Display();
Console.ReadLine();
}
}
}
Is trying to use two Main
methods the wrong way to approach this? This is code I was copying from http://www.tutorialspoint.com/csharp/csharp_basic_syntax.htm I'm trying to modify it around to just to get more experience with this programming language.
In this cases you'll have to tell the compiles wich is the class with the entry point.
"If your compilation includes more than one type with a Main method, you can specify which type contains the Main method that you want to use as the entry point into the program."
http://msdn.microsoft.com/en-us/library/x3eht538.aspx
And yes, having two main methods is confusing and senseless.
There are some problem with you code, let's analyse them:
a program must have a unique entry point and it must be a declared as static void, here you have two main but they are wrong
you in your static Main the one in the rectangle class you can't reference the variables length e width because they're not declared as static
I think that what you want is:
This is a working code that do what you want. Note before copy pasting, since you're trying and lear read the steps and try to fix it without looking at the code