Last post Mar 18, 2016 02:17 PM by Ken Carter
Mar 18, 2016 01:30 PM|Ken Carter|LINK
I'm a rank noobie to C# so please excuse my ignorance. I'm having to quickly convert several page from VB to C# along with code behind an have run into a simple string function where in the VB we are doing this: String_Var > "A" .... Here is what I am
getting for red line:
(local variable) string ARSFamily
Operator '>' cannot be applied to operands of type string and string
The code fragment looks like this:
if (ARSFamily != "Select!" && !string.IsNullOrEmpty(ARSFamily) && ARSFamily > "A")
This is sitting at the third position of a && conditional If statement so I was thinking that I might be able to use "string.compare" to do this evaluation but I've not been able to wrap my head around how I would go about doing that.
Mar 18, 2016 01:49 PM|venkatachalapathi|LINK
in C#, we can use String.CompareOrdinal(string,string) method for string comparison in this scenario:
string strOne = "XYZ";
string strTwo = "xyz";
int result = String.CompareOrdinal(strOne, strTwo);
so that we can modify condition like;
if (ARSFamily != "Select!" && !string.IsNullOrEmpty(ARSFamily) && String.CompareOrdinal(ARSFamily, "A") > 0)
Mar 18, 2016 02:03 PM|gerrylowry|LINK
you can use "==" and "!=" with string comparison, but that is not recommended.
you can not use "<" or ">" with string comparison
MSDN is your best friend for questions like yours.
https://msdn.microsoft.com/en-us/library/cc165449.aspx "How to: Compare Strings (C# Programming Guide)"
i strongly recommended that you study the above article.
also, follow the links from "Comparing Strings in the .NET Framework" https://msdn.microsoft.com/en-us/library/fbh501kz.aspx
beware https://msdn.microsoft.com/en-us/library/system.string.compareordinal.aspx "String.CompareOrdinal Method"; it's important to understand
the pros and cons of any .NET Framework methods that you use.
with MSDN articles, pay attention to notes and remarks since they tend to highlight the gotchas.
Mar 18, 2016 02:17 PM|Ken Carter|LINK
Thanks Gerry. I was hoping that there was a simpler solution to this but apparently there isn't. I think my course here will be to restructure my IF statement a bit to avoid that comparison and I believe that I can do that. This may have just forced
me to eliminate some extraneous code because I don't want to end up injecting a entirely new code block to cover this replacement for the single > operator coming over from VB.
Good material for study however, really appreciate it!