'Object'不包含用于'dispose' C# 编译器错误的方法



我正试图为我的家庭作业编译和测试我的代码,Visual Studio抛出错误,说"object"不包含"dispose"的方法,我不明白为什么它不允许我编译代码。任务显示:

矩形类:创建一个类矩形。该类具有属性length和width,每个属性默认为1。它具有计算矩形周长和面积的只读属性。它具有长度和宽度两个属性。设置的访问者应验证长度和宽度是否均为大于0.0且小于20.0的浮点数字。编写一个应用程序来测试类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:userskyledocumentsvisual studio 2012ProjectsWindowsFormsApplication16
WindowsFormsApplication16Form2.Designer.cs 14  33  WindowsFormsApplication16
Error   2   'object' does not contain a definition for 'Dispose'    
c:userskyledocumentsvisual studio 2012ProjectsWindowsFormsApplication16
WindowsFormsApplication16Form2.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
}
}

有人能阐明这一点并告诉我为什么它不会编译吗?

Form2必须从类派生。。。我认为应该是"形式"。创建一个新项目/添加一个新Form并查看它派生自的类。你可能是偶然删除了它。

我最近在将表单导入项目时遇到了这个错误。

错误发生在导入表单之后,我更改了main.cs代码文件上的名称空间,但没有更改相应designer.cs文件的名称空间。当我匹配命名空间时,错误消息就消失了。

相关内容

最新更新