M
Marcus
I have a couple of classes looking like this:
========================================
[Serializable]
public class AdminSettingsData
{
public string adminSettingsPath;
public string adminSettingsFile;
public string usersSettingsPath;
public bool playersHaveToLogin;
private TBusers[] users = new TBusers[100];
private int userCount;
public TBusers[] Users
{
get { return users; }
set { users = value; }
}
public int UserCount
{
get {return userCount; }
set {userCount = value; }
}
}
[Serializable]
public class TBusers
{
public string name;
public string passwd;
}
In the constructor I do:
=====================
// this variable is passed to other forms in the app for
// letting the administrator add
// or remove entries of users
adminData = new AdminSettingsData();
for (int i = 0; i < 100; i++)
{
adminData.Users = new TBusers();
}
I then initializes some name and passwords in TBusers and Serializes
this with binaryFormatter to a file.
When I later on Deserializes this file by the line:
adminData = (AdminSettingsData)formatter.Deserialize(fs);
I get the names and passwords from the file (I have only initialized 4
of them ([0]-[3]).
The problem is that after Deserialization when I want to add one more
name and password to adminData.Users[4] then multiple entries (like
adminData.Users[5] and adminData.Users[6]) are also filled with that
name and password and likewise when I try nullify the entries 5 and 6,
I also gets entry 4 nullified. It is like multiple entries in my array
are referencing the same memory.
If I initialize the entry adminData.Users[4] with name and password
before the Deserialization then it behaves like expected (only
adminData.Users[4] is touched).
Question 1:
===========
shouldn't
private TBusers[] users = new TBusers[100];
be enough to allocate my array. Why is the loop necessary?
for (int i = 0; i < 100; i++)
{
adminData.Users = new TBusers();
}
Question 2:
============
Is it the binaryFormatter that compresses all the tailing null entries
in adminData.Users array or what is happening here?
I am new to c#. Any help would be greatly appreciated!
========================================
[Serializable]
public class AdminSettingsData
{
public string adminSettingsPath;
public string adminSettingsFile;
public string usersSettingsPath;
public bool playersHaveToLogin;
private TBusers[] users = new TBusers[100];
private int userCount;
public TBusers[] Users
{
get { return users; }
set { users = value; }
}
public int UserCount
{
get {return userCount; }
set {userCount = value; }
}
}
[Serializable]
public class TBusers
{
public string name;
public string passwd;
}
In the constructor I do:
=====================
// this variable is passed to other forms in the app for
// letting the administrator add
// or remove entries of users
adminData = new AdminSettingsData();
for (int i = 0; i < 100; i++)
{
adminData.Users = new TBusers();
}
I then initializes some name and passwords in TBusers and Serializes
this with binaryFormatter to a file.
When I later on Deserializes this file by the line:
adminData = (AdminSettingsData)formatter.Deserialize(fs);
I get the names and passwords from the file (I have only initialized 4
of them ([0]-[3]).
The problem is that after Deserialization when I want to add one more
name and password to adminData.Users[4] then multiple entries (like
adminData.Users[5] and adminData.Users[6]) are also filled with that
name and password and likewise when I try nullify the entries 5 and 6,
I also gets entry 4 nullified. It is like multiple entries in my array
are referencing the same memory.
If I initialize the entry adminData.Users[4] with name and password
before the Deserialization then it behaves like expected (only
adminData.Users[4] is touched).
Question 1:
===========
shouldn't
private TBusers[] users = new TBusers[100];
be enough to allocate my array. Why is the loop necessary?
for (int i = 0; i < 100; i++)
{
adminData.Users = new TBusers();
}
Question 2:
============
Is it the binaryFormatter that compresses all the tailing null entries
in adminData.Users array or what is happening here?
I am new to c#. Any help would be greatly appreciated!