469,622 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

Explaination please - variable placement.

Newbie looking for some insight here.
I am writing a block to check limitations on a query string as follows:

public bool validNumber(string qstring){
bool isValidNumber;
foreach(char c in qstring){
if(!Char.IsNumber(c)){
isValidNumber = false;
break;
}
else{isValidNumber = true;}
}
return isValidNumber;
}

This however returns a compiler error: - CS0165: Use of unassigned local
variable 'isValidNumber' - at the "return isValidNumber" line.
But if I move the second line [isValidNumber decleration] outside of the
function, it works fine.

Why does this happen if I am only using the variable within the function?

And if anyone has an easier way of checking a number only value, I'd sure
like to know.

Thanks for any help with this.
Nov 17 '05 #1
3 1211
Thanks, but I am using C# not VB.NET
IsNumeric does not appear to be available using C#

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"estafford" <estafford your pants @newmancom.com> wrote in
news:u2**************@TK2MSFTNGP09.phx.gbl:
And if anyone has an easier way of checking a number only value, I'd sure like to know.


how about using the IsNumeric command. Or you can check the data type to
ensure it is an integer:

Try
Dim iNumberToCheck = Convert.ToInt16(sNumbertoCheck)
Return True
Catch E as Exception
Return False
End Try

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 17 '05 #2
estafford wrote:
Newbie looking for some insight here.
I am writing a block to check limitations on a query string as follows:

public bool validNumber(string qstring){
bool isValidNumber;
foreach(char c in qstring){
if(!Char.IsNumber(c)){
isValidNumber = false;
break;
}
else{isValidNumber = true;}
}
return isValidNumber;
}

This however returns a compiler error: - CS0165: Use of unassigned local
variable 'isValidNumber' - at the "return isValidNumber" line.
But if I move the second line [isValidNumber decleration] outside of the
function, it works fine.

Why does this happen if I am only using the variable within the function?

And if anyone has an easier way of checking a number only value, I'd sure
like to know.

Thanks for any help with this.

All you have to do is assign isValidNumber a value when declaring it, i.e.
bool isValidNumber = false; //default value

PS, I would have written it like this, to avoid the temporary variable
altogether:
public bool validNumber(string qstring){
foreach(char c in qstring)
{
if(!Char.IsNumber(c))
return false;
}

return true;
}

Another technique could be to try "int.Parse(qstring)". If the parse
works, then it is a number, and if it gives an exception then it is not.

Nov 17 '05 #3
I also should mention that I did try your data type suggestion and that
works great.
I didn't realize such a simple solution was available.

I spent hours sifting through the .NET and C# info in the SDK and MSDN.
Thanks for your help.

ES

"Lucas Tam" <RE********@rogers.com> wrote in message
news:Xn***************************@140.99.99.130.. .
"estafford" <estafford your pants @newmancom.com> wrote in
news:u2**************@TK2MSFTNGP09.phx.gbl:
And if anyone has an easier way of checking a number only value, I'd sure like to know.


how about using the IsNumeric command. Or you can check the data type to
ensure it is an integer:

Try
Dim iNumberToCheck = Convert.ToInt16(sNumbertoCheck)
Return True
Catch E as Exception
Return False
End Try

--
Lucas Tam (RE********@rogers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by Giancarlo Niccolai | last post: by
20 posts views Thread by Ioannis Vranos | last post: by
5 posts views Thread by Andrew Poulos | last post: by
5 posts views Thread by Lagarde Sébastien | last post: by
15 posts views Thread by LuB | last post: by
9 posts views Thread by karthikbalaguru | last post: by
72 posts views Thread by goacross | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.