Pete.. progress, but here's another spanner tossed into the works.
I've declared my classes like this now:
public abstract class ConfigurationOptionBase
{
#region Class Properties
public string Name { get; set; }
public string Description { get; set; }
public ConfigOptionValueType ValueType { get; set; }
public ConfigOptionGroup Group { get; set; }
public bool IsAdvanced { get; set; }
public bool IsReadOnly { get; set; }
public float? MaximumValue { get; set; }
public float? MinimumValue { get; set; }
public bool DisplayAsYesNo { get; set; }
public abstract string ValueString { get; }
public abstract string OriginalValue { get; set; }
public abstract bool Modified { get; }
public abstract void SetValue(object Input);
#endregion
}
public class ConfigurationOptionString : ConfigurationOptionBase
{
public string Value { get; set; }
public override string OriginalValue { get; set; }
public override bool Modified
{
get { return OriginalValue != Value; }
}
public override string ValueString
{
get { return Value; }
}
public override void SetValue(object Input)
{
Value = Input.ToString();
}
}
public class ConfigurationOptionFloat : ConfigurationOptionBase
{
public float Value { get; set; }
public override string OriginalValue { get; set; }
public override bool Modified
{
get { return OriginalValue != Value.ToString(); }
}
public override string ValueString
{
get { return Value.ToString(Common.Cuture); }
}
public override void SetValue(object Input)
{
Value = float.Parse(Input.ToString());
}
}
So, for now, I have my base class, with a class for Float and a class
for String. I think I had to make the base class abstract, so that my
sub classes can all have the same fields and methods. The reasoning is
that .. when my application started, I create all the objects (before
I have read them from the device), like place holders. So,l they need
a type, before I know what the type will be. So, I have a whole lot of
calls to this method:
private static ConfigurationOptionBase Add(string name, bool
isAdvanced, ConfigOptionGroup group, bool readOnly, bool
displayAsYesNo)
{
ConfigurationOptionBase ko = new ConfigurationOptionString
{
Name = name,
Group = group,
IsReadOnly = readOnly,
IsAdvanced = isAdvanced,
DisplayAsYesNo =
displayAsYesNo
};
return ko;
}
It sets up an object with the name and some other details... And these
all get added to a List<ConfigurationOptionBase>.
I then connect to the device, and start reading the parameters. I get
the name of the parameter from the device, then find the placeholder
object with that name, and set the values:
So, when going through all the incoming data from the device, I do:
ConfigurationOptionBase tmpOption = GetOption(parameterName, config);
GetOption does this:
private static ConfigurationOptionBase GetOption(string
parameterName, Configuration config)
{
foreach (ConfigurationOptionBase co in
config.ConfigOptions)
{
if (co.Name == parameterName)
{
return co;
}
}
Common.WriteLog("== WARNING: " + parameterName + " found
on OSD, but is not known by me. Adding to Advanced.");
ConfigurationOptionBase newOption = new
ConfigurationOptionString
{
Group =
ConfigOptionGroup.ADVANCED,
Name =
parameterName
};
config.ConfigOptions.Add(newOption);
return newOption;
}
So, it finds the placeholder, and returns that object. If the object
with that name doesn't exist, it logs an issue and creates a new
object with default values. And returns that new object.
Now, once I have the returned object, which is a placeholder... I can
then check the incoming ParameterType field... and based on that, I
now need to change the object type from ConfigurationOptionBase to
either ConfigurationOptionFloat or ConfigurationOptionString.
So, I was hoping to do something like this:
ConfigurationOptionBase tmpOption = GetOption(parameterName, config);
switch(parameterType)
{
case "4": // Float
tmpOption =
(ConfigurationOptionFloat)tmpOption;
break;
case "5": // String
tmpOption =
(ConfigurationOptionString)tmpOption;
break;
default:
option = tmpOption;
}
It's not letting that happen, saying the cast is redundent.. which I
can't see how it can be.
Can you see an issue?