Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static to object and serialize

Status
Not open for further replies.

mrdance

Programmer
Apr 17, 2001
308
SE
I have a class with some static methods. Some variables are public. I define a static object like this:

public static SoundClass Sounds;

In the constructor I do this:

SoundClass Sounds = new SoundClass();

Later I have set some variables like this:

Sounds.SoundOn = true;

When I push a button I would like to serialize this into an XMLfile. I have understood that you can't serialize static objects so I tried this:

SoundClass s = new SoundClass();
s = Sounds;

SerializeFunction(s);

The problem is that:

s = Sounds don't work correctly. Sounds is undefined. I thought that I could come around this by copying the static object this way to this new object. The SerializeFunction work when I don't copy it. Is there anyway to copy and serialize this static object?

thanks!

--- neteject.com - Internet Solutions ---
 
When you write:
Code:
SoundClass s = new SoundClass();
s = Sounds;
you do nothing. The first line allocate a place for SoundClass instance named s. The second line redirect the s reference (like a pointer in c++) to the Sounds instance. the garbage collector sees that you lost the allocation of s instance and will collect it.
In the constructor you wrote:
Code:
SoundClass Sounds = new SoundClass();
Now you have two instances called Sounds. The first is the static variable you declared and is still null (why you did it static?). The second is the one you declared in the constructor. When the constructor ends, all variables you declare in it are collected.
You have to write:
Code:
Sounds = new SoundClass();
As for serialization, I'm not an expert in serialization, but I suggest you make another class that have all properties af SoundClass declared as nonstatic. Copy the properties values to an instance of this class and serialize it.
Think again if you must make the SoundClass properties static.
 
I want it static because then I can, during runtime, just write:

Settings.Sounds.SoundOn = true;

But I want to serialize it so that I can save all the values easy.

thanks anyway!

--- neteject.com - Internet Solutions ---
 
For this reason you need the Sounds instance to be static, not the SoundOn variable.
 
No, only Sounds is static. SoundOn (which is in the class sounds) don't have to be static for me to reach the variables. Right now I say that Sounds is static in a class called settings. This let me do this:

Settings.Sounds.SoundOn = true;

I can do this anytime, anywhere in my program. I can also read the values. The rules for serializing is that it is a real object. Of course I can create a object and then copy each value like this:

SoundClass s = new SoundClass();
s.SoundOn = Settings.Sounds.SoundOn;
etc.

and then:

Serialize(s);

This _seems_ unnessesary. I just want to do this:

s = Settings.Sounds;

Unfortunately this don't work. :(

--- neteject.com - Internet Solutions ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top