StrangeWill
IS-IT--Management
using System;
using System.Collections.Generic;
using System.Text;
namespace SaveTest
{
class ProgramSetting
{
private string str_Name;
private object obj_Value;
public string Name
{
get { return str_Name; }
set { str_Name = value; }
}
public object Value
{
get { return obj_Value; }
set { obj_Value = value; }
}
public ProgramSetting()
{
}
public ProgramSetting(string inName, object inValue)
{
str_Name = inName;
obj_Value = inValue;
}
}
class ProgramSettingsCollection : List<ProgramSetting>
{
public ProgramSetting GetSetting(string inName)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
return ps;
}
}
throw new Exception("Setting not found");
}
public object GetValue(string inName)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
return ps;
}
}
throw new Exception("Setting not found");
}
public void SetValue(string inName, object inValue)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
ps.Value = inValue;
return;
}
}
NewSetting(inName, inValue);
}
public void NewSetting(string inName)
{
this.Add(new ProgramSetting(inName, ""));
}
public void NewSetting(string inName, object inValue)
{
this.Add(new ProgramSetting(inName, inValue));
}
}
class Program
{
static ProgramSettingsCollection lst_ProgramSettings = new ProgramSettingsCollection();
static void Main(string[] args)
{
int test = -2;
try
{
test = ((ProgramSettingsCollection)Settings1.Default.Test).Count;
Console.WriteLine("Settings exist, yay!");
}
catch
{
}
if (test == -2)
{
Console.WriteLine("Settings do not exist, creating new ones");
CreateSettings();
SaveSettings();
}
Console.ReadLine();
}
private static void SaveSettings()
{
Settings1.Default.Test = lst_ProgramSettings;
Settings1.Default.Save();
}
private static void CreateSettings()
{
lst_ProgramSettings.Add(new ProgramSetting("test", "test"));
lst_ProgramSettings.Add(new ProgramSetting("test2", "test2"));
}
}
}
So basically as you see, I'm trying to save an object that I created into the settings file, the settings file type is set to "System.Object" even trying to set it to "SaveTest.ProgramSettingsCollection" doesn't work, strings save fine.
What is my problem? Shouldn't this be possible?
using System.Collections.Generic;
using System.Text;
namespace SaveTest
{
class ProgramSetting
{
private string str_Name;
private object obj_Value;
public string Name
{
get { return str_Name; }
set { str_Name = value; }
}
public object Value
{
get { return obj_Value; }
set { obj_Value = value; }
}
public ProgramSetting()
{
}
public ProgramSetting(string inName, object inValue)
{
str_Name = inName;
obj_Value = inValue;
}
}
class ProgramSettingsCollection : List<ProgramSetting>
{
public ProgramSetting GetSetting(string inName)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
return ps;
}
}
throw new Exception("Setting not found");
}
public object GetValue(string inName)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
return ps;
}
}
throw new Exception("Setting not found");
}
public void SetValue(string inName, object inValue)
{
foreach (ProgramSetting ps in this)
{
if (ps.Name == inName)
{
ps.Value = inValue;
return;
}
}
NewSetting(inName, inValue);
}
public void NewSetting(string inName)
{
this.Add(new ProgramSetting(inName, ""));
}
public void NewSetting(string inName, object inValue)
{
this.Add(new ProgramSetting(inName, inValue));
}
}
class Program
{
static ProgramSettingsCollection lst_ProgramSettings = new ProgramSettingsCollection();
static void Main(string[] args)
{
int test = -2;
try
{
test = ((ProgramSettingsCollection)Settings1.Default.Test).Count;
Console.WriteLine("Settings exist, yay!");
}
catch
{
}
if (test == -2)
{
Console.WriteLine("Settings do not exist, creating new ones");
CreateSettings();
SaveSettings();
}
Console.ReadLine();
}
private static void SaveSettings()
{
Settings1.Default.Test = lst_ProgramSettings;
Settings1.Default.Save();
}
private static void CreateSettings()
{
lst_ProgramSettings.Add(new ProgramSetting("test", "test"));
lst_ProgramSettings.Add(new ProgramSetting("test2", "test2"));
}
}
}
So basically as you see, I'm trying to save an object that I created into the settings file, the settings file type is set to "System.Object" even trying to set it to "SaveTest.ProgramSettingsCollection" doesn't work, strings save fine.
What is my problem? Shouldn't this be possible?