2017-01-17 78 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文件的名称空间。当我匹配命名空间时,错误消息就消失了。