extending system class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • active T

    #1

    extending system class

    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 DateTimeToTimeS pan(DateTime value)
    {
    return new TimeSpan(value. Hour, value.Minute,
    value.Second);
    }
    }


    But I like a funtction like:

    DateTime inputVar
    Timestamp outputVar= inputVar.GetTim eStamp


    is this possible? Just extending the systemclass.

    thnx

  • Jon Skeet [C# MVP]

    #2
    Re: extending system class

    On Oct 23, 2:03 pm, active T <TonnyBr...@gma il.comwrote:
    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

    Comment

    • Martin Honnen

      #3
      Re: extending system class

      active T wrote:
      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-extension-methods.aspx>



      --

      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: extending system class

        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 ?

        --
        Ignacio Machin
        The #1 Warehouse Management System & Direct Store Delivery Software (DSD) for QuickBooks & ERP Systems – LaceUp Solutions

        Mobile & warehouse Solutions.
        "active T" <TonnyBrink@gma il.comwrote in message
        news:1193144612 .674320.80700@k 35g2000prh.goog legroups.com...
        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 DateTimeToTimeS pan(DateTime value)
        {
        return new TimeSpan(value. Hour, value.Minute,
        value.Second);
        }
        }
        >
        >
        But I like a funtction like:
        >
        DateTime inputVar
        Timestamp outputVar= inputVar.GetTim eStamp
        >
        >
        is this possible? Just extending the systemclass.
        >
        thnx
        >

        Comment

        • Christof Nordiek

          #5
          Re: extending system class

          "active T" <TonnyBrink@gma il.comschrieb im Newsbeitrag
          news:1193144612 .674320.80700@k 35g2000prh.goog legroups.com...
          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:
          >
          public class Convert
          {
          public static TimeSpan DateTimeToTimeS pan(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

          Comment

          Working...