Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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...

C#
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>
}
Posted

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
 
Share this answer
 
v2
Comments
rfresh 10-Sep-13 14:38pm    
Thank you Sergey.
(1) In the syncJob Class Constructor I initialized it's fields but did the <snip code="" out=""> since XMLserialization wasn't relevant to the question so you didn't see that.
(2) I will look into data contract.
Sergey Alexandrovich Kryukov 10-Sep-13 15:42pm    
Well, I guess you need to do something real in these events, then the problem will disappear...
—SA
Aside from the should or should not use XmlSerializer, here is your problem:

Change

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


To

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


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.
 
Share this answer
 
Comments
rfresh 10-Sep-13 15:46pm    
When I do this:
XmlSerializer serializer = new XmlSerializer(typeof(SyncData));

I get this:
.syncJobData1 is a 'field' but is used like a 'type'
Do this changes.

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

C#
private void newSyncJobToolStripMenuItem_Click(object sender, EventArgs e)
{
    //Create new sync job menu selection
    syncData1 = new SyncJob();
}
 
Share this answer
 
Comments
rfresh 10-Sep-13 14:40pm    
Thanks for pointing that out Arun. That fixed the problem. I moved

SyncJob syncJobData1;

to the top of mainform class.

and updated the new menu selection code per your suggestion:

syncData1 = new SyncJob();
ArunRajendra 11-Sep-13 0:36am    
Can you please mark it as answered. if possible please rate as well ;-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900