locked
check if a generic property is null using equals method RRS feed

  • Question

  • User254389092 posted

    In my code, 

    public static IHtmlString RecipientSelectorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string buttonText, bool isReadOnly, string onClickFunctionName)
    {

    ...

    output.AppendFormat(@"<div class='input-group '>
    <input id='{0}' name='{0}' type='text' class='input-sm form-control{7}' value='{6}' title='{6}' />
    <div class='input-group-btn'>
    <button type='button' class='btn btn-default dropdown-toggle' {5} title='{1}' onclick='{3}(""{2}"",""{4}"")'>{1}
    <span class='caret'></span>
    </button>
    </div>
    </div>",
    name,
    buttonTitle,
    recipientSelectorTitle,
    buttonFunction,
    urlHelper.Action("_RecipientSelector", "Recipient", new { Id = name, isPrivate = false }),
    isReadOnly ? "disabled='disabled'" : "",
    property == null ? string.Empty : property.ToString(),htmlHelper.ViewData.ModelState[name] != null && htmlHelper.ViewData.ModelState[name].Errors.Any() ?
    " input-validation-error" : string.Empty);

    }

    My code factor tool suggests me to "Use a comparison to 'default(TProperty)' instead or add a constraint to 'TProperty' so that it can't be a value type." I have tried:

    output.AppendFormat(@"<div class='input-group '>
    <input id='{0}' name='{0}' type='text' class='input-sm form-control{7}' value='{6}' title='{6}' />
    <div class='input-group-btn'>
    <button type='button' class='btn btn-default dropdown-toggle' {5} title='{1}' onclick='{3}(""{2}"",""{4}"")'>{1}
    <span class='caret'></span>
    </button>
    </div>
    </div>",
    name,
    buttonTitle,
    recipientSelectorTitle,
    buttonFunction,
    urlHelper.Action("_RecipientSelector", "Recipient", new { Id = name, isPrivate = false }),
    isReadOnly ? "disabled='disabled'" : "",
    property.Equals(null, default(TProperty)) ? string.Empty : property.ToString(),
    htmlHelper.ViewData.ModelState[name] != null && htmlHelper.ViewData.ModelState[name].Errors.Any() ?
    " input-validation-error" : string.Empty);

    This code returns compile time error:

    member object.Equals(object, object) can not be accessed with an instance reference; Qualify it with a type name instead. 

    How can I check this expression if it has a null value inside that element? Please help me with a fix

    Luc

     

    Tuesday, September 24, 2019 1:23 PM

Answers

  • User-719153870 posted

    Hi Luc,

    Thank you so much, to be frank, i was not familiar with MVC html helper with Lambda expression.

    But after several hours learning about this and also with the help of your code, i finally understand how this thing goes.

    As for your question

    I need to convert below line of code:

    property == null ? string.Empty : property.ToString()

    to use Equals to compare. Can it be done?

    The answer is NO, you can't use object.equals() method to a null object.

    For the method, it needs to work like xxx.Equals(null) instead of null.Equals(null).

    When the propert is null, it will generate an error: Object reference not set to an instance of an object.

    Lastly, if the property can be null in cases, you should use the property == null ? string.Empty : property.ToString().

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 27, 2019 8:05 AM

All replies

  • User303363814 posted

    property.Equals( default(TProperty))

    But it would probably better to tell the compiler about any extra information you have about TProperty with a constraint.  What is the range of types that can be passed?

    Wednesday, September 25, 2019 12:28 AM
  • User254389092 posted

    Hi PaulTheSmith,

    I need to convert below line of code:

    property == null ? string.Empty : property.ToString()

    to use Equals to compare. Can it be done?

    Luc

    Wednesday, September 25, 2019 6:45 AM
  • User-719153870 posted

    Hi Lucifer_deep,

    property.Equals(null, default(TProperty)) ? string.Empty : property.ToString()

    What is this property? Is this an instance of Generics or a common object like string?

    If you are trying to use the Equals(Object) method, you will only need to set the Object to null, like below:

    string property = "abc";
    Response.Write(property.Equals(null) ? string.Empty : property.ToString());

    As for the error member object.Equals(object, object) can not be accessed with an instance reference; Qualify it with a type name instead, you want to check this thread.

    Best Regard,

    Yang Shen

    Wednesday, September 25, 2019 9:17 AM
  • User254389092 posted

    I think I need to use the code from your link. But I did not quite understand the code. Can you give me a code that works in my case?

    Unfortunately, I did not get a proper understanding of the issue.

    Wednesday, September 25, 2019 12:34 PM
  • User303363814 posted

    Remove 

    property == null

    and replace with

    property.Equals(default(TProperty))

    Or, equivalently, delete 

    null,

    from the code you showed which had a compilation error

    Wednesday, September 25, 2019 10:13 PM
  • User254389092 posted

    Thanks PaulTheSmith,

    I tried that but unfortunately, this seems to be another code smell than a valid solution. I am still working on it. But right now, I do not have a valid solution to it. Is there another way to achieve this?

    Luc

    Thursday, September 26, 2019 6:12 AM
  • User-719153870 posted

    Hi Luc,

    Can you provide the definition or declaration of THE 'property' in your code? Because it's nowhere to be find what is the 'property'.

    This will be very helpful. Thanks.

    Best Regard,

    Yang Shen

    Thursday, September 26, 2019 7:13 AM
  • User303363814 posted

    So now you have two very, very slightly different pieces of code which work.  What problem are you trying to solve?  It is not clear.

    Thursday, September 26, 2019 7:23 AM
  • User254389092 posted

    Hi Yang shen, Trying to check not equals for this piece of code. Warning says: "change condition sothat it does not always evaluate to True"

    // Invoke model property via expression
    var property = expression.Compile().Invoke(model);
    var propertyValue = !property.Equals(null) && property.ToString() != "0" ? property.ToString() : string.Empty;

    the second part where I try to implement is:

    // Invoke model property via expression
    TProperty property = expression.Compile().Invoke(model);

    output.AppendFormat(@"<div class='input-group '>
    <input id='{0}' name='{0}' type='text' class='input-sm form-control{7}' value='{6}' title='{6}' />
    <div class='input-group-btn'>
    <button type='button' class='btn btn-default dropdown-toggle' {5} title='{1}' onclick='{3}(""{2}"",""{4}"")'>{1}
    <span class='caret'></span>
    </button>
    </div>
    </div>",
    name,
    buttonTitle,
    recipientSelectorTitle,
    buttonFunction,
    urlHelper.Action("_RecipientSelector", "Recipient", new { Id = name, isPrivate = false }),
    isReadOnly ? "disabled='disabled'" : "",
    property.Equals(null) ? string.Empty : property.ToString(),
    htmlHelper.ViewData.ModelState[name] != null && htmlHelper.ViewData.ModelState[name].Errors.Any() ?
    " input-validation-error" : string.Empty);

    I hope I gave you the right code. Please look into this 

    Thursday, September 26, 2019 7:39 AM
  • User-719153870 posted

    Hi Lucifer_deep,

    I think we have made great progress.

    But i will need also the definition of your model in expression.Compile().Invoke(model);.

    Thanks.

    Best Regard,

    Yang Shen

    Thursday, September 26, 2019 10:08 AM
  • User254389092 posted

    Hi Yang Shen,

    Please find the below code for model definition:

    // Grab model from view
    var model = (TModel)htmlHelper.ViewContext.ViewData.ModelMetadata.Model;

    same model declaration was used to implement both the lines of code.

    Luc

    Thursday, September 26, 2019 10:17 AM
  • User-719153870 posted

    Hi Luc,

    Thank you so much, to be frank, i was not familiar with MVC html helper with Lambda expression.

    But after several hours learning about this and also with the help of your code, i finally understand how this thing goes.

    As for your question

    I need to convert below line of code:

    property == null ? string.Empty : property.ToString()

    to use Equals to compare. Can it be done?

    The answer is NO, you can't use object.equals() method to a null object.

    For the method, it needs to work like xxx.Equals(null) instead of null.Equals(null).

    When the propert is null, it will generate an error: Object reference not set to an instance of an object.

    Lastly, if the property can be null in cases, you should use the property == null ? string.Empty : property.ToString().

    Best Regard,

    Yang Shen

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 27, 2019 8:05 AM