Is it possible to extend the system classes?
I want to create a function for converting a DateTime into a TimeSpan.
Any Ideas?
at the moment i've created:
public class Convert
{
public static TimeSpan DateTimeToTimeSpan(DateTime value)
{
return new TimeSpan(value.Hour, value.Minute,
value.Second);
}
}
But I like a funtction like:
DateTime inputVar
Timestamp outputVar= inputVar.GetTimeStamp
is this possible? Just extending the systemclass.
thnx
Jon Skeet [C# MVP] - 23 Oct 2007 14:06 GMT
> Is it possible to extend the system classes?
>
> I want to create a function for converting a DateTime into a TimeSpan.
>
> Any Ideas?
<snip>
Well, there are three points here:
1) DateTime and TimeSpan are both structs, not classes, and therefore
can't be derived from.
2) You can't really add methods to an existing type
3) Extension methods in C# 3 make it *look* like you're adding methods
- you could do what you want with C# 3.
Jon
Martin Honnen - 23 Oct 2007 14:16 GMT
> Is it possible to extend the system classes?
Will be possible in the upcoming version of C#/.NET:
<URL:http://weblogs.asp.net/scottgu/archive/2007/03/13/new-orcas-language-feature-ext
ension-methods.aspx>

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Ignacio Machin ( .NET/ C# MVP ) - 23 Oct 2007 14:35 GMT
Hi,
You cannot extend an already defined class (will be possible in the next
version though). You could derive from it but then you need to make
reference to your own class.
Unfortunatelly Convert is marked as sealed so you cannot inherit from it.
In your particular case, I do not see how you will do it. A DateTime and a
TimeSpan represent two different concepts.You cuold only do it IF you set a
starting point only then you could do a TimeSpan.
What would be your starting point ?

Signature
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
> Is it possible to extend the system classes?
>
[quoted text clipped - 21 lines]
>
> thnx
Christof Nordiek - 23 Oct 2007 15:22 GMT
> Is it possible to extend the system classes?
>
> I want to create a function for converting a DateTime into a TimeSpan.
<snip>
> at the moment i've created:
>
[quoted text clipped - 6 lines]
> }
> }
Besides, that this omits seconds fractions, it's the same as TimeOfDay, wich
allready is in DateTime.
Christof