User defined types in application settings

  • Thread starter Thread starter Tony Tallman
  • Start date Start date
T

Tony Tallman

Is there a way to store an object of a user defined type in the applications
settings?

I have compiled the type in a dll and referenced the dll into my
application.
I have applied the [Serializable] attribute to my user defined type.
I can create a settings property of the correct type, but it won't serialize
(or won't deserialize).

Example:

public class State
{
bool b1;
string s1;
int n1;

public State(bool b, string s, int n)
{
b1=b; s1=s; n1=1;
}
}

public void TestStateSetting()
{
Settings.Default.MyState = new State(true, "Hi", 0);
Settings.Default.MyString = "System.string serializes ok";

// Save and reload settings to simulate program shutdown and restart.
Settings.Default.Save();
Settings.Default.Reload();

// Primitive types work but user defined (and other system types) don't
work.
MessageBox.Show("Settings.Default.MyState == null !!");
MessageBox.Show("Settings.Default.MyString == 'System.string serializes
ok'");
}
 
Is there a way to store an object of a user defined type in the applications
settings?

I have compiled the type in a dll and referenced the dll into my
application.
I have applied the [Serializable] attribute to my user defined type.
I can create a settings property of the correct type, but it won't serialize
(or won't deserialize).

Example:

public class State
{
bool b1;
string s1;
int n1;

public State(bool b, string s, int n)
{
b1=b; s1=s; n1=1;
}
}

public void TestStateSetting()
{
Settings.Default.MyState = new State(true, "Hi", 0);
Settings.Default.MyString = "System.string serializes ok";

// Save and reload settings to simulate program shutdown and restart.
Settings.Default.Save();
Settings.Default.Reload();

// Primitive types work but user defined (and other system types) don't
work.
MessageBox.Show("Settings.Default.MyState == null !!");
MessageBox.Show("Settings.Default.MyString == 'System.string serializes
ok'");
}
Tony,

It might be easier to just Serialize the class and write it to a file
using binary serialization.

I believe if you search the help for SerializableAttribute you will
find a description of the attribute and some sample code to get you
started.

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
Is there a way to store an object of a user defined type in the applications
settings?

I have compiled the type in a dll and referenced the dll into my
application.
I have applied the [Serializable] attribute to my user defined type.
I can create a settings property of the correct type, but it won't serialize
(or won't deserialize).

Example:

public class State
{
bool b1;
string s1;
int n1;

public State(bool b, string s, int n)
{
b1=b; s1=s; n1=1;
}
}

public void TestStateSetting()
{
Settings.Default.MyState = new State(true, "Hi", 0);
Settings.Default.MyString = "System.string serializes ok";

// Save and reload settings to simulate program shutdown and restart.
Settings.Default.Save();
Settings.Default.Reload();

// Primitive types work but user defined (and other system types) don't
work.
MessageBox.Show("Settings.Default.MyState == null !!");
MessageBox.Show("Settings.Default.MyString == 'System.string serializes
ok'");
}
Oops! I see you've already found the attribute...

I still would serialize it to a binary file, though...

Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 
Back
Top