0

I have an initial snippet which deserealized parameters, checks for value and handles the error:

var param = js.Deserialize<int?>(jqData.Params);


if (param.HasValue)
{
     resp.ReturnValue = param.Value;
}
else
{
     //handle error code
}

Now, during my modification, I have changed the method to accept a list of class parameters instead of nullable integers

var param = js.Deserialize<ClassName>(jqData.Params);

Now it invalidates .HasValue and .Value methods.

My question is: How do I properly modify these two lines so it would hold the same meaning as initial if statement?

Thus far I only thought about switching to if (param != null), but I cannot think of proper equivalent to .Value.

Vadzim Savenok
  • 930
  • 3
  • 14
  • 37
  • If `ClassName` is a class - its value is in the `param`. Being a reference type means that `null` is one of possible values naturally. – zerkms Oct 10 '16 at 22:42
  • @zerkms You mean, the whole if statement becomes unnecessary? – Vadzim Savenok Oct 10 '16 at 22:43
  • Well, `param` holds a reference to the object. If it is `!= null`, then it definitely refers to an object. – zerkms Oct 10 '16 at 22:44
  • @zerkms Wait, then the whole if else statement is unnecessary? – Vadzim Savenok Oct 10 '16 at 22:47
  • You are deserializing to a nullable int in the first statement and to ClassName in the second. ClassName does not have properties (I'm guessing) called HasValue and Value as these are native to the nullable int. –  Oct 10 '16 at 22:47
  • It's not possible to say that, you have not explained what the expected outcome is. – zerkms Oct 10 '16 at 22:47
  • @zerkms After doing some additional research, the whole if-else indeed becomes irrelevant. So thanks for tipping me in the right direction. – Vadzim Savenok Oct 10 '16 at 23:04
  • @zerkms You can get is as an answer, so I will accept it and close question properly. – Vadzim Savenok Oct 10 '16 at 23:04

1 Answers1

0

As soon as ClassName is a class (see - a reference type) you just need to check if it is not null.

If it is not - then the variable holds a reference to an object that you use as-is.

zerkms
  • 249,484
  • 69
  • 436
  • 539