IsNullEmptyOrWhiteSpace Yielding True when Text is ">=" - c#

I have a if statement that is doing validation for textbox on a WinForms application.
if (txtRule.Text.IsNullEmptyOrWhiteSpace())
{
result = false;
//error provider code
}
I know for a fact that the text in this textbox is the string ">=" because I'm using a breakpoint to figure out what the current text is in the textbox.
Obviously the text cannot be null since there is something in the textbox, and the same can be said about it not being empty. This means that is must be the case that the special characters ">=" are considered white space for some reason?
I would like to know the reason this if statement yields true when everything seems to be pointing towards a false value.

Considering that there is no instance method with that name in the System.String class (the most similar by name is String.IsNullOrWhiteSpace that is static, while you are using your method on a txtRule.Text that is a System.String), then probably that is an extension method written by someone where you work. Try doing a Go To Definition and check.

Related

HashSet<string>.Contains not finding match with variable, but does with hard-coded string

I've run into a weird situation when looking up a value in a HashSet. It finds a match correctly when I pass in a hard-coded string, but not when using a variable whose contents are exactly the same as that hard-coded string.
This image and the debugging info at the bottom shows exactly the behavior I'm wondering about. Why does orgs.Contains(org) return false while orgs.Contains("Barr Family Practice") returns true? In this case, the contents of org are exactly "Barr Family Practice".

C# how to get value from specific cell datagridview

i am working on C# and trying to save my datagridview to xml.
my code as follow :
writer.WriteStartElement("NamaBarang");
writer.WriteString(dgvCart.Rows[i].Cells[1].Value.ToString);
writer.WriteEndElement();
and the error occur at
writer.WriteString(dgvCart.Rows[i].Cells[1].Value.ToString);
Error 2 The best overloaded method match for
'System.Xml.XmlWriter.WriteString(string)' has some invalid
arguments C:\Users\Eric\Desktop\C#\Gridview\Gridview\Form1.cs 59 25 Gridview
and
Error 3 Argument 1: cannot convert from 'method group' to
'string' C:\Users\Eric\Desktop\C#\Gridview\Gridview\Form1.cs 59 44 Gridview
i am previously a vb user so i am having a hard time in c#
please help.
Youve forgotten to put empty brackets () after your ToString
VB isn't bothered about this but c# is- every time you want to call a method that has no arguments, you have to put empty brackets
//this is fine in VB
Dim x as String = y.ToString
//so is this
Dim x as String = y.ToString()
//in c# we insist on brackets when calling a method
string x = y.ToString();
//no brackets means "get the value of the property"
int I = mystring.Length;
Seeing something that looks like a method name, without brackets, usually means it's a property rather than a method. It can occasionally mean that the method itself is being used as an argument - a way of passing a method around as a variable, but that's beyond the scope of what we're discussing here.. Read up on delegates if you're interested.
In short, if you want to call a method in c#, it absolutely must have brackets after the name, whether there are arguments or not
Also handy to remember that when you see the error about "is a method group" it probably means you've tried to call a method but omitted the brackets.
C# is a struggle for VB people because it's essentially way more demanding that the syntax be absolutely right- you'll get used to it though, and it does help eventually!

C# - What's the opposite of "IsNullOrWhiteSpace"?

I am using an if condition with "IsNullOrWhiteSpace(+textbox)" to refer to a text filed that has no value or whitespace only. However, now I need to know hot to specify a field that is not empty or whitespace only.
This is the code I wrote:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
But what if I want to specify to have the command run only when the textfield is NOT null or empty?
Thank you very much for any help. I am a beginner. Help is appreciated.
Have a great day!
string.IsNullOrWhiteSpace returns true if the text is null or whitespace. If the return value is false, then the text is populated with non-whitespace characters.
Search for where the condition is not true using !.
if (!string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text))
this is the equivalent of:
if (string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text) == false)
Both of the above will enter the if statement if pathofsrcfilesTOCOPY.Text is populated with non-whitespace text.
Worth noting, that although the accepted answer is correct, I find it easier to follow, when assigning its result to a variable and then evaluating that variable in the if condition.
bool isPathEmpty = string.IsNullOrWhiteSpace(pathofsrcfilesTOCOPY.Text);
if (!isPathEmpty)
{
//...
}

Why checking for null before comparing a value to a constant?

Sometimes, a value must be checked for equality with a constant. In such cases, I've always seen the code like this:
if (!string.IsNullOrEmpty(text))
{
if (text == "Some text here")¹
{
// Do something here.
}
}
In my case, I would rather write:
if ("Some text here".Equals(text))
{
// Do something here.
}
After all, if text is null, Equals will return false, which is expected. The inversion of a constant and a variable feels strange, but is still understandable for a beginner, and avoids the NullReferenceException which would be thrown with text.Equals("Some text here").
Am I missing something?
Why all source code I've seen use the syntax from the first example, and never from the second one?
¹ In real code, it would rather be a constant or a readonly field. To shorten the examples, I put the strings inline.
In such cases, I've always seen the code like this:
You're right to think that's odd and unnecessary, because it is. It's a completely superfluous null or empty check. Frankly, I'd admonish code like that in a code review.
if (text == "Some text here") {
// Do something here.
}
is perfectly fine and that's what I'd use.
Am I missing something?
No, you're not missing anything.
Why all source code I've seen use the syntax from the first example, and never from the second one?
Because you're looking for love in all the wrong places?
There is nothing wrong with just this.
if (text == "Some text here")
{
// Do something here.
}
There is no need to check for null/empty, since it won't be equal anyway.
If you wish to use the Equals method, there is a version that isn't sensitive to null values.
if (string.Equals(text, "Some text here"))
{
// Do something here.
}
Your version is fine so long as you have a literal or something that you know will not be null.
If, on the other hand, you had
if (text1.Equals(text2))
then clearly you would need to defend against text1 being null.
But I would stop using Equals here and use == which removes the need for the null check
if (text1 == text2)
Are you preferring to use Equals because of your Java roots?
I would think it is in human nature, when you compare smth, you always compare "your" stuff against someone else. not the other way around, i think same natual thinking went to C# coding as well :)
I would just use if (text == "Some text here"). It's clear, concise, and fast. The IsNullOrEmpty check you refer to might be a (most likely useless) micro-optimization.

ReportViewer IIf expression in text box returns #ERROR

I'm using ReportViewer for work and I'm trying to get an IIf expression to work with a few text boxes that are using FormatCurrency(). The reason I needed an IIf statement in the first place was because occasionally on this report, there will be null parameters. The example here is a shipping box that may or may not have a value. If it has a value, say 15, it will format correctly to $15.00. If it doesn't have a value it returns #ERROR. Obviously this won't do.
Here is one IIf statement I'm using on a different report that works totally fine (sets the visibility of a text box):
=IIf(Fields!DATASET_NAME.Value.ToString() <> "DELETE", True, False)
Here is the one that's not working:
=IIf(Parameters!ShipAmt.Value.ToString() <> "", FormatCurrency(Parameters!ShipAmt.Value,2), "")
The IIf seems to work because if I enter a value it will still format correctly, but without a value it still returns #ERROR. Any ideas?
Calling ToString() on a null value goes Kaboom. Try IsNot Nothing instead.
But I think the real answer is here.

Categories