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. "object" type problem

"object" type problem

Scheduled Pinned Locked Moved C#
helpcsharpdesigntutorial
8 Posts 4 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.
  • S Offline
    S Offline
    Sheel Gohe
    wrote on last edited by
    #1

    In vb.net when i declare a variable as object type then i can set any property for that object that is not assigned at design time but but will be assigned at runtime. for example: dim a as object if typeof a is textbox then a.name="test1" end if The above code will not give any error at design time in vb.NET but in C# it will give an error during design time only - "'object' does not contain a definition for 'name'". Please help me,how do i solve this problem. Thanks in advance Sheel Sheel Gohe

    S 1 Reply Last reply
    0
    • S Sheel Gohe

      In vb.net when i declare a variable as object type then i can set any property for that object that is not assigned at design time but but will be assigned at runtime. for example: dim a as object if typeof a is textbox then a.name="test1" end if The above code will not give any error at design time in vb.NET but in C# it will give an error during design time only - "'object' does not contain a definition for 'name'". Please help me,how do i solve this problem. Thanks in advance Sheel Sheel Gohe

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      The following should work:

      if (typeof(a) is TextBox)
      {
      ((TextBox) a).name="test1";
      }


      www.troschuetz.de

      S L 2 Replies Last reply
      0
      • S Stefan Troschuetz

        The following should work:

        if (typeof(a) is TextBox)
        {
        ((TextBox) a).name="test1";
        }


        www.troschuetz.de

        S Offline
        S Offline
        Sheel Gohe
        wrote on last edited by
        #3

        Thanks Stefan. Sheel Gohe

        1 Reply Last reply
        0
        • S Stefan Troschuetz

          The following should work:

          if (typeof(a) is TextBox)
          {
          ((TextBox) a).name="test1";
          }


          www.troschuetz.de

          L Offline
          L Offline
          LdqxYY
          wrote on last edited by
          #4

          Actually the codes should be: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } Or: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; }

          J 1 Reply Last reply
          0
          • L LdqxYY

            Actually the codes should be: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } Or: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; }

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Actually, your code is just as incorrect. LdqxYY wrote: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } If a has been instantiated as an object, then the line if(a is textBox) will always evaluate false then in your second example: LdqxYY wrote: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } you are testing to see whether an instance of System.Type is-a TextBox - which it clearly isnt it is System.Type! To correct these two examples assume that there is a TextBox on the form with the instance name myTextBox. Now assume that you have a variable named myVar which is of type object, but is set to myTextBox

            // assumes the lines "object myVar;" and "myVar=myTextBox"
            if(myVar is TextBox)
            {
               ((TextBox)myVar).Text = "Hello World";
            }
            

            or in the second example above

            if(myVar.GetType() == typeof(TextBox))
            {
                ((TextBox)myVar).Text = "Hello World";
            }
            
            L 1 Reply Last reply
            0
            • J J4amieC

              Actually, your code is just as incorrect. LdqxYY wrote: object a = new object(); if( a is TextBox ) { ((TextBox) a).Name = "test1"; } If a has been instantiated as an object, then the line if(a is textBox) will always evaluate false then in your second example: LdqxYY wrote: object a = new object(); if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } you are testing to see whether an instance of System.Type is-a TextBox - which it clearly isnt it is System.Type! To correct these two examples assume that there is a TextBox on the form with the instance name myTextBox. Now assume that you have a variable named myVar which is of type object, but is set to myTextBox

              // assumes the lines "object myVar;" and "myVar=myTextBox"
              if(myVar is TextBox)
              {
                 ((TextBox)myVar).Text = "Hello World";
              }
              

              or in the second example above

              if(myVar.GetType() == typeof(TextBox))
              {
                  ((TextBox)myVar).Text = "Hello World";
              }
              
              L Offline
              L Offline
              LdqxYY
              wrote on last edited by
              #6

              J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!

              L J 2 Replies Last reply
              0
              • L LdqxYY

                J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!

                L Offline
                L Offline
                LdqxYY
                wrote on last edited by
                #7

                God!Wrong again,sorry!It should be: object a = new object(); // //some codes handle with the "a" // if( a is TextBox ) { ((TextBox) a).Name = "test1"; }

                1 Reply Last reply
                0
                • L LdqxYY

                  J4amieC,you are right!And I had realized my second example were incorrect after submitted.By the way,what exact mean in my first example is: object a = new object(); // //some codes handle with the "a" // if( a.GetType() is TextBox ) { ((TextBox) a).Name = "test1"; } I forgot writing something,hehe... Thanks again!

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  LdqxYY wrote: were incorrect after submitted That's why we have a Modify button ;)

                  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