0

I am trying to get a rectangle application to work , the purpose is to let the user input the length and width but I don't know how to call the methods and get the user input to be displayed in my output.

here is my class (langd = lenght , bredd = width in swedish)

namespace _10._3 { class Rectangle { private double langd; private double bredd; public Rectangle() { langd = 1.0; bredd = 1.0; } public Rectangle(double langden, double bredden) { langd = langden; bredd = bredden; } public double langden { get { return langd; } set { if (value > 0 && value < 20) langden = value; else throw new ArgumentOutOfRangeException("längd", value, "langde måste vara mer än 0 och mindre än 20 "); } } public double bredden { get { return bredd; } set { if (value > 0 && value < 20) bredd = value; else throw new ArgumentOutOfRangeException("bredd", value, "bredden måste vara mer än 0 och mindre än 20"); } } public double omkrats { get { return 2 * langd + 2 * bredd; } } public double area{ get { return langd * bredd; } } public override string ToString() { return string.Format("{0}: {1}{2}: {3}{4}: {5}{6}: {7}", "Längd", langd, "bredd", bredd, "Omkräts", omkrats, "Area", area); } } 

}

Here is my program:

namespace _10._3 { class test { public static void Main(string[] args) { Rectangle rectangle = new Rectangle(); Console.WriteLine("Skriv in längden :"); rectangle.langden = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("Skriv in bredden :"); rectangle.bredden = Convert.ToDouble(Console.ReadLine()); Console.WriteLine(rectangle.ToString()); Console.WriteLine("Längd :" ); Console.WriteLine("Bredd :"); Console.WriteLine("Omkräts:"); Console.WriteLine("Area"); } } } 
3
  • 2
    what language is this ?? :) Commented Jun 5, 2013 at 19:14
  • 1
    @LittleChild I'd bet my soul that's C#. Commented Jun 5, 2013 at 19:17
  • @Renan no, please don't. Commented Jun 5, 2013 at 19:19

1 Answer 1

1

are you trying to use the property getters/setters?(you seem to be using the setters just fine)

Just treat them as variables

Console.WriteLine("Längd : " + rectangle.langden.ToString() ); 

or

Console.WriteLine("Längd : {0}", rectangle.langden ); 
Sign up to request clarification or add additional context in comments.

3 Comments

on a related note, the common naming standard would probably name your length property Langd
ok thanks ! just what i was looking for :) but i get An unhandled exception of type 'System.StackOverflowException' occurred in 10.3.exe
@user2456264 the reason for that is because when you do langden = value; you're essentially calling the setter from within itself. change that line to langd = value;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.