Ok, I'm really stumped, and I'm probably gonna feel like a fool when someone explains it to me.
I've created a strongly typed collection in vb.net (all lower case for ease of typing)
public class Foo
public name as string
public line as integer
end class
public class FooCollection
inherits collectionbase
public sub Add( byval aFoo as Foo )
me.list.add( aFoo )
end sub
end class
Assume that I load a bunch of Foo's, all of which have the same name but have different lines.
dim f as Foo
dim fs as Foos = new Foos
for x = 0 to 9
f = new foo
f.name = "bar"
f.line = x
fs.add(f)
next
When I iterate over the collection
for each f as Foo in Foos
debug.writeline(f.bar & " - " & f.line)
next
Each line of output displays *exactly the same data*, that is, line is the same in each case, despite having been explicitly given a different value at creation. This doesn't make any sense to me, so I must be missing something pretty dramatic. Shouldn't I get back the 10 different lines, after all, I put 10 distinct objects into the Foos collection
I'm seriously confused.
Sean

strongly typed collection wierdness, vb.net 2005
dasl
what you say would be the case if
public class Foo
public shared name as string
public shared line as integer
end class
name and line are shared.
But there are 10 instances, 10 foo objects. And the Debug.WriteLine Statement allways shows the same, the last added, foo.
Geoff_Praha
>what you say would be the case if
Right. I'm with you on that. It's just that the included code looked like pseudo code or code that was just retyped for the sake of the post. I made a guess based on the symptoms, surmising that the actual code may have defined "name" and "line" as shared or some other key difference that would have accounted for the bug.
Thanks tho!
- Mitchell S. Honnert
KWright
public class Foo
public name as string
public line as integer
end class
You don't have a constructor or private member variables, so to me the symptoms point to the fact that you only ever have one instance of foo. And that each time you are setting the name and line properties of what you think are separate instances, you're really just overwriting the current value. So, when you say...
>Each line of output displays *exactly the same data*
...I'm guessing that that values are displayed are name {"bar"} and line {9}, i.e. the last values that happened to be set.
My recommendation would be to change name and line to be properties rather than public members of foo and to create a constructor. I think you'll be able to debug the problem from there
- Mitchell S. Honnert
Goober Foober
The actual code in question was imported from VB2003 and so used what I guess is now the *old style* typed collections, and it still returns bogus results. I'm really puzzled. I'll take a look at generics - it seems pretty easy and maybe it'll fix my problem.
And yes, I do use Option Explicit and Option Strict - it's saved my bacon more than a few times since I've started using them.
Blicos
I don't know why the output should be the same for every iteration (I suppose the declaration of the variable fs as Foos is just a little mistake that happened typing the code into the post, of course is should be Dim fs as FooCollection).
Anyway, I recommend to use generics in this case. Use a Collection(Of Foos) instead, so you don't have to write any additional code for getting in strongly typed :-)
cvajre
zhargoli
The Debug.WriteLine Statement allways shows the same, the last added, foo.
I copied the code and it did not compile:
Variable f hides a variable in an enclosing block.
Dim f As Foo
'What is Foos Is this FooCollection
'Dim fs As Foos = New Foos
Dim fs As FooCollection = New FooCollection
' Please allways choose: Option Exlicit On!
' For x = 0 To 9
For x As Integer = 0 To 9
f = New Foo
f.name = "bar"
f.line = x
fs.Add(f)
Next
' What is Foos
' Here is the reason, but i get a Error Message:
' "Variable f hides a variable in an enclosing block."
' For Each f As Foo In Foos
' f is allready declared in Dim f as Foo
For Each f In fs
Debug.WriteLine(f.name & " - " & f.line)
Next
JayWK
Can you post the actual code since the code you have posted actually works, it's kind of hard figuring out what's wrong...
Blaine Anderson