extending system class

  • Thread starter Thread starter active T
  • Start date Start date
A

active T

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
 
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
 
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 ?
 
active T said:
Is it possible to extend the system classes?

I want to create a function for converting a DateTime into a TimeSpan.
at the moment i've created:

public class Convert
{
public static TimeSpan DateTimeToTimeSpan(DateTime value)
{
return new TimeSpan(value.Hour, value.Minute,
value.Second);
}
}
Besides, that this omits seconds fractions, it's the same as TimeOfDay, wich
allready is in DateTime.

Christof
 

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

Back
Top