-
Apr 16th, 2018, 02:48 PM
#1
Thread Starter
Member
[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
-
Apr 16th, 2018, 03:17 PM
#2
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
-
Apr 16th, 2018, 04:47 PM
#3
Thread Starter
Member
Re: Problem creating a list of objects containing a structure
-
Apr 17th, 2018, 01:57 AM
#4
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:
Dim ed1 As New EntryDef("One", "First") MessageBox.Show(String.Format("Name: :{0}; Data: {1}", EntryDef.detail.Name, EntryDef.detail.Data), "EntryDef") MessageBox.Show(String.Format("Name: :{0}; Data: {1}", ed1.detail.Name, ed1.detail.Data), "ed1") Dim ed2 As New EntryDef("Two", "Second") MessageBox.Show(String.Format("Name: :{0}; Data: {1}", EntryDef.detail.Name, EntryDef.detail.Data), "EntryDef") MessageBox.Show(String.Format("Name: :{0}; Data: {1}", ed1.detail.Name, ed1.detail.Data), "ed1") MessageBox.Show(String.Format("Name: :{0}; Data: {1}", ed2.detail.Name, ed2.detail.Data), "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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|