2016-11-29 7 views
0

複数のエラーが発生していますが、理由はわかりません。エラーは、GetAreaメソッドの後に導入されます。それは宣言だかのようにinitilizationコンストラクタはどのように導入しますか? C#CS0236

namespace Lesson02 
{ 
    class Rectangle 
    { 
     static void Main(string[] args) 
     { 

     } 

     private double length; 
     private double width; 
     public Rectangle(double l, double w) 

     { 
      length = l; 
      width = w; 

     } 

     public double GetArea() 

     { 
      return length * width; 
     } 

     public Rectangle rect = new Rectangle(10.0, 20.0); 
     double area = rect.GetArea(); 
     Console.WriteLine("Area of Rectagle: {0}", area); 
+1

'Console.WriteLineを( "Rectagleのエリア:{0}"、面積);'メソッド –

+0

あなたの完全なクラスを記述するかのコンパイル一部下さい*内*べきです。そして、どのようなエラーが出てくるのか話してください。 –

+0

私はすべてを完全に書きました。私は質問のタイトルにあるエラーを含めました。あなたの答えをありがとうが、私は彼らがConsole.WriteLineメソッドの "外"を見た回であったことはほとんど確信しています。 – CoadA

答えて

0

あなただけのクラスの範囲内で実行

Console.WriteLine("Area of Rectagle: {0}", area); 

を置くことはできません。 Mainメソッドに移動:コメントで述べたように

namespace Lesson02 
{ 
    class Rectangle 
    { 
     // Method, here we execute 
     static void Main(string[] args) 
     { 
      // Executions are within the method 
      Rectangle rect = new Rectangle(10.0, 20.0); 
      double area = rect.GetArea(); 
      Console.WriteLine("Area of Rectagle: {0}", area); 
     } 

     // Declarations 
     private double length; 
     private double width; 

     public Rectangle(double l, double w) 
     { 
      length = l; 
      width = w; 
     } 

     public double GetArea() 
     { 
      return length * width; 
     } 
    } 
} 
+0

ありがとうございました。 – CoadA

0

、あなたがプログラムコードと混同クラス本体を持っています。 1つのクラスですべてを持つことも悪い考えです。

あなたのRectangleクラスが別々でなければなりません:

public class Rectangle 
{ 
    private double length; 
    private double width; 

    public Rectangle(double l, double w) 
    { 
     length = l; 
     width = w; 

    } 

    public double GetArea() 
    { 
     return length * width; 
    } 
} 

そして、あなたのプログラムコードの別々:どちらか一方

public class Program 
{ 
    static void Main(string[] args) 
    { 
     Rectangle rect = new Rectangle(10.0, 20.0); 
     double area = rect.GetArea(); 
     Console.WriteLine("Area of Rectagle: {0}", area); 
    } 
} 
+0

ありがとうございます。ほんとうにありがとう。 – CoadA

0

パブリックとしてクラス四角形を作るそう 公共のRectangle RECT =新しいRectangle(10.0、20.0を変更); as Rectangle rect =新しい長方形(10.0、20.0);

public class Rectangle 
    { 

     private double length; 
     private double width; 
     public Rectangle(double l, double w) 
     { 
      length = l; 
      width = w; 
     } 

     public double GetArea() 
     { 
      return length * width; 
     } 

    } 
    static void Main(string[] args) 
    { 
     Rectangle rect = new Rectangle(10.0, 20.0); 
     double area = rect.GetArea(); 
     Console.WriteLine("Area of Rectagle: {0}", area); 
    } 
+0

ありがとうございました。 – CoadA

関連する問題