-1

I have a object that has a boolean field called NameIndicator (External contract one). In my code, I made the my boolean "IsIndicated" as nullable.

How do I check for null and assign value only if non null?

I currently get compile time error with the below code as obvious its assining nullable to non nullable field

 personDetails.Name= new Name_Format()
                    {
                        NameSpecified = true,
                        NameIndicator = contract.IsIndicated
                    };
Jasmine
  • 5,186
  • 16
  • 62
  • 114
  • 1
    What do you mean? If it's non-nullable then it will *always* be "non null". – David Jun 25 '18 at 22:22
  • @David: Easy, Assign value only if its not null. Coz, NameIndicator is not nullable boolean and it gives compile time error of conversion from non nullable to nullable assigning values – Jasmine Jun 25 '18 at 22:24
  • 1
    If it's "easy" then what's the problem? So far you've described two boolean fields as "non-nullable" and you're asking how to set one to another if there's a value. Well, non-nullable means there's *always* a value. So just assign it exactly like you already are. What's the problem? What error are you talking about? Clarify the question. – David Jun 25 '18 at 22:26
  • @David: Mate, again, where did I mention two boolean field as non nullable? I meant one is nullable which is on right hand side. The one in left side is not :( That is what creating compile time error – Jasmine Jun 25 '18 at 22:28
  • 2
    Here: *"In my code, I made the my boolean "IsIndicated" as non nullable."* And here: *"NameIndicator is not nullable boolean"* Have you tried reading your own question? – David Jun 25 '18 at 22:29
  • 1
    @David: Apologize, my bad, my boolean in right hand side is nullable...sorry typo in initial writing, I will update question – Jasmine Jun 25 '18 at 22:29
  • The way you do something conditionally in C# is with the `if` statement. So you could for example say `if (something != null) somethingElse = something.Value;` Is that what you're asking? – Eric Lippert Jun 25 '18 at 22:29
  • 2
    Is there a value you would like to assign if the nullable value is null? In that case, you use the `??` operator -- the **null coalescing operator**. – Eric Lippert Jun 25 '18 at 22:30
  • 2
    @David: Though that is perfectly legal, the more idiomatic way to write that would be `NameIndicator = contract.IsIndicated ?? false` – Eric Lippert Jun 25 '18 at 22:31
  • 1
    @EricLippert: Thank you for your time in writing and helping me again :) I remember your writings for my questions years ago and great explanations. Yes, you are right, exactly I am looking for something like you mentioned now...something like NameIndicator = ((person.IsIndicated == null) ? false : person.IsIndicated)....its still throws compile time error :( – Jasmine Jun 25 '18 at 22:31
  • @EricLippert: Your answer helped me get rid of compile time problem :) Thanks a lot, that is what exactly I was looking for :) :) Thank you so much again. – Jasmine Jun 25 '18 at 22:35
  • `NameIndicator = contract.IsIndicated.GetValueOrDefault()` or `NameIndicator = contract.IsIndicated.HasValue ? contract.IsIndicated.Value : NameIndicator;` might work – Rufus L Jun 25 '18 at 22:35

1 Answers1

2

If you want to assign a particular value in the case of null, and the value otherwise, you use the null coalescing operator.

personDetails.Name= new Name_Format()
{
  NameSpecified = true,
  NameIndicator = contract.IsIndicated ?? true
};

That has the same semantics as

personDetails.Name = new Name_Format()
{
  NameSpecified = true,
  NameIndicator = contract.IsIndicated == null ? true : contract.IsIndicated.Value
};

except that of course it only calls IsIndicated once.

If you want the runtime to choose a default value for you then you can do

personDetails.Name = new Name_Format()
{
  NameSpecified = true,
  NameIndicator = contract.IsIndicated.GetValueOrDefault()
};

In this case it will choose "false", since that is the default value for Booleans.

If you want nothing at all to happen if the value is null then you can use an if statement:

if (contract.IsIndicated != null)
{
  personDetails.Name = new Name_Format()
  {
    NameSpecified = true,
    NameIndicator = contract.IsIndicated.Value
  }
};
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • Thank you Eric. One quick question on the last block, so if its null then still the NameIndicator value would be assumed as false? Coz that block won't execute at all if its null – Jasmine Jun 25 '18 at 22:39
  • @Learner: In the final case `Name` is not assigned at all if the value is null, correct. But you still have to use `Value` to get the non-nullable value out to make the compiler happy. C# does not have *flow typing*, which is what it would need to have to determine that `IsIndicated` is non-null inside the body of the `if`. – Eric Lippert Jun 25 '18 at 22:41
  • `Nullable` structs also have a `HasValue` property that you can check, which reads more fluently (at least to me). – Rufus L Jun 25 '18 at 22:42
  • 1
    @EricLippert: Thank you again, sure. :) Thank you for helpful insights, gained a lot from here... :) – Jasmine Jun 25 '18 at 22:44