0

So the beginning of my controller action is

 [HttpGet] public ActionResult FillOut ( Guid pid, int? sid ) { // pid: partner id // sid (optional): survey id // if survey id not supplied in query string, find which survey the user should be on if ( sid == null ) { sid = this._Db.CheckIfFinished(pid, 1) ? 2 : 1; } ViewBag.pid = pid; ViewBag.sid = sid; ViewBag.finished = this._Db.CheckIfFinished(pid,sid); ViewBag.survtitle = this._Db.GetSurveyTitle(sid); var AllAnswers = this._Db.GetAnswersByPartner(pid,sid); 

and VS isn't happy because my methods this._Db.CheckIfFinished(pid,sid);, this._Db.GetSurveyTitle(sid); and this._Db.GetAnswersByPartner(pid,sid) expect the sid parameter to be an int and not an int?. But with how I'm using it, sid is guaranteed to be non-null. So is this a Visual Studio bug or is my C# technically invalid, and if it's invalid, what's the cleanest way of overcoming this issue in my particular scenario?

6 Answers 6

4

It is not a bug (besides it's the compiler that gets the final say).

You can either stick .Value everywhere you need an actual int, or since you need it so many times, create another non-nullable int variable to store the value.

Sign up to request clarification or add additional context in comments.

Comments

3

For nullable types, use .Value to get the underlying value. So in this case, sid.Value will return the int you want.

You can also check that the variable has a value by the HasValue property.

Comments

0

So when you've checked that sid != null (or sid.HasValue), it's safe to use sid.Value to get the non-nullable int value.

Comments

0

If you are sure then use parameter?.value instead

[HttpGet] public ActionResult FillOut ( Guid pid, int? sid ) { // pid: partner id // sid (optional): survey id // if survey id not supplied in query string, find which survey the user should be on if ( !sid.HasValue ) { sid = this._Db.CheckIfFinished(pid, 1) ? 2 : 1; } ViewBag.pid = pid; ViewBag.sid = sid; ViewBag.finished = this._Db.CheckIfFinished(pid,sid); ViewBag.survtitle = this._Db.GetSurveyTitle(sid); var AllAnswers = this._Db.GetAnswersByPartner(pid,sid.Value); 

Comments

0

But with how I'm using it, sid is guaranteed to be non-null.

It's a nullable int that isn't null, but it's still a nullable int. It's very purpose is to possibly be null.

You can obtain the int value by using it's Value property.

If you are absolutely certain that it isn't null, then as an optimisation you can call its GetValueOrDefault() method (the overload that doesn't take a value to use as the default). Because this method doesn't test whether the value is set to null or not it is faster. But only use this optimisation if you are 100% sure it can't be null (e.g. just after checking exactly that, or setting it to a non-null value) because if it is null it will just return 0 rather than throwing an exception that would point out your error.

Comments

0

Just use this, to get the actual int-value that is stored inside the int?-container:

ViewBag.pid = pid; ViewBag.sid = sid.Value; ViewBag.finished = this._Db.CheckIfFinished(pid, sid.Value); ViewBag.survtitle = this._Db.GetSurveyTitle(sid.Value); var AllAnswers = this._Db.GetAnswersByPartner(pid, sid.Value); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.