Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey peps,

Is this statement incorrect..? If yes Why? Please specify some references to support your answers...

C#
int buzzInfluence = 0;
int.tryParse(Request.QueryString["buzzInfluence"], out buzzInfluence);

int? z = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? null : buzzinflunce;
Posted
Updated 9-Jan-13 1:10am
v3
Comments
Ibrahim Uylas 9-Jan-13 6:46am    
What is the type of buzzinflunce? is it int ?
the headless nick 9-Jan-13 7:11am    
updated my query, thanks :)

see the details error message you got
Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'int'

you can fix this issue by changing your code as follows

C#
int buzzInfluence = 0;
int.TryParse(Request.QueryString["buzzInfluence"], out buzzInfluence);
int? z = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? null : (int?)buzzInfluence;

int? zz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? (int?)null : buzzInfluence;


you have to modify your code by adding (int?)buzzInfluence or (int?)null

also, here are some more alternatives
C#
int? zzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? default(int?) : buzzInfluence;

int? zzzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? new int?() : buzzInfluence;
 
Share this answer
 
v3
Comments
phil.o 9-Jan-13 8:20am    
int? zz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? (int?)null : buzzInfluence;
won't work because the return type isn't the same for both possibilities
Tharaka MTR 9-Jan-13 12:19pm    
I can't agree your comments. try this, this works definitely.
Tharaka MTR 9-Jan-13 12:38pm    
I don't know who are the persons down voted my answer. I only know two person did that. But I'm pretty sure they haven't tested this or they haven't check the actual logic behind the solution. And also, I'm pretty sure they had some doubt with the following part

int? zz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? (int?)null : buzzInfluence;

Logic behind here is, you can cast the null value to nullable type. Please googled and verified. or please try this in visual studio. even you can use as follows also

int? zzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? default(int?) : buzzInfluence;

int? zzzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? new int?() : buzzInfluence;

I'm just asking please check this and correct your voting.
Richard MacCutchan 9-Jan-13 18:05pm    
I just tried your code and it worked. Unfortunately my 5 vote was not enough to counteract the three 1s. However, you really should not fret about this, as downvotes are largely irrelevant; your reputation will grow despite these little setbacks.
Tharaka MTR 9-Jan-13 21:47pm    
Yep.. Thx Richard :)
No - you need to cast the int to a nullable int:
C#
int buzzInfluence = 0;
int.TryParse(Request.QueryString["buzzInfluence"], out buzzInfluence);

int? z = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? null : (int?)buzzInfluence;


If you don't, you are returning two different types, and it won't allow that.
But personally, I would do the test first, and then do the TryParse only if it wasn't empty - or use the result of the TryParse itself...
 
Share this answer
 
Comments
fjdiewornncalwe 9-Jan-13 9:54am    
+5. Agreed.
the headless nick 9-Jan-13 23:43pm    
Nicely phrased....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900