473,626 Members | 3,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why can't I inherit from DateTime?

Any ideas?
Nov 16 '05 #1
44 16949
Because System.DateTime is a sealed class which means that you
cannot extend (inherit from) it.

But why in the world would you want to extend DateTime?

In the worst case, create a class, and make a DateTime field.

Hope this helps.
Good luck.

On Tue, 22 Feb 2005 18:25:32 -0800, Frank Rizzo <no**@none.co m> wrote:
Any ideas?


--
Regards,
Nurchi BECHED

P.S.
C makes it easy to shoot yourself in the foot;
C++ makes it harder, but when you do,
it blows away your whole leg."
--Bjarne Stroustrup
Nov 16 '05 #2
???? According to the docs DateTime is a value type, a struct and you
cannot extend a struct.

Regards,
Jeff
Because System.DateTime is a sealed class which means that you cannot

extend (inherit from) it.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3
"Frank Rizzo" <no**@none.co m> wrote in message
news:On******** ******@TK2MSFTN GP12.phx.gbl...
Any ideas?


Have a look at value types and reference types, called structs versus
classes in C#.
In many ways, a struct is quite an opposite of a class.

Here are some differences.

A class instance is allocated with reference on stack or heap, with the
object always on heap. A struct instance is allocated directly on stack (for
local instances in methods) or embedded in an object on heap.

A class is inheritable. A struct is implicitly sealed and always inherits
from Object. (This answers your question)

A class can be cast to itself and to any superclass of it's type. A struct
can only be boxed into Object and unboxed to it's sealed type.

A class supports polymorhpism. Structs have limited support for polymorfism
as you can override or reindtroduce methods on the Object class. However,
the abstract or virtual keyword makes non sense on value types and are so
forbidden in C#.

A class instance is garbage collected. A struct instance on stack is simply
removed when a method ends or are implictly collected from heap when when an
class instance is garbage collected.

A class is a beautiful thing. structs sucks! =)

Hope this helps

- Michael S



Nov 16 '05 #4
You forgot one - and this is the reason for their existance - With no boxing, structs are faster and smaller which is why all the primatives are valuetypes and things like System.Drawing. Point

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Frank Rizzo" <no**@none.co m> wrote in message
news:On******** ******@TK2MSFTN GP12.phx.gbl...
Any ideas?


Have a look at value types and reference types, called structs versus
classes in C#.
In many ways, a struct is quite an opposite of a class.

Here are some differences.

A class instance is allocated with reference on stack or heap, with the
object always on heap. A struct instance is allocated directly on stack (for
local instances in methods) or embedded in an object on heap.

A class is inheritable. A struct is implicitly sealed and always inherits
from Object. (This answers your question)

A class can be cast to itself and to any superclass of it's type. A struct
can only be boxed into Object and unboxed to it's sealed type.

A class supports polymorhpism. Structs have limited support for polymorfism
as you can override or reindtroduce methods on the Object class. However,
the abstract or virtual keyword makes non sense on value types and are so
forbidden in C#.

A class instance is garbage collected. A struct instance on stack is simply
removed when a method ends or are implictly collected from heap when when an
class instance is garbage collected.

A class is a beautiful thing. structs sucks! =)

Hope this helps

- Michael S

Nov 16 '05 #5
"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> a écrit dans
le message de news: #r************* *@TK2MSFTNGP14. phx.gbl...
You forgot one - and this is the reason for their existance - With no

boxing, structs are faster and smaller which is why all the primatives are
valuetypes and things like System.Drawing. Point

So, if I design my own value types that include events, etc and have
implicit conversion operators to simplify code e.g. IntegerType, StringType,
FloatType, etc; Am I better using structs or classes ?

If I rely on implicit operators, does remove some of the requirement for
boxing ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 16 '05 #6

"Joanna Carter (TeamB)" <jo*****@nospam forme.com> wrote in message
news:uN******** ******@TK2MSFTN GP09.phx.gbl...
"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> a écrit
dans
le message de news: #r************* *@TK2MSFTNGP14. phx.gbl...
You forgot one - and this is the reason for their existance - With no

boxing, structs are faster and smaller which is why all the primatives are
valuetypes and things like System.Drawing. Point


Indeed Richard and Joanna.

I added that last tidbit that structs sucks just for letting someone make
this point (about Points *S*).
However, I think there is seldom reason for defining your own value types.
Every time I start by thinking of a type as a struct I usually wind up
making it a class. I typically ask my self, - Can I assure that an instance
of this type won't be passed to a method as an Object. Most structs seldom
pass that test.

I think structs will be more used in .NET 2.0 as generic types as List<T>
and such will cope with the boxing problem. When this happens I think people
will use structs for business entities and data entities, and not interfaces
as Java typically does things.

I'd rather see a Customer as a struct and not an class or interface. The
struct says - "I am a customer and I do this and that and I am written in
stone so you can always expect the same behavour from me!" I think this
makes more sense than having an class go: - "I am perhaps a customer or
something more special than that and I may do what you expect if not
something completly overriden." Interfaces though are worst in my view: - "I
am sorta like a customer and I behave in a customerlike way. But exactly
what I do is hidden from you and may change at anytime depending on the
factory that (perhaps) loaded me dynamically. You'll never know!"

In of .NET 1.1, I don't use structs for the above purposes as it's
performance problem in regards to boxing is quite costly.

As I have always admired your articles and posts in the delphi OO group,
Johanna; I hope you comment on the above.

- Michael S

Nov 16 '05 #7
The boxing will only occur if you pass the value to a method that takes System.Object, if you take an interface based reference or you call one of the System.Object virtual methods that you haven't overridden.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> a ?crit dans
le message de news: #r************* *@TK2MSFTNGP14. phx.gbl...
You forgot one - and this is the reason for their existance - With no

boxing, structs are faster and smaller which is why all the primatives are
valuetypes and things like System.Drawing. Point

So, if I design my own value types that include events, etc and have
implicit conversion operators to simplify code e.g. IntegerType, StringType,
FloatType, etc; Am I better using structs or classes ?

If I rely on implicit operators, does remove some of the requirement for
boxing ?

Joanna

Nov 16 '05 #8

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
You forgot one - and this is the reason for their existance - With no boxing, structs are faster and smaller which is why all the primatives are
valuetypes and things like System.Drawing. Point
Regards

Richard Blewett - DevelopMentor


So why make DateTime a struct? Its not what I'd consider a primitive.
Nov 16 '05 #9

"Richard Blewett [DevelopMentor]" <ri******@NOSPA Mdevelop.com> wrote in
message news:Oi******** ******@TK2MSFTN GP12.phx.gbl...
The boxing will only occur if you pass the value to a method that takes
System.Object, if you take an interface based reference or you call one of
the System.Object virtual methods that you haven't overridden.

Regards


That 'only' is not as harmless as it seems. Heavily used classes as
ArrayList and HashTable is really hurt by boxing value types. When those
general classes becomes generic classes in .NET 2.0 this problem will be no
more.

In .NET 1.1 I use structs on large classes with many members to group
members in a ordely fashion. So for me structs are just for readability,
while it may become quite useful in NET 2.0.

- Michael S
Nov 16 '05 #10

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

Similar topics

3
2975
by: Grant Edwards | last post by:
Is it true that a datetime object can convert itself into a string, but not the other way around? IOW, there's no simple way to take the output from str(d) and turn it back into d? So, I tried to create a class that knows how to do that, but I don't seem to be able to subclass datetime.datetime: import datetime class MyDatetime(datetime.datetime):
1
3148
by: Julia | last post by:
Hi, Is it possible to inherit from a control and use it's desginer? I am trying to inherit from TabControl but than i dont have the default designer so i cannot put pages at design time thanks in advance.
2
1630
by: ad | last post by:
I use a textbox for user to enter a date. How can I determine if the string can convert to datetime.
4
428
by: David | last post by:
I have trying to have a webform inherit controls from another form and can't get it to work Say I have a form that saves the person's demographic info. ****one.aspx**** //I have an object to save the person's name in code behind protected void SavePersonInfo(Person p)
3
1095
by: ad | last post by:
We can inherited a class. but when a class has a .aspx ahead, can we inherit the class and it's .aspx together?
2
1349
by: Randall Parker | last post by:
I've noticed that C# can only inherit from a single class. At the same time, the CodeBehind in an aspx.cs file inherits from Page. So what do you all do to reuse code in .aspx.cs classes? Seems to me one has to create classes and make them members of classes in the aspx.cs files. Is that what you all do? (at least those of you who try to get re-use)
5
1639
by: Jim Hubbard | last post by:
I have created a simple usercontrol that adds functionality to the webbrowser control (let's call it ctrl1). I would like to add it to another usercontrol I am creating (let's call it ctrl2), but I can't INHERIT ctrl1 into ctrl2 because I can only use a single INHERITS statement in a class and doing so would eliminate the "Inherits System.Windows.Forms.UserControl" statement needed tom implement ctrl2. Any suggestions?
3
8927
by: J | last post by:
I tried to inherit 'Shot' class from 'Image' class, only to fail. It gives me the CS0122 error, which says that it can't access 'System.Drawing.Image.Image()'. What am I missing? using System;
10
12056
by: Polaris431 | last post by:
I get a casting error when I execute the following: DateTime d = DateTime.Now; Double f = Convert.ToDouble(d); Why? The Convert.ToDouble has many overloads, one of them being the ability to convert a datetime value to a double. Thanks Johann
0
8203
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4094
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1515
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.