Click here to Skip to main content
15,890,399 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

Can anyone please tell me what is the mistake in it.
C#
string Stat;

                    if (fsensorvalue1 >= 05 &&  fsensorvalue1 <=29)
                    {

                        Stat = "Normal";

                    }

                    else if ( fsensorvalue1 >= 30 && fsensorvalue1 <= 34)

                    {
                        Stat = " Critical";

                    }
Posted
Comments
Azziet 27-Feb-13 6:51am    
what is fsensorvalue1 ?
CHill60 27-Feb-13 6:52am    
Use the Improve question link to post the error message or compiler warning you got
Chris Reynolds (UK) 27-Feb-13 6:54am    
What do you think is wrong? You haven't said what is happening that is unexpected. I can see a problem if fsensorvalue1 is a float as a value of 29.5 will fall between the two ifs.

Probably, the compiler is complaining about "Use of unassigned local variable 'Stat'" because there is a code path where Stat does not receive a value: when fsensorvalue1 is less than or greater than 34. This may not be possible in the wider context of your code, but the compiler will still complain about it.

Just change the definition line:
C#
string Stat = "Undefined";
and it will go away.
 
Share this answer
 
Hi,
There are couple of things in your code,

First: The string variable "State" is not initialized.

Second: The conditions are between range 4 and 35, so if the sensor value was beyond this range the "Stat" variable will not be used, so you will get an unused variable warning.

Have you considered this:

C#
string Stat = "";
if (fsensorvalue1 > 4 &&  fsensorvalue1 < 30)
{
    Stat = "Normal";
}

else if ( fsensorvalue1 > 29 && fsensorvalue1 < 35)
{
    Stat = "Critical";
}
else
{
   Stat = "Sensor Value '" + fsensorvalue1.ToString() + "' out of range.";
}



Regards
Jegan
 
Share this answer
 
v2
OR...

If the compiler is returning an error similar to
Error 1 Operator '>=' cannot be applied to operands of type 'System.Windows.Forms.TextBox' and 'int'

then you need to use the value of the textbox (or combobox or whatever you've used), converted to an int e.g.
C#
string Stat;
int i;

int.TryParse(fsensorvalue1.Text, out i);

if (i >= 05 &&  i <=29)
{
    Stat = "Normal";
}
else if ( i >= 30 && i <= 34)
{
    Stat = " Critical";
}
 
Share this answer
 
Comments
ontheline89 27-Feb-13 23:29pm    
Thank you so much for your answers.
CHill60 28-Feb-13 4:31am    
You're welcome. I'm curious though ... which error was it?
ontheline89 28-Feb-13 9:00am    
thanks .... it was saying, Unused Variable !

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