Help with generics

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?
 
C

Cralis

Thanks Pete.

The whole morning I was working on the last option you mentioned. ie,
I had an abstract base class, with the common properies (Name,
Description etc).

This seemed to be going well, until I hit the List<> issue (Which is
actually the point at which I thought, 'Hang on, what aboput
generics!'.

The problems I encountered (And this is probably an issue with my
design, or lack of knowledge... or both), was numerous.

1. I have a method that is called GetParameterValue(string
parameterName), which does a ForEach on the list of parameter objects
I have in memory. Problem is, some objects would contain float type
values, some would be string, and some are int. So, what would the
return type of my method be? Looking at it now, the return type could
maybe be the base class of my objects? Can I do that? And then return
the object (inherited from base class) that is macthed in the list?

2. It seems a lot of the time, when I am itterating through the list
of objects, I needs to do a switch(ConfigOptionValueType), and then
based on the type, handle the values differently. I'm wondering if
that can be built into the objects. I guess I could rely on the
ToString(), as most of the time, I am displaying the data in text
boxes, labels etc.

Just to explain the min and max values. I get data from an external
source, which provides me the name (identifier), the value, the
minimum permissable values, the maximim permissible value, and a type,
which is an int, which I then enumerate into ConfigOptionValueType.
 
C

Cralis

Thanks Pete. I sent 6 hours this morning doing the abstract way of
things... then canned it, thinking Generics are the way to go, but ...
I'll make use of the powers of OOP, and go back that way. If it's OK,
I'll fire off a question here as soon as I get stuck?

Thanks,
Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top