I'm trying to make my particles fade in my application using the alpha argument of the Color.FromArgb method but my math won't work because I have a nullable DateTime variable and the error it gives me is 'System.Nullable<System.TimeSpan>' does not contain a definition for 'TotalMilliseconds' and no extension method 'TotalMilliseconds' accepting a first argument of type 'System.Nullable<System.TimeSpan>' could be found (are you missing a using directive or an assembly reference?). Below is the code line that is throwing this error, can anyone help me get around this?

// Declaration of the variable
private DateTime? ParticleBirth;

// Error Line
double TotalLife = (DateTime.Now - ParticleBirth).TotalMilliseconds;

Thanks,
Jamie

Since the DateTime is nullable, you should check if it is null before your TotalLife calculation, then cast it into a DateTime.

Why would the birth ever be nullable? Once the object is initialized, is this not its birth time?

I solved the problem:

// Declaration of the variable
private DateTime? ParticleBirth;

// Error Line
double TotalLife = (DateTime.Now - ParticleBirth.Value).TotalMilliseconds;

The point of a nullable time is so that I can achieve handling for if it's null, then set it to the birth time. Because the particles are not born at runtime, they are born on an event that can be triggered multiple times in a row. Thanks for the help anyways.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.