I've got objects like:
DateTime a;
DateTime b;
DateTime c;
If I make:
b=a;
and then:
b=c;
my a equals c.
I understand that this are dynamic objects and I've binded them with reference. But I don't want to bind them. How can I avoid this? How can I make:b=c;without making a=c; in the same time?
DateTime is a struct which means it's a value type.
See the following example:
void Main()
{
DateTime a = new DateTime(2005, 05, 05);
DateTime b = a;
Console.WriteLine (a);
Console.WriteLine (b);
a = new DateTime(2012, 05, 05);
Console.WriteLine (a);
Console.WriteLine (b);
}
output:
5/05/2005 0:00:00
5/05/2005 0:00:00
5/05/2012 0:00:00
5/05/2005 0:00:00
Usually this can be done (in a different situation) by implementing ICloneable which defines the Clone method.
Related
The below code works fine :
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(-1);
int d3 = (int)(d1 - d2).TotalDays;
But what if I define DateTime as DateTime? :
DateTime? d1 = DateTime.Now;
DateTime? d2 = DateTime.Now.AddDays(-1);
int d3 = (int)(d1 - d2).TotalDays;
underlined red with error
Cannot implicitly convert 'System.TimeSpan?' to 'System.TimeSpan'
Is it possible to get the difference of number of days between two datetimes that are defined as nullable?
Well yes, but you need to use the Value property to "un-null" it:
int d3 = (int)(d1 - d2).Value.TotalDays;
However, you should consider the possibility that either d1 or d2 is null - which won't happen in your case, but could in other cases. You may want:
int? d3 = (int?) (d1 - d2)?.TotalDays;
That will give a result of null if either d1 or d2 is null. This is assuming you're using C# 6, of course - otherwise the ?. operator isn't available.
(You could use GetValueOrDefault() in the first case as suggested by user3185569, but that would silently use an empty TimeSpan if either value is null, which feels unlikely to be what you want.)
Yes, using GetValueOrDefault():
DateTime? d1 = DateTime.Now;
DateTime? d2 = DateTime.Now.AddDays(-1);
int d3 = (int)(d1 - d2).GetValueOrDefault().TotalDays;
d1 - d2 return Nullable TimeSpan which doesn't directly contains a property called TotalDays. But using GetValueOrDefault() you can return a TimeSpan object and get 0 Total Days if the value was NULL
If you do really expect Null Values, it is better to differentiate between 0 days (What the above approach returns) and and invalid operation (date - null), (null - date) and (null - null). Then you might need to use another approach:
int? d3 = (int) (d1 - d2)?.TotalDays;
Or if you're using a version prior to C# 6 :
int? d3 = d1.HasValue && d2.HasValue ? (int)(d1 - d2).Value.TotalDays : new int?();
use long and convert
long x
System.TimeSpan y= new TimeSpan(x);
COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum;
datum.FromOADate(d);
I am trying to get date and time from a DateTimePicker control and to later on set the value of datum to that value. Datum is System::DateTime (C#). But datum is this "1/1/1 00:00:00" What's the problem?
The problem is the very last line:
datum.FromOADate(d);
DateTime::FromOADate is actually a static member function that returns a DateTime object. In C++ terms, you can think of it like a named constructor.
It does not initialize datum like a normal member function would. What's confusing you is the fact that C++ allows you to call static members using an instance of the object. In C#, that would not be possible, and you would have gotten a compile-time error alerting you to the problem.
Write the code like this, and you will be fine:
COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum = System::DateTime::FromOADate(d);
You could also do the following (but it would be similarly confusing):
COleDateTime m_dt;
m_ctrlDateTime.GetTime(m_dt);
double d = dt.m_dt;
System::DateTime datum;
datum = datum.FromOADate(d);
I have created a constructor as follows :
public Animal(string regNum, DateTime brought, string name)
{
this.RegNumber = regNum;
this.DateBrought = brought;
this.Name = name;
this.NameNewOwner = null;
}
And based on the constructor above, I created an object called pet, as follows:
Animal pet = new Animal("a12344", Convert.ToDateTime(23/01/2013), "Fluffy");
However, when I run my program it gives me error saying: Invalid cast from Int32 to DateTime Can anyone help me with this?
Constructor is not a problem. Convert.ToDateTime call is:
Convert.ToDateTime(23 / 01 / 2013);
It's equivalent to Convert.ToDateTime(0) (because 23/1/2013 as integer division returns 0), which is not possible.
Use new DateTime(2013, 1, 23) instead.
Animal pet = new Animal("a12344", new DateTime(2013, 1, 23), "Fluffy");
You could also use Convert.ToDateTime("23/01/2013"), which would be equivalent to DateTime.Parse("21/01/2013") parsing, but if you know the date at compile time, you should definitely use DateTime constructor.
Is it possible to create a C# pointer to a DateTime object? I'm trying to do this:
DateTime Event1 = DateTime.Now;
DateTime Event2 = DateTime.Now.AddYears(10);
DateTime EventPointer; // A Pointer?
if (something)
{
EventPointer = Event1;
}
else
{
EventPointer = Event2;
}
EventPointer.DoSomething? // Something that would change the value of Event1/2 variable.
Not directly, but you can wrap it around a class:
Wrapper w1 = new Wrapper { TheDate = DateTime.Now };
Wrapper w2 = new Wrapper { TheDate = DateTime.Now.AddYears(10) };
Wrapper w;
if (something)
{
w = w1;
}
else
{
w = w2;
}
w.DoSomething();
class Wrapper
{
public DateTime TheDate { get; set; }
public void DoSomething()
{
}
}
DateTimes are value types which unlike reference types everytime you assign its value to another variable their value is copied.
A DateTime is a value type, when you assign it the value is copied. However, methods can take a reference to a value type using the ref keyword. Your EventPointer.DoSomething would take a DateTime as reference. Here's a simple example of how you might be able to apply it.
var date = DateTime.Today;
MakeMinValue(ref date);
Console.Out.WriteLine("date = {0}", date);
public void MakeMinValue(ref DateTime dateTime)
{
dateTime = DateTime.MinValue;
}
This will only work with a method parameter.
You can't use pointers in managed code unless you use unsafe. I'd advise against it.
DateTimes are immutable. The only thing you can do to modify a variable of type DateTime is to reassign to it.
You can do something close to what you want, but in general it's not a good idea to do this:
DateTime event1 = new DateTime(2011, 10, 11);
DateTime event2 = new DateTime(2021, 10, 11);
Action<DateTime> eventPointer; // A Pointer?
if (true)
{
eventPointer = x => { event1 = x; };
}
else
{
eventPointer = x => { event2 = x; };
}
eventPointer(new DateTime(2016, 10, 11));
Console.WriteLine(event1.ToString(CultureInfo.InvariantCulture));
Console.WriteLine(event2.ToString(CultureInfo.InvariantCulture));
Result:
10/11/2016 00:00:00
10/11/2021 00:00:00
I think it is a better idea is to find another way to solve your problem.
Pointers, as you are thinking of them, don't exist in managed C# code. You might want to look into out and ref parameters if you need to pass a DateTime and have the original value change. Since DateTimes are structs (value type) they are always copied instead of passed by reference, unless you specifically use an out or ref parameter.
You can replace type of EventPointer, Event1 and Event2 types from DateTime type to DateTime?. It makes EventPointer's type nullable reference type.
DateTime? Event1 = DateTime.Now;
DateTime? Event2 = DateTime.Now.AddYears(10);
DateTime? EventPointer;
if (condition)
{
EventPointer = Event1;
}
else
{
EventPointer = Event2;
}
EventPointer.Value //.DoSomething
Say I have enum as follows (taken from MSDN example):
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};
I can then use is in the code as follows:
int today = (int)Days.Sun;
Question:
Can I evaluate enums? Say I have a variable Day whose value is "Sun".
Will Days.Day evaluate to Days.Sun?
Thanks!
Please don't get hung up on the days example and tell me I should use comparison etc...i am looking to replace the usage of arrays with enums as it is more compact. So what I am looking for is essentially an ability to retrieve values from enum similar to an array:
myArray[Day] where day will evaluate to array key....
Am I right in saying you've got:
string day = "Sun";
and you want to get back the value Days.Sun?
If so, use:
string day = "Sun";
Day day = (Day) Enum.Parse(typeof(Days), day);
Yes, you can do that. You can freely cast between an enum and its underlying type.
using System;
class Program
{
static void Main()
{
// prints "zero"
Console.WriteLine((Foo)0);
// prints "1"
Console.WriteLine((int)Foo.one);
}
}
enum Foo { zero, one, two };
You can also parse the string values as well. Using Enum.Parse
[TestFixture]
public class EnumParsingSO
{
public enum Days
{
Sat = 1,
Sun,
Mon,
Tues
}
[Test]
public void EnumFromString()
{
Days expected = Days.Mon;
int expectedInt = 3;
Days actual = (Days)Enum.Parse(typeof(Days), "Mon");
Assert.AreEqual(expected, actual);
Assert.AreEqual(expectedInt, (int)actual);
}
}
As Andrew says, you can cast from an enum to its underlying type. You can also unbox between the two as well, and there's an implicit conversion from the constant 0 to any enum type:
using System;
enum Demo { Foo, Bar, Baz };
class Test
{
static void Main()
{
int i = 2;
object o = i;
// Unboxing from int to Demo
Demo d = (Demo) o;
Console.WriteLine(d);
o = Demo.Baz;
// Unboxing from Demo to int
i = (int) o;
Console.WriteLine(i);
// Implicit conversion of 0
d = 0;
Console.WriteLine(d);
}
}
for int yes , but for string you should do something like this
Yes you can, you just need to provide the native integer value for each enum value. From your example:
enum Days {
Sat=1,
Sun=2,
Mon=3,
Tue=4,
Wed=5,
Thu=6,
Fri=7
};
(Now, why you would order days of the week in this way is beyond me...)