类范围问题 [英] Class scoping problem

查看:45
本文介绍了类范围问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我对类作用域有疑问。



我在文件菜单中声明一个名为syncData1的类实例新选择。该类在我的main()外面声明,称为syncJob。



当我选择File-> Exit我试图保存一些我的syncJob类数据到一个文件,但是XML序列化程序抱怨实例名称syncData1,我在下面的代码中用两个位置标记了//< ----



我认为只要syncJob类是公共的,我就可以在我的应用程序命名空间内访问它。



我的syncJob类在下面(外面)我的主类并将其作为最后一个类在主类中移动,但这没有任何区别。



编译错误是:

当前上下文中不存在名称syncData1。



感谢您的帮助...



I'm new to C# and I have a question about class scoping.

I'm declaring a class instance called syncData1 in a file menu New selection. The class is declared at the end of my main() outside of it, called syncJob.

When I select File->Exit I am trying to save some of my syncJob class data to a file but the XML serializer is complaining about the instance name syncData1 and I have marked those in two locations in the code below with //<----

I thought as long as the syncJob class was public I could access it inside my app's namespace.

I had my syncJob class below (outside) my main class and moved it up inside the main class as the last class, but that didn't make any difference.

The compiler error is:
The name 'syncData1' does not exist in the current context.

Thanks for any help...

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
    // File->Exit
    string path = @"C:\SyncData";
    if (Directory.Exists(path))
    {

    }
    else
    {
        Directory.CreateDirectory(path);
    }
    XmlSerializer serializer = new XmlSerializer(syncData1.GetType());//<----
    StreamWriter writer = new StreamWriter(path + @"\syncJob1.xml");
    serializer.Serialize(writer.BaseStream, syncData1);// <----

    Close();
}
private void newSyncJobToolStripMenuItem_Click(object sender, EventArgs e)
{
    //Create new sync job menu selection
    SyncJob syncData1 = new SyncJob();
}
public class syncJob
{
  <snip out code>
}

推荐答案

这个实例在不同方法的堆栈框架中是不是很明显,永远不能从任何其他上下文?



当你创建这个实例时,它会被无法访问到你的程序中,所以它只会被删除但是垃圾收集器。整个行动都没用。



(要了解垃圾收集,请参阅: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ]。)



同时,在 exitToolStripMenuItem_Click 此实例根本没有任何价值。在第一次出现这个未声明的名称时,你只需要它来获取类,但你已经知道了这个类,这是 SyncJob 。序列化它时,您可以使用与 newSyncJobToolStripMenuItem_Click 中相同的方式创建实例,但为什么?您不需要序列化未填充任何数据的对象。换句话说,代码并不表示你理解你想要实现的目标。



对于序列化,我不建议使用 XmlSerializer 。更加强大,通用,可支持且易于使用的方法是数据合同

http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ]。



最后,对不起,没有具体的帮助,因为整个想法没有意义(或者,我会说,缺席)。我确信,在进行序列化,UI和其他高级内容之前,您应该解决一些更简单的问题,以了解类型和成员,实例,以及本质上是OOP。现在,你根本没有信心。不用担心:它会很快到来,但你需要避免冲进更复杂的应用程序,首先学习基础知识。



-SA
Isn't that obvious that this instance is in the stack frame of a different method, never accessible from any other context?

When you create this instance, it will be made unreachable to your program, so it will simply be removed but the Garbage Collector. The whole action is useless.

(To understand Garbage Collection, please see: http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].)

At the same time, in exitToolStripMenuItem_Click this instance has no value at all. In first occurrence of this undeclared name, you only need it to get the class, but you already know the class, this is SyncJob. When you serialize it, you can create the instance in the same exact way as in newSyncJobToolStripMenuItem_Click, but why? You don't need to serialize an object which is not populated with any data. In other words, the code does not show that you understand what you want to achieve.

For serialization, I don't recommend using XmlSerializer. Much more robust, universal, supportable and easy to use approach is Data Contract:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

And ultimately, sorry, there is no concrete help, as the whole idea makes no sense (or, I would say, absent). I'm sure that, before doing serialization, UI and other advanced things, you should solve some simpler problems to understand types and members, instances, and, essentially, OOP. Right now, you have no confidence at all. Not to worry: it will come soon, but you need to avoid rushing into more complex applications, learn the basics first.

—SA


除了应该或不应该使用XmlSerializer,这是你的问题:



更改



Aside from the should or should not use XmlSerializer, here is your problem:

Change

XmlSerializer serializer = new XmlSerializer(syncData1.GetType());//<----











To

XmlSerializer serializer = new XmlSerializer(typeof(SyncData));//<----





正如谢尔盖所​​说,你需要将syncData1声明为类变量,而不是只存在于单个函数中的变量。您应该了解范围以及如何通过声明变量的位置来控制范围。只需在函数外部移动声明(不一定是初始化)就可以解决很多问题。



As Sergey said, you need to declare syncData1 as a class variable, not one that just resides in a single function. You should learn about scope and how to control it by where you declare your variables. Simply moving the declaration (not necessarily the initialization) outside of the function will solve a lot of your problems.


做这个改变。



声明:在你声明newSyncJobToolStripMenuItem_Click的类中执行此操作。

SyncJob syncData1 = null;



Do this changes.

Declaration: Do this in the class where you have newSyncJobToolStripMenuItem_Click declared.
SyncJob syncData1=null;

private void newSyncJobToolStripMenuItem_Click(object sender, EventArgs e)
{
    //Create new sync job menu selection
    syncData1 = new SyncJob();
}


这篇关于类范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆