0

The problem stands at line 23 and 25.

double r.length = Convert.ToDouble(Console.ReadLine());

double r.width = Convert.ToDouble(Console.ReadLine());

I don't know how to make the conversion from string to double so that the program would take double numbers from the user.

My programming teacher once made it but I forgot to note it.

class Rectangle
{
    double length;
    double width;
    public double GetArea()
    {
        return length * width;
    }
    public void Display()
    {
        Console.WriteLine("Length: "+length);
        Console.WriteLine("Width: "+ width);
        Console.WriteLine("Area: "+ GetArea());
    }
}

class ExecuteRectangle
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();
        Console.WriteLine("Length= ");
        double r.length = Convert.ToDouble(Console.ReadLine()); // <===
        Console.WriteLine("Width= ");
        double r.width  = Convert.ToDouble(Console.ReadLine())); // <===
        Console.Writeline("Area= ");
        r.Display();
        Console.ReadLine();
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
dennis
  • 3
  • 5
  • Possible duplicate of [Converting string to double in C#](https://stackoverflow.com/questions/11399439/converting-string-to-double-in-c-sharp) `Double.TryParse()` is more sppropriate for data coming from the user – Ňɏssa Pøngjǣrdenlarp Jan 26 '19 at 19:05
  • 1
    @NoneoftheAbove I'm thinking the problem is at "r.length or r.width" , my IDE has underlined them with red – dennis Jan 26 '19 at 19:08
  • The question asks about converting to Double - I posted a link to one of the 2.5 million posts here concerning `c#, double, convert`. It is entirely possible you have more than one problem. The error pane describes the underlines to an error message – Ňɏssa Pøngjǣrdenlarp Jan 26 '19 at 19:13
  • 1
    The error message is *probably* `'Rectangle' does not contain a definition for 'width'` Use your mouse to confirm. – Ňɏssa Pøngjǣrdenlarp Jan 26 '19 at 19:18
  • The problem was at declaring the variables. I didnt declare them as "public double length; " but "double length; ". I just started to learn C# from a year+ in C++ world. Thank you for your generosity! :) – dennis Jan 26 '19 at 19:31

3 Answers3

1

Besides what Amir Arbabian has stated in his answer, there is another problem. The protection level for Rectangle.width and Rectangle.length are not specified, so they default to private. To make it accessible, add the public keyword before the length and width fields in Rectangle.

public double length;
public double width;

One last thing is that you have an extra closing parentheses ()) on the line

double r.width  = Convert.ToDouble(Console.ReadLine()));

Simply remove one of the closing parentheses to remove that error. And remove the double keyword, as Amir Arbabian has stated.

Also, what do you need that last Console.ReadLine() for?

Gymhgy
  • 338
  • 4
  • 11
  • It worked man ! Thank you! Only problem was at declaring the variables public as you said. I only started to shift to C# from 1 year of C++. As for the last Console.Readline , i must've put it by mistake :p – dennis Jan 26 '19 at 19:23
1

There are a couple things to fix:

Rectangle

  • Both length and width should be public
  • Both length and width should be properties
    • I.e., they should have { get; set; } appended
  • The names for both length and width should be capitalized because they are properties
    • This is a C# naming convention
  • Override ToString() for creating a description/string for an object

ExecuteRectangle

  • You don't need the double keyword when referencing r.length and r.width in ExecuteRectangle
  • To convert a string to a double, use double.TryParse
class Rectangle
{
    public double Length { get; set; }
    public double Width { get; set; }

    public double GetArea()
    {
        return Length * Width;
    }

    public override string ToString()
    {
        var stringBuilder = new StringBuilder();

        stringBuilder.AppendLine("Length: " + Length);
        stringBuilder.AppendLine("Width: " + Width);
        stringBuilder.Append("Area: " + GetArea());

        return stringBuilder.ToString();
    }
}

class ExecuteRectangle
{
    static void Main(string[] args)
    {
        Rectangle r = new Rectangle();

        Console.WriteLine("Length= ");
        double.TryParse(Console.ReadLine(), out r.Length);

        Console.WriteLine("Width= ");
        double.TryParse(Console.ReadLine(), out r.Width);

        Console.Writeline("Area= " + r.GetArea());

        Console.WriteLine(r.ToString());

        Console.ReadLine();
    }
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
0

You have to write it as

r.width = Convert.ToDouble(Console.ReadLine());

without double keyword. You use type only when you declare something, but here you just assign value to field of object. Hope that helps.

Amir Arbabian
  • 3,419
  • 1
  • 6
  • 12