Click here to Skip to main content
15,917,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)
                   {
                      //
                   }
Posted
Updated 8-Jan-18 20:23pm
Comments
Karthik_Mahalingam 9-Jan-18 2:45am    
what is the value you are getting in
lbltime.Text,dwldstime ,dwldetime ?
[no name] 9-Jan-18 3:03am    
Please mention what is type of value you are expecting always in label.There are two solutions mentioned.One is with expecting the label text as numeric and other as datetime

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:
C#
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.
 
Share this answer
 
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...
 
Share this answer
 
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.
 
Share this answer
 
v7
Comments
Pete O'Hanlon 9-Jan-18 2:49am    
You are making assumptions that this is for a DateTime. Similarly, you shouldn't use Convert.ToDateTime if you can't guarantee that inputs are in suitable formats. Finally, if this is indeed a DateTime, there's so much that needs to be taken into account with regards to cultures that your code is overlooking. At best, your solution is a simplistic case that might solve for a narrow range of inputs.
[no name] 9-Jan-18 2:55am    
Well you are right and i have updated the date time to try parse,but below solution of yours is also based on assumptions that the label used is numeric.
Pete O'Hanlon 9-Jan-18 3:03am    
Indeed - and I called this out to highlight this to the user.
[no name] 9-Jan-18 3:04am    
Right. Up voted your solution and asked user for the label text data type in questions or comments section.

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