C
Cralis
Hi guys...
I have a class, which has a type within it that is only determined
when I create the object. So, I am thinking, this is a job for
Generics.
My class looks like this:
public class ConfigurationOption<T>
{
#region Class Properties
public string Name { get; set; }
public string Description { get; set; }
public ConfigOptionValueType ValueType { get; set; }
public DefaultParameterStructure.ParameterGroup Group { get;
set; }
public bool IsVisible { get; set; }
public bool IsReadOnly { get; set; }
public bool DisplayAsYesNo { get; set; }
public T Value { get; set; }
public T MinimumValue { get; set; }
public T MaximumValue { get; set; }
public T OriginalValue { get; set; }
public bool Modified
{
get
{
return Value.Equals(OriginalValue);
}
}
}
I haven't worked with generics much, but am already stuck. I'm not
100% clear how I should create an object, for a start. The T could be
a string, a float or an int. This is determined based on a bit of data
I recieve from an extrenal source. Part of the string based data I
recieve is a 'type' parameter, and based on that, I'd like to create a
ConfigurationOption object based on either a float, int or string.
How do I do that cleanly?
Then, my second snag:
Later, I need to itterate though a list of these objects. I thought I
could do:
foreach (ConfigurationOption co in
DeviceServices.ConfigurationOptions)...
However, the editor has informed me of an invalid number of
parameters.
I tried:
foreach (ConfigurationOption<T> co in
DeviceServices.ConfigurationOptions)
Failed as well.
I then checked what my list of these objects is declared as, and since
introducing the <T>, I get an error at my declaration as well.
I used this:
public static List<ConfigurationOption> ConfigurationOptions { get;
set; }
But it seems again, invalid number of parameters.
Could someone assist me with this?
I have a class, which has a type within it that is only determined
when I create the object. So, I am thinking, this is a job for
Generics.
My class looks like this:
public class ConfigurationOption<T>
{
#region Class Properties
public string Name { get; set; }
public string Description { get; set; }
public ConfigOptionValueType ValueType { get; set; }
public DefaultParameterStructure.ParameterGroup Group { get;
set; }
public bool IsVisible { get; set; }
public bool IsReadOnly { get; set; }
public bool DisplayAsYesNo { get; set; }
public T Value { get; set; }
public T MinimumValue { get; set; }
public T MaximumValue { get; set; }
public T OriginalValue { get; set; }
public bool Modified
{
get
{
return Value.Equals(OriginalValue);
}
}
}
I haven't worked with generics much, but am already stuck. I'm not
100% clear how I should create an object, for a start. The T could be
a string, a float or an int. This is determined based on a bit of data
I recieve from an extrenal source. Part of the string based data I
recieve is a 'type' parameter, and based on that, I'd like to create a
ConfigurationOption object based on either a float, int or string.
How do I do that cleanly?
Then, my second snag:
Later, I need to itterate though a list of these objects. I thought I
could do:
foreach (ConfigurationOption co in
DeviceServices.ConfigurationOptions)...
However, the editor has informed me of an invalid number of
parameters.
I tried:
foreach (ConfigurationOption<T> co in
DeviceServices.ConfigurationOptions)
Failed as well.
I then checked what my list of these objects is declared as, and since
introducing the <T>, I get an error at my declaration as well.
I used this:
public static List<ConfigurationOption> ConfigurationOptions { get;
set; }
But it seems again, invalid number of parameters.
Could someone assist me with this?