For my application I need to store some data in an array with the properties (string main
, string[] status
, int curParCount
, etc.).
I am currently storing it in this class:
class Repository
{
public static Rep[] rep = new Rep[6];
public struct Rep
{
public string main;
public string clean;
public int curParCount;
public int totalCount;
public int parStart;
public int partialStart;
public double scrollPos;
public int selectionStart;
public int selectionEnd;
public string[] status;
}
public static string repName()
{
string name;
if (MainWindow.repnum == 0)
{ name = "Main Text"; }
else { name = "Repository " + MainWindow.repnum; }
return name;
}
public static string getStatus(int repNum, int statNum)
{
return rep[repNum].status[statNum];
}
}
Is this the right way for me to do this? It sure doesn't feel like it is.
The basic idea is fine. The implementation could be improved. In particular, I'm concerned that you have a mutable struct; either make it immutable or turn it into a class. I'd also recommend changing the public fields into public properties with automatic backing fields (and then upper-casing the names).
edit
Here's my version:
class Repository
{
public class Rep
{
public string Main {get; set;}
public string Clean {get; set;}
public int CurParCount {get; set;}
public int TotalCount {get; set;}
public int ParStart {get; set;}
public int PartialStart {get; set;}
public double ScrollPos {get; set;}
public int SelectionStart {get; set;}
public int SelectionEnd {get; set;}
public string[] Statuses {get; set;}
}
public const int StatusCount = 6;
public static List<Rep> Reps = new List<Rep>();
public static string Name
{
get
{
if (MainWindow.repnum == 0)
return "Main Text";
return "Repository " + MainWindow.repnum;
}
}
public static string GetStatus(int repIndex, int statIndex)
{ return Reps[repIndex].Status[statIndex]; }
}
Well, the way I would do it...
Public Class Repository: ObservableCollection<RepositoryItem>
{
}
public class RepositoryItem
{
public string main {get; set};
public string clean {get; set};
public int curParCount {get; set};
public int totalCount {get; set};
public int parStart {get; set};
public int partialStart {get; set};
public double scrollPos {get; set};
public int selectionStart {get; set};
public int selectionEnd {get; set};
public string[] status {get; };
}
Basically, this is a class of RepositoryItem (which holds your data, and any functions that you may need to utilize to IMPACT that data) and then a class Repository, which inherits the ObservableCollection class, typing it to the RepositoryItem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With