0

I have been using this ((Control)name) for some time, but I don't understand the construction of the brackets and what that means.

For example, when I'm looping through controls on a page, I do so like this:

foreach (Control ctrl in Booking_Quote.Controls)
{
    if (ctrl is Panel)
    {

        foreach (Control tb in ctrl.Controls)
        {
            if (tb is TextBox)
            {
                ((TextBox)tb).Text = "Hello world";

            }
            else
            {

            }
        }
    }
}

I am looking to know what ((TextBox)tb) means.

Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
  • 4
    It is an explicit type conversation from `Control` to `TextBox`. You can read [Explicit Conversion](http://msdn.microsoft.com/en-us/library/8s682k58%28v=vs.90%29.aspx) Since `TextBox` inherited from `Control`, it is ok to do it. http://msdn.microsoft.com/en-us/library/ms173105.aspx – Soner Gönül Apr 03 '14 at 13:57
  • 1
    and it's needed because not all `Control` objects have a `Text`property for example, so before accessing any specific property you've got to indicate what is the exact type of your quite generic `Control` object. – Laurent S. Apr 03 '14 at 13:58
  • 1
    ((TextBox)tb) is the old "C" style of casting. Here's a link that discusses this method of casting and the use of the "as" for casting. http://stackoverflow.com/questions/132445/direct-casting-vs-as-operator – Chris L Apr 03 '14 at 14:10
  • Its customary to upvote your answer. (as well as others you found helpful) ;) – crthompson Apr 12 '14 at 04:00

4 Answers4

0

That is a type cast to let the compiler know that the tb object is a actually a TextBox object.

mituw16
  • 5,126
  • 3
  • 23
  • 48
0

It is the cast operator. In this code:

if (tb is TextBox)
{
    ((TextBox)tb).Text = "Hello world";
}

You're casting tb to TextBox type, to get access to the Text property. Without the cast it would remain of Control type and the Text property would not be available to you.

Haney
  • 32,775
  • 8
  • 59
  • 68
0

It means that you're casting your object to TextBox (or Control)

In your example, you wrote this:

if (tb is TextBox)
{
   ((TextBox)tb).Text = "Hello world";
}

If tb is a TextBox, then you cast your object to a TextBox to have access to its methods and set values as you want.

You can do a explicit cast, which will throw an exception if the cast fails, OR you can convert your object, using the as operator, which will return null if the conversion fails, like this:

(tb as TextBox).Text = "Hello world";
Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
0

As a Control may not have a Text property, it first checks if the current Control tb is a TextBox via the line

if (tb is TextBox)
{
....
}

Then before using the Control tb as a TextBox you first need to explicitly cast it to TextBox in order to access the Text property.

You do this by preceding a variable with a Type object within parentheses, in this case (TextBox) tb

See Casting and Type Conversions (C# Programming Guide)

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118