Does nullcheck really work on a statebag?
public long MetaExtraData
get return (long)(ViewState["MetaData"] ?? 0);
set ViewState["MetaData"] = value;
When ViewBag doesn't contains the key 'MetaData' I get back a "can't cast to long" exception. Isn't the whole idea with ?? to handle this?
And of course, the below snippet will also throw a cast error. When checking the values of the variables: o=null
public long MetaExtraDataInt
get
object o = ViewState["MetaData"];
return (long)(o ?? 0);
set ViewState["MetaData"] = value;
I know the problem is easy to solve, but I really do want to know why the ?? don't work on a statebag
c# asp.net
|
show 4 more comments
public long MetaExtraData
get return (long)(ViewState["MetaData"] ?? 0);
set ViewState["MetaData"] = value;
When ViewBag doesn't contains the key 'MetaData' I get back a "can't cast to long" exception. Isn't the whole idea with ?? to handle this?
And of course, the below snippet will also throw a cast error. When checking the values of the variables: o=null
public long MetaExtraDataInt
get
object o = ViewState["MetaData"];
return (long)(o ?? 0);
set ViewState["MetaData"] = value;
I know the problem is easy to solve, but I really do want to know why the ?? don't work on a statebag
c# asp.net
1
No, you could try to useViewState["MetaDataInt"] as int? ?? 0
instead.
– Sebastian Hofmann
Nov 13 '18 at 9:55
1
Is it possible thatViewState[...]
returnsDbNull
instead ofnull
?
– HimBromBeere
Nov 13 '18 at 10:05
1
What is the value ofViewState["MetaDataInt"]
when the exception is thrown? AlsoViewState["MetaDataInt"].GetType()
. You can check both in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:10
1
Please don't guess - check in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:15
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04
|
show 4 more comments
public long MetaExtraData
get return (long)(ViewState["MetaData"] ?? 0);
set ViewState["MetaData"] = value;
When ViewBag doesn't contains the key 'MetaData' I get back a "can't cast to long" exception. Isn't the whole idea with ?? to handle this?
And of course, the below snippet will also throw a cast error. When checking the values of the variables: o=null
public long MetaExtraDataInt
get
object o = ViewState["MetaData"];
return (long)(o ?? 0);
set ViewState["MetaData"] = value;
I know the problem is easy to solve, but I really do want to know why the ?? don't work on a statebag
c# asp.net
public long MetaExtraData
get return (long)(ViewState["MetaData"] ?? 0);
set ViewState["MetaData"] = value;
When ViewBag doesn't contains the key 'MetaData' I get back a "can't cast to long" exception. Isn't the whole idea with ?? to handle this?
And of course, the below snippet will also throw a cast error. When checking the values of the variables: o=null
public long MetaExtraDataInt
get
object o = ViewState["MetaData"];
return (long)(o ?? 0);
set ViewState["MetaData"] = value;
I know the problem is easy to solve, but I really do want to know why the ?? don't work on a statebag
c# asp.net
c# asp.net
edited Nov 13 '18 at 12:52
Doctor Jones
17.4k116391
17.4k116391
asked Nov 13 '18 at 9:52
stebbergstebberg
134
134
1
No, you could try to useViewState["MetaDataInt"] as int? ?? 0
instead.
– Sebastian Hofmann
Nov 13 '18 at 9:55
1
Is it possible thatViewState[...]
returnsDbNull
instead ofnull
?
– HimBromBeere
Nov 13 '18 at 10:05
1
What is the value ofViewState["MetaDataInt"]
when the exception is thrown? AlsoViewState["MetaDataInt"].GetType()
. You can check both in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:10
1
Please don't guess - check in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:15
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04
|
show 4 more comments
1
No, you could try to useViewState["MetaDataInt"] as int? ?? 0
instead.
– Sebastian Hofmann
Nov 13 '18 at 9:55
1
Is it possible thatViewState[...]
returnsDbNull
instead ofnull
?
– HimBromBeere
Nov 13 '18 at 10:05
1
What is the value ofViewState["MetaDataInt"]
when the exception is thrown? AlsoViewState["MetaDataInt"].GetType()
. You can check both in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:10
1
Please don't guess - check in theImmediate Window
.
– mjwills
Nov 13 '18 at 10:15
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04
1
1
No, you could try to use
ViewState["MetaDataInt"] as int? ?? 0
instead.– Sebastian Hofmann
Nov 13 '18 at 9:55
No, you could try to use
ViewState["MetaDataInt"] as int? ?? 0
instead.– Sebastian Hofmann
Nov 13 '18 at 9:55
1
1
Is it possible that
ViewState[...]
returns DbNull
instead of null
?– HimBromBeere
Nov 13 '18 at 10:05
Is it possible that
ViewState[...]
returns DbNull
instead of null
?– HimBromBeere
Nov 13 '18 at 10:05
1
1
What is the value of
ViewState["MetaDataInt"]
when the exception is thrown? Also ViewState["MetaDataInt"].GetType()
. You can check both in the Immediate Window
.– mjwills
Nov 13 '18 at 10:10
What is the value of
ViewState["MetaDataInt"]
when the exception is thrown? Also ViewState["MetaDataInt"].GetType()
. You can check both in the Immediate Window
.– mjwills
Nov 13 '18 at 10:10
1
1
Please don't guess - check in the
Immediate Window
.– mjwills
Nov 13 '18 at 10:15
Please don't guess - check in the
Immediate Window
.– mjwills
Nov 13 '18 at 10:15
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04
|
show 4 more comments
3 Answers
3
active
oldest
votes
If (int)(ViewState["MetaDataInt"] ?? 0)
is throwing an InvalidCastException
, it must be because your value in the statebag isn't an int
.
This is because:
a boxed T can only be unboxed to T (or Nullable.) Once it is
unboxed, it’s just a value that can be cast as usual.
The above quote is taken from Representation and Identity by Eric Lippert, which I recommend you read.
So if you are storing a type that is anything other than int
or int?
in your "MetaDataInt" statebag entry, you will get an invalid cast exception, even if that value is something that could cast to int.
For example, if it contains a decimal, then you can only unbox it as a decimal. After that, you can cast as you normally would.
The following will work in the example scenario of unboxing a decimal:
return (int)(decimal)(ViewState["MetaDataInt"] ?? 0)
I suggest you look at the type of ViewState["MetaDataInt"]
in the debugger, and alter your cast accordingly.
Edit:
I can see what's going on. In your screenshot, you're attempting to unbox to a long, but your coalesced zero is an int. This is why you're getting an exception.
The following will fix your specific problem.
return (long)(ViewState["ParentID"] ?? 0L)
Note the 0L
, this is how you write a long as a numeric literal.
You're having the issue because you're coalescing the 0 int
with a null object
, so the result of that coalesce is also an object, in this case a boxed int
. A boxed int
can only be unboxed to an int
, but you're attempting to unbox it to a long
. We fix this by boxing a 0 long
instead, so you can unbox it to a long.
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do itlong l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicerlong l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones
– stebberg
Nov 14 '18 at 7:30
|
show 3 more comments
Use Nullable<int>
property instead of normal int
to cast with null-coalescing operator:
public int? MetaExtraDataInt
get return (int?)ViewState["MetaDataInt"] ?? 0;
set ViewState["MetaDataInt"] = value;
Based from operator precedence, the null-coalescing is performed after cast to Nullable<int>
, so that it returns zero when ViewState["MetaDataInt"]
contains null value.
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommendsNullable<int>
as return type if you want to contain null values, otherwiseint
should be enough.
– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
|
show 1 more comment
Does it work when ViewBag contains the key? Try to cast the viewstate return like:
get return (int)(ViewState["MetaDataInt"]) ?? 0);
(int)(ViewState["MetaDataInt"])
never returnsnull
, because whenViewState["MetaDataInt"]
returnsnull
, aInvalidOpertationException
is thrown when casting toint
.
– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278234%2fdoes-nullcheck-really-work-on-a-statebag%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
If (int)(ViewState["MetaDataInt"] ?? 0)
is throwing an InvalidCastException
, it must be because your value in the statebag isn't an int
.
This is because:
a boxed T can only be unboxed to T (or Nullable.) Once it is
unboxed, it’s just a value that can be cast as usual.
The above quote is taken from Representation and Identity by Eric Lippert, which I recommend you read.
So if you are storing a type that is anything other than int
or int?
in your "MetaDataInt" statebag entry, you will get an invalid cast exception, even if that value is something that could cast to int.
For example, if it contains a decimal, then you can only unbox it as a decimal. After that, you can cast as you normally would.
The following will work in the example scenario of unboxing a decimal:
return (int)(decimal)(ViewState["MetaDataInt"] ?? 0)
I suggest you look at the type of ViewState["MetaDataInt"]
in the debugger, and alter your cast accordingly.
Edit:
I can see what's going on. In your screenshot, you're attempting to unbox to a long, but your coalesced zero is an int. This is why you're getting an exception.
The following will fix your specific problem.
return (long)(ViewState["ParentID"] ?? 0L)
Note the 0L
, this is how you write a long as a numeric literal.
You're having the issue because you're coalescing the 0 int
with a null object
, so the result of that coalesce is also an object, in this case a boxed int
. A boxed int
can only be unboxed to an int
, but you're attempting to unbox it to a long
. We fix this by boxing a 0 long
instead, so you can unbox it to a long.
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do itlong l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicerlong l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones
– stebberg
Nov 14 '18 at 7:30
|
show 3 more comments
If (int)(ViewState["MetaDataInt"] ?? 0)
is throwing an InvalidCastException
, it must be because your value in the statebag isn't an int
.
This is because:
a boxed T can only be unboxed to T (or Nullable.) Once it is
unboxed, it’s just a value that can be cast as usual.
The above quote is taken from Representation and Identity by Eric Lippert, which I recommend you read.
So if you are storing a type that is anything other than int
or int?
in your "MetaDataInt" statebag entry, you will get an invalid cast exception, even if that value is something that could cast to int.
For example, if it contains a decimal, then you can only unbox it as a decimal. After that, you can cast as you normally would.
The following will work in the example scenario of unboxing a decimal:
return (int)(decimal)(ViewState["MetaDataInt"] ?? 0)
I suggest you look at the type of ViewState["MetaDataInt"]
in the debugger, and alter your cast accordingly.
Edit:
I can see what's going on. In your screenshot, you're attempting to unbox to a long, but your coalesced zero is an int. This is why you're getting an exception.
The following will fix your specific problem.
return (long)(ViewState["ParentID"] ?? 0L)
Note the 0L
, this is how you write a long as a numeric literal.
You're having the issue because you're coalescing the 0 int
with a null object
, so the result of that coalesce is also an object, in this case a boxed int
. A boxed int
can only be unboxed to an int
, but you're attempting to unbox it to a long
. We fix this by boxing a 0 long
instead, so you can unbox it to a long.
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do itlong l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicerlong l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones
– stebberg
Nov 14 '18 at 7:30
|
show 3 more comments
If (int)(ViewState["MetaDataInt"] ?? 0)
is throwing an InvalidCastException
, it must be because your value in the statebag isn't an int
.
This is because:
a boxed T can only be unboxed to T (or Nullable.) Once it is
unboxed, it’s just a value that can be cast as usual.
The above quote is taken from Representation and Identity by Eric Lippert, which I recommend you read.
So if you are storing a type that is anything other than int
or int?
in your "MetaDataInt" statebag entry, you will get an invalid cast exception, even if that value is something that could cast to int.
For example, if it contains a decimal, then you can only unbox it as a decimal. After that, you can cast as you normally would.
The following will work in the example scenario of unboxing a decimal:
return (int)(decimal)(ViewState["MetaDataInt"] ?? 0)
I suggest you look at the type of ViewState["MetaDataInt"]
in the debugger, and alter your cast accordingly.
Edit:
I can see what's going on. In your screenshot, you're attempting to unbox to a long, but your coalesced zero is an int. This is why you're getting an exception.
The following will fix your specific problem.
return (long)(ViewState["ParentID"] ?? 0L)
Note the 0L
, this is how you write a long as a numeric literal.
You're having the issue because you're coalescing the 0 int
with a null object
, so the result of that coalesce is also an object, in this case a boxed int
. A boxed int
can only be unboxed to an int
, but you're attempting to unbox it to a long
. We fix this by boxing a 0 long
instead, so you can unbox it to a long.
If (int)(ViewState["MetaDataInt"] ?? 0)
is throwing an InvalidCastException
, it must be because your value in the statebag isn't an int
.
This is because:
a boxed T can only be unboxed to T (or Nullable.) Once it is
unboxed, it’s just a value that can be cast as usual.
The above quote is taken from Representation and Identity by Eric Lippert, which I recommend you read.
So if you are storing a type that is anything other than int
or int?
in your "MetaDataInt" statebag entry, you will get an invalid cast exception, even if that value is something that could cast to int.
For example, if it contains a decimal, then you can only unbox it as a decimal. After that, you can cast as you normally would.
The following will work in the example scenario of unboxing a decimal:
return (int)(decimal)(ViewState["MetaDataInt"] ?? 0)
I suggest you look at the type of ViewState["MetaDataInt"]
in the debugger, and alter your cast accordingly.
Edit:
I can see what's going on. In your screenshot, you're attempting to unbox to a long, but your coalesced zero is an int. This is why you're getting an exception.
The following will fix your specific problem.
return (long)(ViewState["ParentID"] ?? 0L)
Note the 0L
, this is how you write a long as a numeric literal.
You're having the issue because you're coalescing the 0 int
with a null object
, so the result of that coalesce is also an object, in this case a boxed int
. A boxed int
can only be unboxed to an int
, but you're attempting to unbox it to a long
. We fix this by boxing a 0 long
instead, so you can unbox it to a long.
edited Nov 14 '18 at 9:06
answered Nov 13 '18 at 12:07
Doctor JonesDoctor Jones
17.4k116391
17.4k116391
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do itlong l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicerlong l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones
– stebberg
Nov 14 '18 at 7:30
|
show 3 more comments
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do itlong l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicerlong l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones
– stebberg
Nov 14 '18 at 7:30
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@[doctor jones] You're so right, it would be nicer to have a null point exception. But I promise you ;) check my debug, I don't have the key in my collection: dropbox.com/s/pguvim5ky2qdvzg/…
– stebberg
Nov 13 '18 at 12:19
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
@stebberg, I have updated my answer based on your feedback
– Doctor Jones
Nov 13 '18 at 12:47
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
Here is working example of the behavior dotnetfiddle.net/UPjorI
– Jodrell
Nov 13 '18 at 13:02
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
@stebberg Here is an even more applicable version dotnetfiddle.net/sm2SfU
– Jodrell
Nov 13 '18 at 13:07
hmm... that's so strange! As @DoctorJones says converting the second part to long will do it
long l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicer long l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones– stebberg
Nov 14 '18 at 7:30
hmm... that's so strange! As @DoctorJones says converting the second part to long will do it
long l = (long)(ViewState["ParentID"] ?? Convert.ToInt64(0));
or of course as suggested nicer long l = (long)(ViewState["ParentID"] ?? 0L);
To be honest I still don't understand why this works, I have to read your answer again and really think this through.. MANY thanks @DoctorJones– stebberg
Nov 14 '18 at 7:30
|
show 3 more comments
Use Nullable<int>
property instead of normal int
to cast with null-coalescing operator:
public int? MetaExtraDataInt
get return (int?)ViewState["MetaDataInt"] ?? 0;
set ViewState["MetaDataInt"] = value;
Based from operator precedence, the null-coalescing is performed after cast to Nullable<int>
, so that it returns zero when ViewState["MetaDataInt"]
contains null value.
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommendsNullable<int>
as return type if you want to contain null values, otherwiseint
should be enough.
– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
|
show 1 more comment
Use Nullable<int>
property instead of normal int
to cast with null-coalescing operator:
public int? MetaExtraDataInt
get return (int?)ViewState["MetaDataInt"] ?? 0;
set ViewState["MetaDataInt"] = value;
Based from operator precedence, the null-coalescing is performed after cast to Nullable<int>
, so that it returns zero when ViewState["MetaDataInt"]
contains null value.
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommendsNullable<int>
as return type if you want to contain null values, otherwiseint
should be enough.
– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
|
show 1 more comment
Use Nullable<int>
property instead of normal int
to cast with null-coalescing operator:
public int? MetaExtraDataInt
get return (int?)ViewState["MetaDataInt"] ?? 0;
set ViewState["MetaDataInt"] = value;
Based from operator precedence, the null-coalescing is performed after cast to Nullable<int>
, so that it returns zero when ViewState["MetaDataInt"]
contains null value.
Use Nullable<int>
property instead of normal int
to cast with null-coalescing operator:
public int? MetaExtraDataInt
get return (int?)ViewState["MetaDataInt"] ?? 0;
set ViewState["MetaDataInt"] = value;
Based from operator precedence, the null-coalescing is performed after cast to Nullable<int>
, so that it returns zero when ViewState["MetaDataInt"]
contains null value.
edited Nov 13 '18 at 10:04
answered Nov 13 '18 at 9:55
Tetsuya YamamotoTetsuya Yamamoto
15k42040
15k42040
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommendsNullable<int>
as return type if you want to contain null values, otherwiseint
should be enough.
– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
|
show 1 more comment
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommendsNullable<int>
as return type if you want to contain null values, otherwiseint
should be enough.
– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
thank you Tetsuya, but I need to have this property as int and not int?
– stebberg
Nov 13 '18 at 10:07
It depends on how you want to use it, I recommends
Nullable<int>
as return type if you want to contain null values, otherwise int
should be enough.– Tetsuya Yamamoto
Nov 13 '18 at 10:08
It depends on how you want to use it, I recommends
Nullable<int>
as return type if you want to contain null values, otherwise int
should be enough.– Tetsuya Yamamoto
Nov 13 '18 at 10:08
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
hmm, but I don't want to cast a null to an int, that's why I have the ?? check first and then I do the cast
– stebberg
Nov 13 '18 at 10:42
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
I honestly don't think the null-check and the convert is in the wrong order here, Unless the ?? messes up how the logic of parentheses works of course
– stebberg
Nov 13 '18 at 11:18
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
Many thanks @Jodrell I actually have no problem to solve the problem, I just want to know why ?? don't work on a statebag
– stebberg
Nov 13 '18 at 11:19
|
show 1 more comment
Does it work when ViewBag contains the key? Try to cast the viewstate return like:
get return (int)(ViewState["MetaDataInt"]) ?? 0);
(int)(ViewState["MetaDataInt"])
never returnsnull
, because whenViewState["MetaDataInt"]
returnsnull
, aInvalidOpertationException
is thrown when casting toint
.
– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
add a comment |
Does it work when ViewBag contains the key? Try to cast the viewstate return like:
get return (int)(ViewState["MetaDataInt"]) ?? 0);
(int)(ViewState["MetaDataInt"])
never returnsnull
, because whenViewState["MetaDataInt"]
returnsnull
, aInvalidOpertationException
is thrown when casting toint
.
– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
add a comment |
Does it work when ViewBag contains the key? Try to cast the viewstate return like:
get return (int)(ViewState["MetaDataInt"]) ?? 0);
Does it work when ViewBag contains the key? Try to cast the viewstate return like:
get return (int)(ViewState["MetaDataInt"]) ?? 0);
answered Nov 13 '18 at 9:58
Mr. XMr. X
437
437
(int)(ViewState["MetaDataInt"])
never returnsnull
, because whenViewState["MetaDataInt"]
returnsnull
, aInvalidOpertationException
is thrown when casting toint
.
– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
add a comment |
(int)(ViewState["MetaDataInt"])
never returnsnull
, because whenViewState["MetaDataInt"]
returnsnull
, aInvalidOpertationException
is thrown when casting toint
.
– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
(int)(ViewState["MetaDataInt"])
never returns null
, because when ViewState["MetaDataInt"]
returns null
, a InvalidOpertationException
is thrown when casting to int
.– HimBromBeere
Nov 13 '18 at 10:04
(int)(ViewState["MetaDataInt"])
never returns null
, because when ViewState["MetaDataInt"]
returns null
, a InvalidOpertationException
is thrown when casting to int
.– HimBromBeere
Nov 13 '18 at 10:04
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
Yes it works great when ViewBag contains the value.
– stebberg
Nov 13 '18 at 10:08
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278234%2fdoes-nullcheck-really-work-on-a-statebag%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
No, you could try to use
ViewState["MetaDataInt"] as int? ?? 0
instead.– Sebastian Hofmann
Nov 13 '18 at 9:55
1
Is it possible that
ViewState[...]
returnsDbNull
instead ofnull
?– HimBromBeere
Nov 13 '18 at 10:05
1
What is the value of
ViewState["MetaDataInt"]
when the exception is thrown? AlsoViewState["MetaDataInt"].GetType()
. You can check both in theImmediate Window
.– mjwills
Nov 13 '18 at 10:10
1
Please don't guess - check in the
Immediate Window
.– mjwills
Nov 13 '18 at 10:15
thank you @mjwills, when trying to call ViewState["MetaDataInt"].GetType() I receive a null pointer exception and when just trying ViewState["MetaDataInt"] it is "null"
– stebberg
Nov 13 '18 at 11:04