2017-01-17 77 views
-2

我想編譯和測試我的代碼作業我的作業,並且Visual Studio拋出錯誤說'對象'不包含方法'處置',我不明白爲什麼它不會讓我編譯我的代碼。分配如下:'對象'不包含'處置'的方法C#編譯器錯誤

矩形類:創建一個類矩形。該類具有屬性長度和寬度,每個屬性默認爲1.它具有隻讀屬性,可以計算矩形的周長和麪積。它具有長度和寬度的屬性。設置的訪問器應該驗證長度和寬度是每個大於0.0且小於20.0的浮點數。編寫一個應用程序來測試類Rectangle。

下面是Rectangle類的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace WindowsFormsApplication16 
{ 
    class Rectangle 
    { 
     private float length = 1; 
     private float width = 1; 

     public Rectangle(float length, float width) 
     { 
      Length = length; 
      Width = width; 
     } 
     public float Length 
     { 
      get 
      { 
       return this.length; 
      } 
      set 
      { 
       if (value <= 0.0f || value > 20.0f) 
       { 
        this.length = 1.0f; 
       } 
       else 
       { 
        this.length = value; 
       } 
      } 
     } 
     public float Width 
     { 
      get 
      { 
       return this.width; 
      } 
      set 
      { 
       if (value <= 0.0f || value > 20.0f) 
       { 
        this.width = 1.0f; 
       } 
       else 
       { 
        this.width = value; 
       } 
      } 
     } 
     public float Perimeter 
     { 
      get 
      { 
       return(Length + Width)*2; 
      } 
     } 
     public float Area 
     { 
      get 
      { 
       return(Length*Width); 
      } 
     } 

     public override string ToString() 
     { 
      return string.Format("Perimeter is {0:F2} and Area is {1:F2}", Perimeter, Area); 
     } 

    } 
} 

下面是應用測試代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 


namespace WindowsFormsApplication16 
{ 
    class TestProgram 
    { 
     static void main(string[] args) 
     { 
      Rectangle rect = new Rectangle(19.5f, 15.9f); 
      Console.WriteLine(rect); 
      Console.ReadLine(); 
     } 
    } 
} 

當我去編譯它,它拋出了兩個錯誤:

Error 1 'WindowsFormsApplication16.Form2.Dispose(bool)': no suitable method found to 
override c:\users\kyle\documents\visual studio 2012\Projects\WindowsFormsApplication16\ 
WindowsFormsApplication16\Form2.Designer.cs 14 33 WindowsFormsApplication16 

Error 2 'object' does not contain a definition for 'Dispose'  
c:\users\kyle\documents\visual studio 2012\Projects\WindowsFormsApplication16\ 
WindowsFormsApplication16\Form2.Designer.cs 20 18 
WindowsFormsApplication16 

這是它說的錯誤代碼位於:

namespace WindowsFormsApplication16 
{ 
    partial class Form2 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 

     #endregion 
    } 
} 

任何人都可能闡明這一點,並給我一個想法,爲什麼它不會編譯?

+0

最後一塊代碼。系統是否生成? –

+0

@ChetanRanpariya是的,正如評論自己所說的。 – Servy

+0

如果項目中不需要表單,那麼現在就可以刪除它,看看事情是否正常。如果只是形式或其他背後的東西,那麼你肯定會這麼做。 –

回答

0

Form2必須從一個類派生...我認爲它應該是「Form」。創建一個新項目/添加一個新窗體並查看它從中派生出的類。你可能意外刪除了它。

+0

這是一個部分類。基類在另一個文件中定義。 – Servy

-1

我最近在將表單導入項目時遇到此錯誤。

導入表單後出現錯誤,我更改了main.cs代碼文件的名稱空間,但沒有更改相應的designer.cs文件的名稱空間。當我匹配命名空間時,錯誤消息就消失了。