473,491 Members | 2,636 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1285
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
4433
by: Giancarlo Niccolai | last post by:
Hello all. I have peeked through the FAQ and all relevant links, and also through Stroustrup book, but I have not been able to find an answer, so I have to post here as a last resort. It...
20
4110
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
7
3569
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
1
9582
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
5
1374
by: Andrew Poulos | last post by:
I tested the JSON parse/to code from json.org and it works but I don't understand how. Could someone explain how something in this format works: (function() {...})(); Andrew Poulos
5
5815
by: Lagarde Sébastien | last post by:
Hello, I write code to debug new call with following macro: #define new (MemoryManager::Get().setOwner (__FILE__, __LINE__, _FUNCTION-), FALSE) ? NULL : new The setOwner allow to save the...
15
4986
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
9
3261
by: karthikbalaguru | last post by:
Hi, I find that articles stating that 'placement new' constructs an object on a pre-allocated buffer and so takes less time. Actually, we have to consider the allocation of the buffer and then...
72
2975
by: goacross | last post by:
char ch='a'; int v=sizeof ++ch; cout<<ch<<endl;// output: 'a' why not 'b'? thanks
0
7112
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7146
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3074
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.