0

Trying to set a value for a create user soap call and when setting a startdate for a new user:

DateTime? dt = DateTime.Now;
emptyUsr.StartDate = dt;

It returns an error of "Cannot implicitly convert type 'System.DateTime' to 'LearnScan.LearnUser.NullableDateTime'" I was under the impression that DateTime? sets to nullable?

The StartDate property has type LearnScan.LearnUser.NullableDateTime which is defined as:

public partial class NullableDateTime : object, System.ComponentModel.INotifyPropertyChanged {
    internal static object DateTime;
    private bool isNullField;

    private System.DateTime valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public bool IsNull {
        get {
            return this.isNullField;
        }
        set {
            this.isNullField = value;
            this.RaisePropertyChanged("IsNull");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Order=1)]
    public System.DateTime Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
            this.RaisePropertyChanged("Value");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

*Solution - It wanted values for each emptyUsr.StartDate.IsNull = false and emptyUsr.StartDate.Value= DateTime.Now;

17
  • 3
    Read the error carefully. It talks about LearnScan.LearnUser.NullableDateTime, not Nullable<DateTime>. The error will point to the offending line and I'd bet it's emptyUsr.StartDate = dt;. What is the type of StartDate ? Commented May 10, 2019 at 15:02
  • Yes sorry I'm new. How do I cast to NullableDateTime? Commented May 10, 2019 at 15:04
  • There's no such thing. The error complains that you tried to set a DateTime value to a property using your custom type. Commented May 10, 2019 at 15:05
  • 1
    This question can't be answered until you add the definition for LearnScan.LearnUser.NullableDateTime (or you change the type of the StartDate property to DateTime?). Commented May 10, 2019 at 15:06
  • 1
    Please add the full definition of LearnScan.LearnUser.NullableDateTime to the question, no-one can tell you how to construct one from a DateTime? otherwise. Commented May 10, 2019 at 15:40

2 Answers 2

2

You can create an implicit conversion from DateTime? to your NullableDateTime class:

namespace LearnScan.LearnUser {
    public partial class NullableDateTime
    {
        public static implicit operator NullableDateTime(DateTime? dt)
        {
            if(dt.HasValue)
            {
                return new NullableDateTime { IsNull = false, Value = dt.Value };
            }
            else
            {
                 return new NullableDateTime { IsNull = true };
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that before and It didnt work. Anyway I fixed it. It was looking for emptyUsr.StartDate.Value = DateTime.Now; emptyUsr.StartDate.IsNull = false;
@dustin - You tried what before? How did it not work? Your fix is basically what this answer suggests.
1

The NullableDateTime externally-defined type (since that is not part of the regular C# set of libraries and APIs) is not the same as DateTime?. Although its short name might trick you into thinking that, its full name LearnScan.LearnUser.NullableDateTime tells you that it is very different from System.DateTime? (which is the actual full name of DateTime? in .NET). You need to understand how NullableDateTime was implemented and how to use it. It might be a different approach to a nullable DateTime type built upon the DateTime struct .NET offers, as of the short snippet you provided in the comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.