简体   繁体   English

“对象”不包含“处置”C# 编译器错误的方法

[英]'Object' does not contain a method for 'dispose' C# compiler error

I'm trying to compile and test my code for my homework assignment, and Visual Studio is throwing up errors saying that the 'object' does not contain a method for 'dispose', and I can't figure out why it won't let me compile my code.我正在尝试为我的家庭作业编译和测试我的代码,Visual Studio 抛出错误,说“对象”不包含“处理”的方法,我不知道为什么它不会让我编译我的代码。 The assignment reads:作业内容如下:

Rectangle Class: Create a class Rectangle.矩形类:创建一个矩形类。 The class has attributes length and width, each of which default to 1. It has read-only properties that calculate the Perimeter and the Area of the rectangle.该类具有长度和宽度属性,每个属性默认为 1。它具有计算矩形的周长和面积的只读属性。 It has properties for both length and width.它具有长度和宽度的属性。 The set accessors should verify that length and width are each floating point numbers greater than 0.0 and less than 20.0. set 访问器应该验证长度和宽度都是大于 0.0 且小于 20.0 的浮点数。 Write an app to test class Rectangle.编写一个应用程序来测试类 Rectangle。

Here is the code for the rectangle class:这是矩形类的代码:

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);
        }

    }
}

Here is the code for the app to test:这是要测试的应用程序的代码:

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();
        }
    }
}

When I go to compile it, it throws up two errors:当我去编译它时,它抛出了两个错误:

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

Here is the code it says the error is located at:这是它说错误位于的代码:

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
    }
}

Can anyone possibly shed some light on this and give me an idea as to why it won't compile?任何人都可以对此有所了解,并让我知道为什么它不能编译吗?

That Form2 has to derive from a class...I think it should be "Form". Form2 必须从一个类派生......我认为它应该是“Form”。 Create a new project / add a new Form and see the class from which it derives.创建一个新项目/添加一个新 Form 并查看它派生的类。 You probably deleted it by accident.你可能不小心删除了它。

I encountered this error recently when I was importing a form into a project.我最近在将表单导入项目时遇到了这个错误。

The error came after importing the form, I changed the namespace on the main.cs code file, but I did not change the namespace for the corresponding designer.cs file.导入表单后报错,我更改了main.cs代码文件上的namespace,但是没有更改对应的designer.cs文件的namespace。 When I matched the namespaces, the error messages went away.当我匹配命名空间时,错误消息消失了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM