> =和< =不能应用于字符串和字符串类型的操作数 [英] > = And < = cannot be applied to operands of type string and string

查看:81
本文介绍了> =和< =不能应用于字符串和字符串类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我需要将标签与两个值进行比较。但是我收到此错误> =无法应用于字符串和字符串类型的操作数

任何帮助都会非常感激。谢谢。



我尝试过:



Hi All,
I need to compare a label with two values .But I am getting this error ">= cannot be applied to operands of type string and string "
Any help will be really appreciated.Thanks in advance.

What I have tried:

if (lbltime.Text >= dwldstime && lbltime.Text == dwldetime)
                   {
                      //
                   }

推荐答案

错误告诉您究竟是什么问题。基本上,这里有两个字符串值;一个在lbltime.Text中,另一个在dwldetime中。现在,我不知道你在这里期望什么类型,所以我将假设为了这个例子的目的,它们都是数字,这意味着你将不得不将它们转换为适当的数字类型。现在,假设这些都是数字,那么你将要使用TryParse(比使用类似int.Parse或Convert.ToInt32更好的选择,因为它应对垃圾值)来转换输入并执行计算返回的值。这样的事情是合适的:
The error is telling you exactly what the problem is. Basically, you have two string values here; one in lbltime.Text and the other in dwldetime. Now, I have no idea what type you expect in here so I'm going to assume for the purposes of this example that they are both meant to be numeric, which means that you are going to have to convert them into an appropriate numeric type. Now, suppose that these are both numbers, then you are going to want to use TryParse (a much better choice than using something like int.Parse or Convert.ToInt32 because it copes with rubbish values) to convert the inputs and perform the calculations on the returned values. Something like this would be appropriate:
int inputTime;
if (int.TryParse(lbltime.Text, out inputTime))
{
  int comparisonTime;
  if (int.TryParse(dwldetime, out comparisonTime))
  {
    if (inputTime >= comparisonTime && inputTime <= comparisonTime)
    {
      // ....
    }
  }
}

请注意阅读编译器错误消息是宝贵的技能。对于任何编码任何时间段的人来说,这个应该很容易理解。

Please note that reading compiler error messages is a valuable skill. This one should be an easy one to understand for anyone who has been coding for any period of time.


因为String类没有实现那些运算符(只有==和!=)和原因是它必须进行一些排序(并且它会有区域设置识别),这会使基本类的内容太复杂......

当然你可以实现你的你自己的运算符,但可能更好地使用String的Compare / CompareTo方法(适合的那个)......



看看你选择的变量名我我认为你在这里有你想要比较的日期和时间值作为字符串,这当然是最糟糕的方式。如果确实这些是日期和时间值比较那么...
That because String class does not implement those operators (only == and !=) and the reason is that it would have to do some sorting (and it would have be locale aware), and that would complicate things for a basic class far too much...
Of course you can implement your own operators for your case, but probably better use the Compare/CompareTo method of String (the one that fits)...

Looking at your choice of variable names I was thinking that you have here date and time values you try to compare as strings, that of course would be the worst way to do. If indeed these are date and time values compare then as is...


请使用比较功能的日期时间。假设标签是日期时间格式。



Please use Compare function for date time.On an assumption that the labels are in date time format.

DateTime dateval;
DateTime.TryParse(lbltime.Text,out dateval);
result1=DateTime.Compare(dateval, dwldstime);
result2=DateTime.Compare(dateval, dwldetime);
string relationship;
 if (result1 < 0 && result2==0)
       {
}





如果这有帮助请投票给这个答案。这确实有帮助。



If this helps please vote for this answer.This indeed helps.


这篇关于&GT; =和&lt; =不能应用于字符串和字符串类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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