在 C#/.NET 中使用 SaveFileDialog 生成“无法创建抽象类的实例”错误
因此,我一直在努力在 .NET 应用程序中创建“保存”按钮。根据我的研究,我似乎做的每件事都是正确的。我一直将这篇文章作为主要来源: http ://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
以下是我的C#方法代码:
private void save2(object A_0, EventArgs A_1)
{
Stream stream = new Stream();
savefile2 = new SaveFileDialog();
savefile2.InitialDirectory = @"C:\Program Files\Folder\";
savefile2.Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc";
savefile2.FilterIndex = 1;
savefile2.FileName = "*.txt";
savefile2.Title = "Save Box Text";
savefile2.OverwritePrompt = true;
if (savefile2.ShowDialog() == DialogResult.OK)
{
stream = savefile2.OpenFile();
if (stream != null)
{
stream.Close();
}
}
}
运行程序时,我单击“保存”按钮查看是否会打开一个对话框,它会产生以下错误:
“无法创建抽象类的实例。”
但是,我没有使用任何抽象类。所有类都位于 .NET 框架内。所以,我被困住了。任何帮助将不胜感激。
So I have been struggling to create a "Save" button in my .NET application. I seem to be doing everything correctly according to my research. I have been referring to this article as a main source: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
The following is my method code in C#:
private void save2(object A_0, EventArgs A_1)
{
Stream stream = new Stream();
savefile2 = new SaveFileDialog();
savefile2.InitialDirectory = @"C:\Program Files\Folder\";
savefile2.Filter = "Text files (*.txt)|*.txt|Word files (*.doc)|*.doc";
savefile2.FilterIndex = 1;
savefile2.FileName = "*.txt";
savefile2.Title = "Save Box Text";
savefile2.OverwritePrompt = true;
if (savefile2.ShowDialog() == DialogResult.OK)
{
stream = savefile2.OpenFile();
if (stream != null)
{
stream.Close();
}
}
}
When running the program, I click the "Save" button to see if it will open a dialog box, and it produces the following error:
"Instances of abstract classes cannot be created."
However, I am not using any abstract classes. All the classes are within the .NET framework. So, I'm stuck. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如前两张海报所提到的,您不能在 Stream 类上调用 new,因为它是抽象的,这里是代码的编译版本以供参考
请注意,最好将流实例化包装在 using() {} 块中确保它们得到妥善处置
As was mentioned by the previous two posters you can't call new on the Stream class because it's abstract, here's a compiling version of your code for reference
Note that it's a good idea to wrap your stream instantiations in a using() {} block to ensure that they are disposed properly
Stream 类是抽象的,您可以使用它正在尝试创建第 1 行中的实例。您实际上从未使用过在那里创建的对象,因此要么不在声明中为其分配任何内容,要么为其分配
null
。The Stream class is abstract, which you're attempting to create an instance of in line 1. You're not actually ever using the object created there, so just either don't assign anything to it in the declaration, or assign
null
to it.问题是
Stream
是一个抽象类,因此无法像使用Stream stream = new Stream();
那样构造Stream stream = new Stream();
将该行更改为Stream stream;< /code> 它应该可以工作。
The problem is
Stream
is an abstract class and therefore cannot be constructed as you did withStream stream = new Stream();
change that line toStream stream;
and it should work.