A
aine_canby
Hi,
I have the following simple class:
/// <summary>
/// Representative of a lab
/// </summary>
public class Lab
{
string name;
public string Name { set { name = value; } get { return
name; } }
string directory;
public string Directory { set { directory = value; } get
{ return directory; } }
/// <summary>
/// Constructor
/// </summary>
public Lab()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="name">Lab Name</param>
/// <param name="directory">Directory Path</param>
public Lab(string name, string directory)
{
this.name = name;
this.directory = directory;
}
public bool Exists()
{
return new DirectoryInfo(directory).Exists;
}
}
I then have another class which stores objects of type Lab, as
follows:
public List<Lab> labs = new List<Lab>();
which I populates during its construction with:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Lab>));
using (StreamReader streamReader = new
StreamReader(xmlLabDirectoriesFileName))
{
labs = (List<Lab>)xmlSerializer.Deserialize(streamReader);
}
// remove labs from the list if they don't exist locally
for (int i = labs.Count - 1; i >= 0; i--)
{
if (!labs.Exists())
labs.Remove(labs);
}
The code works but it feels like it could be done better. Perhaps it
would be smarter with a LabCollection class that would block the
addition of a lab, if it doesn't exist, during serialization - rather
than doing it afterwards as I am doing. What do you think?
Always interested in learning better ways to do things,
Thanks,
Aine.
I have the following simple class:
/// <summary>
/// Representative of a lab
/// </summary>
public class Lab
{
string name;
public string Name { set { name = value; } get { return
name; } }
string directory;
public string Directory { set { directory = value; } get
{ return directory; } }
/// <summary>
/// Constructor
/// </summary>
public Lab()
{
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="name">Lab Name</param>
/// <param name="directory">Directory Path</param>
public Lab(string name, string directory)
{
this.name = name;
this.directory = directory;
}
public bool Exists()
{
return new DirectoryInfo(directory).Exists;
}
}
I then have another class which stores objects of type Lab, as
follows:
public List<Lab> labs = new List<Lab>();
which I populates during its construction with:
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Lab>));
using (StreamReader streamReader = new
StreamReader(xmlLabDirectoriesFileName))
{
labs = (List<Lab>)xmlSerializer.Deserialize(streamReader);
}
// remove labs from the list if they don't exist locally
for (int i = labs.Count - 1; i >= 0; i--)
{
if (!labs.Exists())
labs.Remove(labs);
}
The code works but it feels like it could be done better. Perhaps it
would be smarter with a LabCollection class that would block the
addition of a lab, if it doesn't exist, during serialization - rather
than doing it afterwards as I am doing. What do you think?
Always interested in learning better ways to do things,
Thanks,
Aine.