Wrapper class template

  • Thread starter Thread starter Ralfeus
  • Start date Start date
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
 
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
 
Back
Top