Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

extending system class

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
active T - 23 Oct 2007 14:03 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.