Wrapper class template

R

Ralfeus

Hi all.
I have a class, which has some data. I work with list of objects of
this class. The data of each object can have different types. For each
type of data should has its own Value property to be correct. It looks
like this:

public class DataElementInt32 : DataElement
{
public override object Value
{
get
{
try
{
return Convert.ToInt32(ExtractData());
}
catch (Exception)
{
return null;
}
}
}

public DataElementInt32()
: base(typeof(System.Int32))
{
}
}

public class DataElementString : DataElement
{
public override object Value
{
get
{
try
{
return ExtractData().ToString();
}
catch (Exception)
{
return null;
}
}
}

public DataElementString()
: base(typeof(System.String))
{
}
}


I suspect I'll need some more such classes for another types in the
future (may be boolean, float....). So the question: is it possible to
reduce classes number using some template or something like this?
Thanks
 
C

Chris Dunaway

Hi all.
I have a class, which has some data. I work with list of objects of
this class. The data of each object can have different types. For each
type of data should has its own Value property to be correct. It looks
like this:

public class DataElementInt32 : DataElement
{
public override object Value
{
get
{
try
{
return Convert.ToInt32(ExtractData());
}
catch (Exception)
{
return null;
}
}
}

public DataElementInt32()
: base(typeof(System.Int32))
{
}
}

public class DataElementString : DataElement
{
public override object Value
{
get
{
try
{
return ExtractData().ToString();
}
catch (Exception)
{
return null;
}
}
}

public DataElementString()
: base(typeof(System.String))
{
}
}

I suspect I'll need some more such classes for another types in the
future (may be boolean, float....). So the question: is it possible to
reduce classes number using some template or something like this?
Thanks

Could you use a generic class?

public class DataElement<T> : DataElement
{
public override T Value
{
get
{
try
{
return ExtractData<T>());
}
catch (Exception)
{
return null;
}
}
}

I'm not sure if this will help you, but it's an idea
 

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