Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. The IS operator

The IS operator

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    martin_hughes
    wrote on last edited by
    #1

    Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

    M G J G B 5 Replies Last reply
    0
    • M martin_hughes

      Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

      M Offline
      M Offline
      Mike Poz
      wrote on last edited by
      #2

      Utini wrote:

      why if (control == TextBox) generates an error

      What error is it generating? Mike Poz

      C 1 Reply Last reply
      0
      • M martin_hughes

        Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

        G Offline
        G Offline
        Graham Nimbley
        wrote on last edited by
        #3

        Utini wrote:

        why if (control == TextBox) generates an error - can anyone illuminate me?

        Because it's suppose to be: control.GetType()==typeof(TextBox)

        1 Reply Last reply
        0
        • M martin_hughes

          Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

          J Offline
          J Offline
          Josh Smith
          wrote on last edited by
          #4

          Utini wrote:

          Well here's a case of "Read the Flipping Manual", if ever I saw one!

          Exactly, RTFM. Your code if( control == TextBox) does not and should not compile for several reasons. 1) TextBox is meaningless by itself. typeof(TextBox) means something. 2) Assuming you use the code provieded (1), it is meaningless to check for equality between a Control and a Type. What on Earth would that mean? Josh

          1 Reply Last reply
          0
          • M martin_hughes

            Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            The is operator checks if the object is (or inherits from) a specific class. (control is TextBox) does the same as Type.GetType("System.Web.UI.WebControls.TextBox").IsInstanceOfType(control). --- b { font-weight: normal; }

            1 Reply Last reply
            0
            • M Mike Poz

              Utini wrote:

              why if (control == TextBox) generates an error

              What error is it generating? Mike Poz

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              The error would be

              'TextBox' is a 'type' but is used like a 'variable'


              Scottish Developers events: * .NET debugging, tracing and instrumentation by Duncan Edwards Jones and Code Coverage in .NET by Craig Murphy * Developer Day Scotland: are you interested in speaking or attending? My: Website | Blog

              1 Reply Last reply
              0
              • M martin_hughes

                Well here's a case of "Read the Flipping Manual", if ever I saw one! I spent ages trying to compare a control to a TextBox, mostly without success - I went through all manner of .Equals(), .GetType, == anotherTextBox.GetType(), and all I needed to do was use was if (control is TextBox)! Gahhh! Still, I'm interested to know what the compiler is doing, and why if (control == TextBox) generates an error - can anyone illuminate me? Thanks, Martin.

                B Offline
                B Offline
                BoneSoft
                wrote on last edited by
                #7

                control is an instance, TextBox is a type. Apples and oranges (or more correctly, cookies and cookie cutters).


                Try code model generation tools at BoneSoft.com.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups