Structure member name conflict rules

StewartBW 1,145 Reputation points
2024-05-20T15:04:43.32+00:00

Hello

When using Structures:

Public Structure Blah
   Public Name As String
   Public Home As String
End Structure

I have 2 strings: Name and Home, does it matter for the variable names inside the Structure to be unique?

ie if I have another string variable somewhere else with the same name of "Name or Home"?

Thanks,

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,755 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,851 Reputation points Microsoft Vendor
    2024-05-21T01:16:18.0066667+00:00

    Hi @StewartBW ,

    If you declare variables with the same names (Name and Home) inside a method or another structure, they are considered different because they are in different scopes.

        Public Structure Blah
            Public Name As String
            Public Home As String
        End Structure
        Public Structure AnotherBlah
            Public Name As String
            Public Home As String
        End Structure
        Public Sub ExampleMethod()
            Dim Name As String = "LocalName"
            Dim Home As String = "LocalHome"
    
            Dim myBlah As Blah
            myBlah.Name = "BlahName"
            myBlah.Home = "BlahHome"
    
            Dim myAnotherBlah As AnotherBlah
            myAnotherBlah.Name = "AnotherBlahName"
            myAnotherBlah.Home = "AnotherBlahHome"
    
            Console.WriteLine(Name) ' Outputs: LocalName
            Console.WriteLine(Home) ' Outputs: LocalHome
            Console.WriteLine(myBlah.Name) ' Outputs: BlahName
            Console.WriteLine(myBlah.Home) ' Outputs: BlahHome
            Console.WriteLine(myAnotherBlah.Name) ' Outputs: AnotherBlahName
            Console.WriteLine(myAnotherBlah.Home) ' Outputs: AnotherBlahHome
        End Sub
    
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Dewayne Basnett 1,366 Reputation points
    2024-05-20T15:24:43.67+00:00

    If I understand correctly the answer is no. Try this out,

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        BlahTest()
    
    End Sub
    
    Public Structure Blah
    
        Public Name_ As String
    
        Public Home As String
    
    End Structure
    
    Public Name_ As String
    
    Private Sub BlahTest()
    
        Dim Name_ As String
    
        Dim blah1 As New Blah
    
        blah1.Name_ = "BLAH1"
    
        Name_ = "local"
    
        Me.Name_ = "class"
    
    End Sub
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.