C#中的字符串赋值 [英] Strings Assignment in C#

查看:733
本文介绍了C#中的字符串赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



任何人都可以告诉我它里面的错误是什么。

Hello,

Can anyone please tell me what is the mistake in it.

string Stat;

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

                        Stat = "Normal";

                    }

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

                    {
                        Stat = " Critical";

                    }

推荐答案

可能是编译器抱怨使用未分配的局部变量''Stat' '因为有一个代码路径,其中 Stat 没有收到值:当 fsensorvalue1 小于或大于34.在你的代码的更广泛的上下文中这可能是不可能的,但是编译器仍然会抱怨它。



只需更改定义行:

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:
string Stat = "Undefined";

它会消失。




有几件事情在你的代码中,



第一:字符串变量State未初始化。



第二名:条件介于4到35之间,因此如果传感器值超出此范围,则不会使用Stat变量,因此您将得到一个未使用的变量警告。



您是否考虑过这个:



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:

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.";
}







问候

Jegan




Regards
Jegan


OR ...



如果编译器返回的错误类似于

错误1运算符''> =''无法应用于''System.Windows.Forms.TextBox''和''int''类型的操作数

<然后你需要使用文本框的值(或组合框或你用过的任何东西),转换为int例如

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.
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";
}


这篇关于C#中的字符串赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆