Reputation: 1009
I am building a desktop app. In Program.cs
I have this:
public struct Settings
{
public string SiteName;
public string IP3Oktets;
public string Data_Source;
public string Initial_Catalog;
public string officialCardNumber;
public bool KarticaUcitacu;
public Dictionary<string, object> KarticuUbacio;
public Dictionary<string, object> Korisnik;
public Dictionary<string, object> PoslednjiUgovor;
public Version Version;
public string userName;
}
public static Settings mySettings = new Settings();
So only one object to hold app-wide properties.
Then during the logon process I set them up - a procedure that gets data from the database looks like this:
public static Dictionary<string, object> IzŠifreKartice(string pKartica)
{
SqlParameter[] @params =
{
new SqlParameter("@KARTICA", SqlDbType.VarChar, 15) { Value = pKartica },
};
DataTable dt = DB.GetData("SELECT * FROM ZAPOSLENI WHERE KARTICA = @KARTICA", @params);
if (dt.Rows.Count == 1)
{
return dt.Rows[0].Table.Columns
.Cast<DataColumn>()
.ToDictionary(c => c.ColumnName, c => dt.Rows[0][c]); ;
}
else
{
return null;
}
}
Now, based on some condition I have to open another login prompt and I do it like this (I also show here the main form):
this.Hide();
var form2 = new frm_glavna(); //main form
form2.Closed += (ss, args) => this.Close();
form2.Show();
var form3 = new frm_RazduživanjeLogin(); //another login
form3.ActiveControl = form3.txt_Lozinka;
form3.ShowDialog(this);
And after that, when the main form opens, I lose all Dictionary<string, object>
values (they become null).
What I am doing wrong here? Is it possible that the program is keeping their references instead of values?
Thanks, Dejan
update:
FIXED!
I have found that this was causing problem:
Program.Settings s = Program.mySettings;
it seems that this actually copy object somehow instead just keeping a reference on mySettings variable, so I remove it. I will use Program.mySettings even if that is much more to write.
Thanks
Upvotes: 0
Views: 571
Reputation: 168834
You can change the Settings
type from a struct
to a class
, and C# won't copy it on assignment anymore.
Upvotes: 1