B
Boaz Ben-Porat
I Know, ... a struct can not be inherited.
** Is there any tricky way to extend a struct ? **
I have to implement a component that represent a date (without time) that
has a small number of functions over those of DataTime type. My first thougt
was : "Inherit from DateTime and implement those functions". Then I found
out that DateTime is a struct, not a class, so it can`t be done.
For example, The new type should implement an overloaded '+' operator that
takes an integer instead of TimeSpan.
I can think of 3 models to accomplish the task:
1. Drop implemetation, use DateTime and force all other developers to learn
how to use it.
----------------------------------------------------------------------------
--------------
2. Define a class that implements a minimum extra functionality:
---------------------------------------------------------------
public class TDate
{
public DataTime theDate;
public TDate(DateTime t)
{
theDate = t;
}
public static TDate operator + (TDate t, int iDaysToAdd)
{
// Add 'iDaysToAdd' to member theDate
}
}
All other manipulations are done directly on the inner DateTime member, by
all developers.
** This is the same problem as first model. All developers must work with
DateTime directly ***
3. Define a class as in model 2, but let it implement ALL DateTime methods
in the form:
public <return-type> SomeMethod(<args>)
{
return theDate.SomeMethod(<args>);
}
*** This is a 'slave work' for the one who implements 'TDate' class , but
saves all others from working with DateTime.
Back to the main question: *** Is the a smarter way to do it ? ***
TIA
Boaz Ben-Porat
DataPharm a/s
Denmark
** Is there any tricky way to extend a struct ? **
I have to implement a component that represent a date (without time) that
has a small number of functions over those of DataTime type. My first thougt
was : "Inherit from DateTime and implement those functions". Then I found
out that DateTime is a struct, not a class, so it can`t be done.
For example, The new type should implement an overloaded '+' operator that
takes an integer instead of TimeSpan.
I can think of 3 models to accomplish the task:
1. Drop implemetation, use DateTime and force all other developers to learn
how to use it.
----------------------------------------------------------------------------
--------------
2. Define a class that implements a minimum extra functionality:
---------------------------------------------------------------
public class TDate
{
public DataTime theDate;
public TDate(DateTime t)
{
theDate = t;
}
public static TDate operator + (TDate t, int iDaysToAdd)
{
// Add 'iDaysToAdd' to member theDate
}
}
All other manipulations are done directly on the inner DateTime member, by
all developers.
** This is the same problem as first model. All developers must work with
DateTime directly ***
3. Define a class as in model 2, but let it implement ALL DateTime methods
in the form:
public <return-type> SomeMethod(<args>)
{
return theDate.SomeMethod(<args>);
}
*** This is a 'slave work' for the one who implements 'TDate' class , but
saves all others from working with DateTime.
Back to the main question: *** Is the a smarter way to do it ? ***
TIA
Boaz Ben-Porat
DataPharm a/s
Denmark