none
Cannot set a var to a string, always null

    Question

  • We have a very weird issue, where by a string variable can be initialised as an empty string then set to another string value.  However, try as we might this string variable will not accept another string value. This is our code

    var shippedTo = "";

    if (!string.IsNullOrWhiteSpace(request.ShippedTo))
    {   // has an order
        if (!string.IsNullOrEmpty(_order?))
        {
            shippedTo = _order.ClientCode;
        }
    }

    We setup the variable, no problem & inspect it shows as an empty string:

    var shippedTo = "";

    The order is inspected is there, with the clientCode which is set to "AAA123".

    However, when we do then shippedTo is always null, never "AAA123".

    shippedTo = _order.clientCode;

    We cannot figure out why shippedTo is always null & never set to the clientCode. 

    The _order object is an object received from a REST call as mapped out via a data contract.

    Anybody come across this kind of issue before?

    Friday, March 31, 2017 9:06 AM

All replies

  • Could you post the relevant REST json object and your data contract in c#?
    Friday, March 31, 2017 9:28 AM
  • Hi,

    I'm a bit confused by your code = IsNullOrEmpty should be _order?.ClientCode I would guess. If this is your code, then the if statement would not evaluate properly (again I'm guessing) which means the code to assign would never be called.

    Regards,

    D

    • Proposed as answer by bowman_d Friday, March 31, 2017 11:38 AM
    Friday, March 31, 2017 11:38 AM
  • Yes that is correct I pasted the wrong code it should read:

        if (!string.IsNullOrEmpty(_order?ClientCode))
        {

    shippedTo = _order.ClientCode;
        }

    Where a ClientCode exists i.e. "AAA123", the shippedTo should retain "AAA123", but it does not goes to null.

    Jay

    Friday, March 31, 2017 3:50 PM
  • Hi,

    Unfortunately you'll have to post your code before anyone can help further - we'd need to see the object you're deserializing to, a sample message and the full code for this handler.

    Regards,

    D

    Saturday, April 1, 2017 6:33 AM