Results 1 to 4 of 4

Thread: [RESOLVED] Problem creating a list of objects containing a structure

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Resolved [RESOLVED] Problem creating a list of objects containing a structure

    Here is my code - the problem is the line that says:
    "Console.WriteLine("{0} has Data of {1}", EntryDef.detail.Name, EntryDef.detail.Data)"

    I am trying to reference structure fields in an instance of EntryDef called "entrydef". However the IDE keeps changing from my instance name reference: "entrydef.detail.Name" back to the name of the structure definition: "EntryDef.detail.Name". Notice the difference in capitalization.

    No matter how many times I change it back to "entrydef", the IDE immediately reverts it back to "EntryDef" again. Kind of maddening. I am using VS 2015.

    I am probably doing something wrong which is why the IDE won't let me do what I am trying to do here - but I can't see what it is.

    Code:
    Public Class Form1
    
        Dim entrylist As New List(Of EntryDef)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim entry As EntryDef = New EntryDef("aaa", "bbb")
            entrylist.Add(entry)
            entry = New EntryDef("ccc", "ddd")
            entrylist.Add(entry)
            entry = New EntryDef("eee", "fff")
            entrylist.Add(entry)
    
            For Each entrydef As EntryDef In entrylist
                Console.WriteLine("{0} has Data of {1}", EntryDef.detail.Name, EntryDef.detail.Data)
            Next
    
            Console.ReadKey()
        End Sub
    End Class
    
    Public Class EntryDef
        Public Shared detail As Details
    
        Public Structure Details
            Dim Name As String
            Dim Data As String
            Public Sub New(A As String, B As String)
                Name = A
                Data = B
            End Sub
        End Structure
    
        Public Sub New(ByVal Name, ByVal Data)
    
            detail = New Details(Name, Data)
    
        End Sub
    End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Problem creating a list of objects containing a structure

    Why is this even an issue? You've created something to fight about where nothing existed before. Visual Basic is, and always has been, case insensitive. This allows the IDE to do case correction, which you can use to catch some subtle bugs....as long as you don't try to name two different things with names that differ only in capitalization, which is what you've done. Technically, the language shouldn't allow you to write some of the code you've written there, but it did.

    You have two options:

    1) If it works as written, then ignore it.

    2) DON"T USE THE SAME NAME AS SOMETHING ELSE WHEN IT ONLY DIFFERS IN CAPITALIZATION!!!
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2011
    Posts
    37

    Re: Problem creating a list of objects containing a structure

    Thanks that fixed it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Problem creating a list of objects containing a structure

    This is just plain bad code:
    Code:
    Public Class EntryDef
        Public Shared detail As Details
    
        Public Structure Details
            Dim Name As String
            Dim Data As String
            Public Sub New(A As String, B As String)
                Name = A
                Data = B
            End Sub
        End Structure
    
        Public Sub New(ByVal Name, ByVal Data)
    
            detail = New Details(Name, Data)
    
        End Sub
    End Class
    Why are you declaring a structure inside a class and why are you setting a Shared field inside a non-Shared constructor?

    You think that changing the name of your variable fixed the problem but it didn't, because you aren't actually aware of what the real problem is. The reason that the IDE kept changing the 'entrydef.detail.Name' to 'EntryDef.detail.Name' is that 'detail' is declared Shared, so it IS a member of the EntryDef type and not of the instance referred to by the 'entrydef' variable. By changing the name of your variable you have now made it look like you are accessing an instance field but you're not. You're still accessing the Shared field of the type. Try this and you'll see why this code is one big mess:
    vb.net Code:
    1. Dim ed1 As New EntryDef("One", "First")
    2.  
    3. MessageBox.Show(String.Format("Name: :{0}; Data: {1}",
    4.                               EntryDef.detail.Name,
    5.                               EntryDef.detail.Data),
    6.                 "EntryDef")
    7. MessageBox.Show(String.Format("Name: :{0}; Data: {1}",
    8.                               ed1.detail.Name,
    9.                               ed1.detail.Data),
    10.                 "ed1")
    11.  
    12. Dim ed2 As New EntryDef("Two", "Second")
    13.  
    14. MessageBox.Show(String.Format("Name: :{0}; Data: {1}",
    15.                               EntryDef.detail.Name,
    16.                               EntryDef.detail.Data),
    17.                 "EntryDef")
    18. MessageBox.Show(String.Format("Name: :{0}; Data: {1}",
    19.                               ed1.detail.Name,
    20.                               ed1.detail.Data),
    21.                 "ed1")
    22. MessageBox.Show(String.Format("Name: :{0}; Data: {1}",
    23.                               ed2.detail.Name,
    24.                               ed2.detail.Data),
    25.                 "ed2")
    That will show that, after creating the first object, the same data is retrieved via that object and the type itself. It will also show that, after creating the second object, the data accessed via the first object and the type are now the same as that accessed via the second object.

    If you explain what you're actually trying to achieve there, we can probably explain how best to achieve it. It will be somewhat different to what you have there.

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