C#,简单代码,为何报这样的错误(60分)

  • 主题发起人 千中元
  • 开始时间

千中元

Unregistered / Unconfirmed
GUEST, unregistred user!
“SampleBase.Form1.Dispose()” : 无法重写继承成员“System.ComponentModel.Component.Dispose()”,因为它未标记为 virtual、abstract 或 override
namespace SampleBase
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components;
public Form1()
{
InitializeComponent();
}
//下面这行出错
代码:
[b]public override void Dispose()[/b]
{
base.Dispose();
if(components != null)
components.Dispose();
}
 
public override void Dispose()
明显错误:Dispose的作用域是protected。你怎么重载也不能改成public。
 
beta版的代码。
做如下修改:
protected override void Dispose(bool disposing)
{
if (disposing)
{
base.Dispose();
if(components != null)
components.Dispose();}
}
F5,然后关闭程序,异常产生
“未处理的“System.StackOverflowException”类型的异常出现在 system.dll 中
”中断于: if(components != null)
你发过来的rar文件不能打开,请有空查一下
 
?我现在在公司,今天不回去,明天会check的。
 
这是讨论delphi的地方,你搞没搞错地方哟
 
如果我没理解错的话(暂时还没玩过C#),Dispose函数应该是拿来释放对象的对吗?
如果是这样,子类应该先释放自己的内存,再调用父类的释放函数。
改成这样试试:)
public override void Dispose()
{
if(components != null)
components.Dispose();
base.Dispose();
}
 
很不幸,你的理解是错的。
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
文件我重新发给你了。
 
[C#]
protected virtual void Dispose( bool disposing);
其中,参数disposing 为 true 则释放托管资源和非托管资源;为 false 则仅释放非托管资源。
错误就在base.Dispose();把它改为base.Dispose( disposing );就行了。
(好像你很懒的看帮助)
 
接受答案了.
 
后退
顶部