Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7802165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:10:46+00:00 2026-06-02T01:10:46+00:00

I have this code : private MyClass CreateObject(MyOtherClass myOtherClass) { return new MyClass {

  • 0

I have this code :

private MyClass CreateObject(MyOtherClass myOtherClass)
{
    return new MyClass
    {
        Name = myOtherClass.Name,
        ValidationDate = (DateTime)myOtherClass.ValidationDate  //ValidationDate is nullable
    };
}

If I do this, I have a compilation error :

Type of conditional expression cannot be determined because there is
no implicit conversion between '<null>' and 'System.DateTime'

private MyClass CreateObject(MyOtherClass myOtherClass)
{
    return new MyClass
    {
        Name = myOtherClass.Name,
        ValidationDate = (myOtherClass.VALIDATION_DATE == null) ? null : DateTime.Now  //ValidationDate is nullable
    };
}

If I do this, no problem :

MyClass myClass = new MyClass();
if (myClass.ValidationDate == null)
    myClass.ValidationDate = null;

The question is why ? and Solustion ? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:10:48+00:00Added an answer on June 2, 2026 at 1:10 am

    The problem in the first code block is that you’re trying to cast a nullable DateTime value to DateTime.

    Try casting it to nullable DateTime (DateTime?) instead:

    ValidationDate = (DateTime?)myOtherClass.ValidationDate
    

    because you’re trying to populate a nullable DateTime field after all.

    If you’re trying to provide a default value in case myOtherClass.ValidationDate is null, then use the coalescing operator (no casts needed):

    ValidationDate = myOtherClass.ValidationDate ?? DateTime.Now
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this snippet of code private Templates retrieveFromCache(String name) { TemplatesWrapper t =
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
I have the following code : public class MyClass { private readonly string name;
I have this code private void writeReport(IReport report, string reportName) { string reportString =
I have this piece of code: private void myFunc(){ obj = doSomething(); //If value
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
I have simplified my code to this internal class Program { private static void
I have a private project, and i want it hosted on google code. this
I have a code segment: public class MyClass { private string _myProperty; public string
I have read a parent Class called MyClass code like this, public class MyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.