Click here to Skip to main content
12,450,335 members (45,226 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET C#4.0
Hey peps,

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

int buzzInfluence = 0;
int.tryParse(Request.QueryString["buzzInfluence"], out buzzInfluence);
 
int? z = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? null : buzzinflunce;
Posted 9-Jan-13 0:31am
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 headlessnick 9-Jan-13 7:11am
   
updated my query, thanks :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

No - you need to cast the int to a nullable int:
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...
  Permalink  
Comments
Marcus Kramer 9-Jan-13 9:54am
   
+5. Agreed.
the headlessnick 9-Jan-13 23:43pm
   
Nicely phrased....
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

you can fix this issue by changing your code as follows

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
int? zzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? default(int?) : buzzInfluence;
 
int? zzzz = string.IsNullOrWhiteSpace(Request.QueryString["buzzInfluence"]) ? new int?() : buzzInfluence;
  Permalink  
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 :)
Tharaka MTR 9-Jan-13 13:56pm
   
At least, if you can give me why you are down voting this? then I can provide the answers..
the headlessnick 9-Jan-13 23:46pm
   
Good.. I got a decent explanation.. thanks guys..
Dont' worry I too voted 5. ;)

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100