What is the best data structure to implement this? [Nested Properties in C#]
I am trying to figure out how to store and organize the following data efficiently within a data structure in C#. The data are settings which each have a corresponding parameter name (string), label (string), and possibly other items.
For example, the data in C++:
//Analog Inputs
#define PROBETEMPERATURE "P33352"
#define PROBETEMPERATURE2 "P33353"
...
//Digital Inputs
#define RACKTEMPERATURE1 "P33384"
#define RACKTEMPERATURE2 "P33385"
...
//Analog Input Labels
#define LBLPROBETEMPERATURE "Probe Temperature"
#define LBLPROBETEMPERATURE2 "Probe Temperature 2"
...
//Digital Input Labels
#define LBLRACKTEMPERATURE1 "Rack Temperature 1"
#define LBLRACKTEMPERATURE1 "Rack Te开发者_如何学Pythonmperature 2"
...
Is there a way to efficiently structure this using nested properties, simply using const
's, or a better way? For example implementation would be:
AnalogInput.PROBETEMPERATURE.Paramater;
AnalogInput.PROBETEMPERATURE.Label;
DigitalInput.RACKTEMPERATURE1.Paramater;
DigitalInput.RACKTEMPERATURE1.Label;
I am trying to avoid creating static objects for each one as there are many.
Thanks.
something like this might help (dont know if it is the best but i would have used it)
declare Class of both types and use them like
public class AnalogInput
{
public string Label { get; set; }
public string Parameter { get; set; }
}
public class DigitalInput
{
public string Label { get; set; }
public string Parameter { get; set; }
}
You may consume them like
create an instance
DigitalInput digiIn = new DigitalInput();
digiIn.Label = "This is digital Input";
digiIn.Parameter = "This is digital Parameter"
if you want to have many values of digital inputs or analogue input you may want to declare List<DigitalInput> digiData = new List<DigitalInput>();
then add digiIn
to it like
digiData.Add(digiIn);
same can be done for analogue
let me show you how
List <DigitalInput> digiData = new List<DigitalInput>();
for(int x =0;x<10;x++)
{
DigitalInput digiIn = new DigitalInput();
digiIn.Label = "This is digital Input No "+ x.ToString();
digiIn.Parameter = "This is digital Parameter No "+ x.ToString();
digiData.Add(digiIn);
}
and to show the values you may do
foreach (DigitalInput dataToDisplay in digiData )
{
MessageBox.Show("Label is :" + dataToDisplay.Label +" and Parameter is " + dataToDisplay.Parameter);
}
if you used #defines in c++, then probably enum should be your choise
You can use GetName method of Enum class http://msdn.microsoft.com/en-us/library/system.enum.getname.aspx
Also see the last example here http://msdn.microsoft.com/en-us/library/cc138362.aspx
Name of each enum value can have '_' in space place ( "Probe_Temperature" instead of "Probe Temperature" ), then simply string.replace or maybe try to write extension method, which will give you correct label
Edit: oops, sorry, i failed you are using strings as params ( "P33352" ), enum is integer, so thats probably not solution for you
I'm assuming that is just a snippet of what you need to do and that the probe/rack temperatures are not static but are in a database or something. Here is a structures that will do what you want:
List<TemperatureInfo> infos = new List<TemperatureInfo>()
{
new TemperatureInfo("Probe")
{
new Temperature("P33352", "Temperature 1"),
new Temperature("P33353", "Temperature 2"),
},
new TemperatureInfo("Rack")
{
new Temperature("P33384", "Temperature 1"),
new Temperature("P33385", "Temperature 2"),
}
};
public class TemperatureInfo : List<Temperature>
{
public string Name { get; set; }
public TemperatureInfo(string name)
{
this.Name = name;
}
}
public class Temperature
{
public string ID { get; set; }
public string Label { get; set; }
public Temperature(string id, string label)
{
this.ID = id;
this.Label = label;
}
}
PS: I wish SO formatted my code properly. :(
Perhas something like this?
public class Input
{
public InputType InputType { get; set; }
public Temperature ProbeTemperature { get; set; }
public Temperature RackTemperature { get; set; }
}
public class Temperature
{
public string Parameter { get; set; }
public string Label { get; set; }
}
public enum InputType
{
Digital,
Analog
}
Then use like so:
List<Input> inputs = new List<Input>();
Input x = new Input() { InputType = InputType.Digital };
x.RackTemperature = new Temperature { Parameter = "P", Label = "L" };
x.ProbeTemperature = new Temperature { Parameter = "P', Label = "L" };
inputs.Add(x);
I guess you can go for a generic Settings class which has instance variables like parameter and label. Then you can extend this class for each setting, and add any other parameters.
public class Settings
{
public readonly string Parameter;
public readonly string Label;
public Settings(string parameter, string label)
{
Parameter = parameter;
Label = label;
}
}
public class ProbeTemperature : Settings
{
..
}
I think all the other answers are also great. Probably you need to be more specific on what you exactly want to achieve.
精彩评论