DateTime

  • Thread starter Thread starter A.M
  • Start date Start date
A

A.M

Hi,

I know we can assigne year, month and day to a DateTime value by the
constructor like

Dim dt As DateTime = New DateTime(2003,8,1)

How can i assign value by year, month and day? I am looking for a function
like this:

st = SetDateTime(2002,7,2)

Is there any way to that without creating a new DateTime object ?

Thanks,
Ali
 
A.M said:
I know we can assigne year, month and day to a DateTime value by
the constructor like

Dim dt As DateTime = New DateTime(2003,8,1)

How can i assign value by year, month and day? I am looking for a
function like this:

st = SetDateTime(2002,7,2)

Is there any way to that without creating a new DateTime object ?

You mean you want to _change_ the date/time of an existing DateTime object?
Unfortunately, the answer is: no, it is not possible. If you are concerned
about the performance, it is no a big deal, because the only field of a
datetime object is/are the Ticks (int64).
 
Hi A.M,

I'm not quite sure to your question. You need to set a DateTime value by
this function, but what's the type of the return value? Did you mean
st.SetDateTime(2002,7,2)?

Could you please provide slightly more information about what you are going
to do, it would be much easier to help you find ways to achieve that.

If anything is unclear, please feel free to reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hello,

A.M said:
I know we can assigne year, month and day to a DateTime
value by the constructor like

Dim dt As DateTime = New DateTime(2003,8,1)

How can i assign value by year, month and day? I am
looking for a function like this:

st = SetDateTime(2002,7,2)

Is there any way to that without creating a new DateTime
object ?

What's the porblem with creating a new object?
 
There is no problem to create a new DateTime object and leave it to GC
Why such a perfect class suffers from lack of method like
st.SetTime(year,month,day) ?
 
Oops, My mistake!

Thanks,
Ali


Jay B. Harlow said:
A.M,
I hope you realize that DateTime is an immutable Structure, not a Class!

Being a Structure means it is a value type and has value semantics, being
immutable means it has full value semantics.

Which means to change a value on a DateTime variable you need to create a
new DateTime 'value'. Being a Structure means that this DateTime 'value'
exists on the stack, not the heap. Hence the GC does not collection DateTime
objects.

So the following statement:
Does not create an object on the heap that the GC needs to collection later.

This is (partially) explained at:
http://msdn.microsoft.com/library/d...genref/html/cpconvaluetypeusageguidelines.asp

(following the links on that page for more details)

Of course if a Class has an instance DateTime field, that DateTime 'value'
exists on the heap as an integral part of that object. Or if a DateTime
'value' is passed to an Object parameter, the 'value' will be boxed, at that
point it is effectively an object.

Hope this helps
Jay
 
Back
Top