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. Visual Basic
  4. get object's name

get object's name

Scheduled Pinned Locked Moved Visual Basic
question
13 Posts 6 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
    Marc Soleda
    wrote on last edited by
    #1

    In VB2005, most of the events pass the sender object as a parameter through the vent itself: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda

    ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

    X J 2 Replies Last reply
    0
    • M Marc Soleda

      In VB2005, most of the events pass the sender object as a parameter through the vent itself: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda

      ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

      X Offline
      X Offline
      xstoneheartx
      wrote on last edited by
      #2

      you can use : if ctype(sender,button).name="Button1" then 'do a task end if

      C M 2 Replies Last reply
      0
      • X xstoneheartx

        you can use : if ctype(sender,button).name="Button1" then 'do a task end if

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Or better yet, cast to Control, which has that property and is more 'guarenteed'.

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        1 Reply Last reply
        0
        • X xstoneheartx

          you can use : if ctype(sender,button).name="Button1" then 'do a task end if

          M Offline
          M Offline
          Marc Soleda
          wrote on last edited by
          #4

          Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda

          ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

          S X G 3 Replies Last reply
          0
          • M Marc Soleda

            Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda

            ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

            S Offline
            S Offline
            shreekar
            wrote on last edited by
            #5

            I think you know that the answer to your question is NO, not by the way already mentioned; however, what I understood you are saying is, in general, all objects will have some name, how can you get access to that name? If that is the question, then I think that the only way is to check for the object type and access its name by the method appropriate to that object type. for e.g., maybe you need to use reflection to get the class name OR you can get the type name which should be the class name - I am not sure. Just my thoughts.

            Shreekar

            M 1 Reply Last reply
            0
            • M Marc Soleda

              Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda

              ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

              X Offline
              X Offline
              xstoneheartx
              wrote on last edited by
              #6

              Try If sender.name="A Name" Then 'Do A Task End If Catch ex As Exception MsgBox("The object dosn't support name property") End Try

              1 Reply Last reply
              0
              • S shreekar

                I think you know that the answer to your question is NO, not by the way already mentioned; however, what I understood you are saying is, in general, all objects will have some name, how can you get access to that name? If that is the question, then I think that the only way is to check for the object type and access its name by the method appropriate to that object type. for e.g., maybe you need to use reflection to get the class name OR you can get the type name which should be the class name - I am not sure. Just my thoughts.

                Shreekar

                M Offline
                M Offline
                Marc Soleda
                wrote on last edited by
                #7

                Using reflection I just can get the class name/type. What I'm looking for is its name that, even it's not a control, I have instanciated an object as a class type and with a name. Isn't there any way to retrieve it?

                ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                C 1 Reply Last reply
                0
                • M Marc Soleda

                  In VB2005, most of the events pass the sender object as a parameter through the vent itself: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Is there any way to get the object's name (not the type) of the sender? .ToString returns its type and I'm looking for a way to get "Button1". I want to handle the xxxx_Click event for several buttons in the same event and without adding a new parameter with an id or something like that. Thanks in advance, Marc Soleda

                  ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                  J Offline
                  J Offline
                  Johan Hakkesteegt
                  wrote on last edited by
                  #8

                  You could also try: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control For Each Ctrl In Me.Controls If Ctrl.GetType Is sender.GetType Then MsgBox(Ctrl.Name.ToString) Next End Sub As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far? Johan

                  My advice is free, and you may get what you paid for.

                  M 1 Reply Last reply
                  0
                  • J Johan Hakkesteegt

                    You could also try: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Ctrl As Control For Each Ctrl In Me.Controls If Ctrl.GetType Is sender.GetType Then MsgBox(Ctrl.Name.ToString) Next End Sub As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far? Johan

                    My advice is free, and you may get what you paid for.

                    M Offline
                    M Offline
                    Marc Soleda
                    wrote on last edited by
                    #9

                    Johan Hakkesteegt wrote:

                    As a small concern, I would like to ask you this, are you sure that you are not trying to take efficient coding a bit too far?

                    In this context maybe, but I want to export this to a class library made by me and it would help a lot if we could handle some events in this way. Thanks, Marc Soleda

                    ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                    1 Reply Last reply
                    0
                    • M Marc Soleda

                      Using reflection I just can get the class name/type. What I'm looking for is its name that, even it's not a control, I have instanciated an object as a class type and with a name. Isn't there any way to retrieve it?

                      ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      Marc Soleda wrote:

                      Using reflection I just can get the class name/type

                      No, using reflection you can find properties and you can get their values.

                      Marc Soleda wrote:

                      I have instanciated an object as a class type and with a name.

                      If you mean the variable name, you can't get it.  If you mean a property on the control, you can get it with reflection, but if you don't know what the variable name is, then you're going to have a hard time. An event handler is always going to have a control come in, I assume you mean the question more generally.  It's unusual for your code to take an object and for you to have *no* idea what the object will be.

                      Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                      M 1 Reply Last reply
                      0
                      • M Marc Soleda

                        Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it? Thanks, Marc Soleda

                        ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

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

                        Marc Soleda wrote:

                        Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it?

                        You can't, as it doesn't have a name. What you are looking for is the name of the variable the holds the reference to the object. The problem is that there can be more than one variable referencing the object, or if there is a single variable, that variable might not have a name either, e.g. if it's part of an array.

                        --- Year happy = new Year(2007);

                        M 1 Reply Last reply
                        0
                        • C Christian Graus

                          Marc Soleda wrote:

                          Using reflection I just can get the class name/type

                          No, using reflection you can find properties and you can get their values.

                          Marc Soleda wrote:

                          I have instanciated an object as a class type and with a name.

                          If you mean the variable name, you can't get it.  If you mean a property on the control, you can get it with reflection, but if you don't know what the variable name is, then you're going to have a hard time. An event handler is always going to have a control come in, I assume you mean the question more generally.  It's unusual for your code to take an object and for you to have *no* idea what the object will be.

                          Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

                          M Offline
                          M Offline
                          Marc Soleda
                          wrote on last edited by
                          #12

                          Christian Graus wrote:

                          An event handler is always going to have a control come in...

                          Not always if it's an object's class - not a control - that raises an event when a something happens: I file has been received, ...

                          Christian Graus wrote:

                          It's unusual for your code to take an object and for you to have *no* idea what the object will be.

                          Unusual maybe but If I handle an event from several objects in only one eventhandler, then I need to differenciate between them to know which one has been. At last, I've solved by using Object.ReferenceEquals that determines if two objects are the same instance. Thanks, Marc Soleda

                          ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                          1 Reply Last reply
                          0
                          • G Guffa

                            Marc Soleda wrote:

                            Ok, that's right for a control but, if it isn't, and it's an object instanciated from a class library that after doing this cast doesn't have the Name property, how could I know it?

                            You can't, as it doesn't have a name. What you are looking for is the name of the variable the holds the reference to the object. The problem is that there can be more than one variable referencing the object, or if there is a single variable, that variable might not have a name either, e.g. if it's part of an array.

                            --- Year happy = new Year(2007);

                            M Offline
                            M Offline
                            Marc Soleda
                            wrote on last edited by
                            #13

                            Guffa wrote:

                            You can't, as it doesn't have a name.

                            That's the key! It doesn't have a name but I can compare two objects to know if they are the same instance that, for my purposes, is what I need: Object.ReferenceEquals Thanks, Marc Soleda

                            ... she said you are the perfect stranger she said baby let's keep it like this... Dire Straits

                            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