About event TextChanged

T

Tony

Hello!

Here I have a simple question.
Below I have a method handler for event TextChanged called
textBox_TextChanged

If event TextChanged is trigged for a TextBox where the name property is set
to textBoxOccupation
than this statement will be true
if (tb == textBoxOccupation)
Console.WriteLine("Equal without name");

But this seems strange because tb is just a reference to the instance.
I prefer using this statement and check the name property with the right
literal here textboxbccupation
if (tb.Name.ToLower() == "textboxbccupation")
Console.WriteLine("Equal with name");

So now to my question how is it possible to use this statement tb ==
textBoxOccupation
when tb is just an address it's not a name ?

private void textBox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;

if (tb == textBoxOccupation)
Console.WriteLine("Equal without name");

if (tb.Name.ToLower() == "textBoxOccupation")
Console.WriteLine("Equal with name");
}

//Tony
 
B

Bob Powell [MVP]

the = operator usually checks equality of objects by confirming that they
are the same instance. tb == textBoxOccupation causes the compiler to
compare against the reference held in the local variable "textBoxOccupation"
which will of course be the same instance.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
M

Marc Gravell

But this seems strange because tb is just a reference to the instance.
Why is that strange? The IDE creates a field for every named control
(unless the "GenerateMember" flag is deliberately disabled). That is
what you are comparing to.
This is a reference equality check, and that instance *is* the sender.
You could use ReferenceEquals if you want to make it more obvious.
I prefer using this statement and check the name property with the right
literal here textboxbccupation
if (tb.Name.ToLower() == "textboxbccupation")
Console.WriteLine("Equal with name");
Two problems I can see here; first, ToLower() is a *bad thing* for this
type of test. Apart from being unnecessarily inefficient, there is the
"Turkish i" problem etc. Second, the compiler doesn't do anything to
help any more. If we rename our textbox in the IDE, there is a good
chance the code will stop working without explanation. But if we are
referencing the field, the compiler will red-flag the exact problem at
compile time.

I wasn't sure if the final question is answered by the above. If not,
please clarify what you mean?

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top