473,394 Members | 1,869 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 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

Oct 23 '07 #1
4 2583
On Oct 23, 2:03 pm, active T <TonnyBr...@gmail.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

Oct 23 '07 #2
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/
Oct 23 '07 #3
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
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"active T" <To********@gmail.comwrote in message
news:11*********************@k35g2000prh.googlegro ups.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 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

Oct 23 '07 #4
"active T" <To********@gmail.comschrieb im Newsbeitrag
news:11*********************@k35g2000prh.googlegro ups.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 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

Oct 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: PlasmaDragon | last post by:
I have a class, let's call it "x". I have another class, y, which extends x. Then I have a class z which extends y. There is a function in y (but not in x) which I have overridden in z. But...
4
by: Matt | last post by:
Hi, I've been thinking about how to do this, but can't think of a solution. I have a class that is derived from System.Web.UI.WebControls.DataGrid which works a treat, but I'd like to extend...
2
by: Markus Eßmayr | last post by:
Hello, I'm writing an extension class for the winforms button control using MC++. I created a class, derived from System::Windows::Forms::Button. In my class I want to extend the functionality...
8
by: Anthony Williams | last post by:
Morning all, I'm having a wee problem with a project I'm working on at the moment. I'm leading my company into producing a website, based upon Web Standards, which will be created using XHTML...
5
by: vbgunz | last post by:
Hello everyone. I own two books. Learning Python and Python in a nutshell. When cross referencing the two books to try and clarify the ideas behind extending methods and delegates, this is where...
10
by: Christophe Peillet | last post by:
I am trying to create a BasePage for use in a large asp.net application, that will centrally provide certain extra properties and logic to the application. (The web project makes use Master Pages...
9
by: Fat Elvis | last post by:
I'd like to extend some of my Asp.net pages by using Partial Classes. Example ASP.Net Page: public partial class Admin_Customer : System.Web.UI.Page { protected void Page_Load(object sender,...
3
by: Jeff | last post by:
Hey ASP.NET 2.0 I'm trying to extend the MembershipUser class, and have encounter a problem: << See in the middle of this post for info about why I do this >> << See below of this post for...
3
by: dmoore | last post by:
Hi Folks: I have a question about the use of static members in Python/C extensions. Take the simple example from the "Extending and Embedding the Python Interpreter" docs: A simple module...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.