Results 1 to 7 of 7

Thread: Getting variable names

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Getting variable names

    I know that Me.Name gets the name of the class of a variable, but is there
    a way to get the name of an instance of an object if for some reason you don't know it ? ( like in a form or something, using Me.somemethod )

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting variable names

    Instances of classes don't have names unless they have a Name property. Controls have a Name property and it will be the same as the name of the variable used to identify them by default if you create them in the designer, but you can change or clear the value of the Name property, or you can create controls with a blank Name property, or you can assign the same control to multiple variables, or you can assign Nothing to a variable. I'm guessing that you're talking about a variable name but the variable is not the object.
    VB Code:
    1. Dim obj1 As Object = New Object
    2. Dim obj2 As Object = obj1
    3. Dim obj3 As Object = obj2
    Now you have three variables and one object with no name.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jun 2005
    Posts
    625

    Re: Getting variable names

    So all three variables, obj1, obj2, obj3, point to the same object? If you modify one, you modify them all, correct?

    What if I use 'New', for example:

    Dim obj4 As New Object = obj3

    What happens then?

  4. #4
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Getting variable names

    it creates a new object (obj4) and set's it equal to what obj3 is

    but obj4 is a new object sperate of obj3
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Getting variable names

    Quote Originally Posted by JuggaloBrotha
    it creates a new object (obj4) and set's it equal to what obj3 is

    but obj4 is a new object sperate of obj3
    Actually no, it'll create a new Object but then obj4 will reference the same object as obj3 and the Newly created object won't be in use (Freed by the Garbage Collector)
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Getting variable names

    Actually nothing will happen because that code will not compile. You can't declare a variable as a new object and then assign another object on the same line. Whenever you use the word "New" you're creating a new object. Whenever you use an equals sign you're assigning the object returned by the expression on the right to the reference on the left.

    This declares a variable but creates no object, so the variable refers to Nothing:
    VB Code:
    1. Dim obj As Object
    This declares a variable, creates a new object and assigns it to the variable:
    VB Code:
    1. Dim obj As [U]New[/U] Object
    This does the same:
    VB Code:
    1. Dim obj As Object = [U]New[/U] Object
    This also does the same:
    VB Code:
    1. Dim obj As Object
    2.  
    3. obj = [U]New[/U] Object
    This creates two objects and assigns them to newly declared variables, then assigns one of the objects to the other variable too, thus the other object is lost:
    VB Code:
    1. Dim obj1 As New Object
    2. Dim obj2 As New Object
    3.  
    4. obj2 = obj1
    In the last example, obj2 now refers to the same object as obj1 and the object that obj2 did refer to is inaccessible because you don't have a variable that refers to it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: Getting variable names

    i had neglected that jm, it's java that would create a new object with the same value(s) as the previous object
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width