Enumerating Collections - Big Bug in vb.Net (I think.)

J

John Talli

DOESNT WORK
1 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
2 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
3 If itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code Then

WORKS
5 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
6 Dim sdbFld_Unq_Code As String = itm.dbFld_Unq_Code
7 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
8 Dim sitmHTML_dbFld_Unq_Code As String = itmHTML.dbFld_Unq_Code
9 If sdbFld_Unq_Code = sitmHTML_dbFld_Unq_Code Then


Why doesn't line #3 above work? I have just ran around in circles trying
to figure out why line #3 produces FALSE results although it is equivilant
to line #9.

Line #9 just uses variables from line #6 and line #8 and should be the same
as line #3.

It seems unless I actually USED a variable in lines #6 & #8,
vb.Net doesn't really load into memory whatever is contained in either
itm (line #1)
itmHTML (line #2)

Unless this is explainable, it seems to be a VERY BIG BUG in vb.Net.

Is it explainable?
 
F

Family Tree Mike

John Talli said:
DOESNT WORK
1 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
2 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
3 If itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code Then

WORKS
5 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
6 Dim sdbFld_Unq_Code As String = itm.dbFld_Unq_Code
7 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
8 Dim sitmHTML_dbFld_Unq_Code As String = itmHTML.dbFld_Unq_Code
9 If sdbFld_Unq_Code = sitmHTML_dbFld_Unq_Code Then


Why doesn't line #3 above work? I have just ran around in circles trying
to figure out why line #3 produces FALSE results although it is equivilant
to line #9.

Line #9 just uses variables from line #6 and line #8 and should be the
same
as line #3.

It seems unless I actually USED a variable in lines #6 & #8,
vb.Net doesn't really load into memory whatever is contained in either
itm (line #1)
itmHTML (line #2)

Unless this is explainable, it seems to be a VERY BIG BUG in vb.Net.

Is it explainable?


What datatypes are itm.dbFld_Unq_Code and itmHTML.dbFld_Unq_Code?
 
T

Tom Dacon

It would be helpful if you would include a complete set of code that
illustrates the problem you're having. From these isolated fragments of code
it's impossible to determine what's going on.

Tom Dacon
Dacon Software Consulting
 
J

John Talli

Option Strict is On

I discovered the code in the "long" way works because I did try to breakpoint &
print-out the property "dbFld_Unq_Code ".

That is when I realized it works in the "long" programming code way but not in the
"abbreviated" way.
 
J

John Talli

What datatypes are itm.dbFld_Unq_Code and itmHTML.dbFld_Unq_Code?

They are both strings.

"yPNameX" in Me._clsInfo.yPNameX is an ArrayList .

Tom Dacon asked for the entire code in another message:

' DOESNT WORK
For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
If itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code Then <<<
Dim b1 As Integer = 0 ' <<< so I can set a breakpoint
End If
Next
Next

' WORKS
For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
Dim sdbFld_Unq_Code As String = itm.dbFld_Unq_Code <<< REQUIRES FORCED reading of
class
For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
Dim sitmHTML_dbFld_Unq_Code As String = itmHTML.dbFld_Unq_Code <<< REQUIRES
FORCED reading of class
If sdbFld_Unq_Code = sitmHTML_dbFld_Unq_Code Then
Dim b1 As Integer = 0 ' <<< so I can set a breakpoint
End If
Next
Next

It does seem like either vb.Net requires us to FORCE read the class "itm" that
is stored in the yPNameX (ArrayList) into temporary variables in order to get
to the contents of "itm".

itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code SHOULD WORK but doesn't.

I would definitedly call this a BUG and not a feature. (Unless someone can explain.)
 
F

Family Tree Mike

John Talli said:
What datatypes are itm.dbFld_Unq_Code and itmHTML.dbFld_Unq_Code?

They are both strings.

"yPNameX" in Me._clsInfo.yPNameX is an ArrayList .

Tom Dacon asked for the entire code in another message:

' DOESNT WORK
For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
If itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code Then <<<
Dim b1 As Integer = 0 ' <<< so I can set a breakpoint
End If
Next
Next

' WORKS
For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
Dim sdbFld_Unq_Code As String = itm.dbFld_Unq_Code <<< REQUIRES FORCED
reading of class
For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
Dim sitmHTML_dbFld_Unq_Code As String = itmHTML.dbFld_Unq_Code <<<
REQUIRES FORCED reading of class
If sdbFld_Unq_Code = sitmHTML_dbFld_Unq_Code Then
Dim b1 As Integer = 0 ' <<< so I can set a breakpoint
End If
Next
Next

It does seem like either vb.Net requires us to FORCE read the class "itm"
that
is stored in the yPNameX (ArrayList) into temporary variables in order to
get
to the contents of "itm".

itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code SHOULD WORK but doesn't.

I would definitedly call this a BUG and not a feature. (Unless someone
can explain.)



Family Tree Mike said:
What datatypes are itm.dbFld_Unq_Code and itmHTML.dbFld_Unq_Code?



I asked about the type, because you were essentially casting to a string by
creating a variable.

The following code is essentially equivalent to what you are trying to do,
and is a complete code set that runs as a console app. Please try and
create a complete code that fails as you describe. This code below works as
expected showing the strings that match. While you did provide a code
snippet, it cannot be run without knowledge of the rest of your code.


Module Module1

Sub Main()

Dim list1 As String() = {"Red", "Richmond", "Florida"}
Dim list2 As String() = {"Orange", "Richmond", "Ohio"}

For Each s1 As String In list1
For Each s2 As String In list2
If s1 = s2 Then
Console.Out.WriteLine(s1 & "->" & s2)
End If
Next
Next

Console.In.ReadLine()
End Sub

End Module
 
J

John Talli

In essence, this is EXACTLY what is being done. But the ArrayLists (List1 & List2) are
in two seperate classes. Then in a third class I am using a loop to compare the two
ArrayLists. In your code below, "item1.MyString = item2.MyString" is what is not
working. (But putting each property into a seperate VARIABLE and comparing them does
work.)

I will strip the existing classes (they are too big to post on here) to the bare minimum
to show you all. I appreciate this discussion and although I have a workaround, I would
like to get to the bottom of this because something doesn't seem to be proper.

THANKS to all that are discussing this.


Patrice said:
Tom likely meant a self contained proof of concept including variable declaration. For
example if I try to reproduce your code from what I know (are you using VB 2008 ?) :

Public Class Program
Class MyData
Public MyString As String
End Class
Shared Sub Main()
Dim List1 As New ArrayList
Dim List2 As New ArrayList
List1.Add(New MyData With {.MyString = "A"})
List1.Add(New MyData With {.MyString = "B"})
List2.Add(New MyData With {.MyString = "A"})
List2.Add(New MyData With {.MyString = "B"})
For Each item1 As MyData In List1
For Each item2 As MyData In List2
If item1.MyString = item2.MyString Then
Debug.WriteLine(item1.MyString & "=" & item2.MyString)
Else
Debug.WriteLine(item1.MyString & "<>" & item2.MyString)
End If
Next
Next
End Sub
End Class

gives as expected :

A=A
A<>B
B<>A
B=B

It shows you can compare those strings directly without having to copy them in a
variable. So you likely still have something else in your code that differs from my
repro... It could be also something such as not going into the loop at all etc (i.e.
the breakpoint is good but printing out values also to make sure the loop is
entered)... If you provide us with the smallest amount of code that show us the
problem :
- either when creating this code you'll find by yourself what is wrong in your
original code
- or we'll be able to cut/paste your code and run instantly here to find if we have
the same problem and hopefully find the culprit
 
C

Chris Dunaway

DOESNT WORK
1 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
2 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
3 If itm.dbFld_Unq_Code = itmHTML.dbFld_Unq_Code Then

WORKS
5 For Each itm As Info.fld_Info In Me._clsInfo.yPNameX
6 Dim sdbFld_Unq_Code As String = itm.dbFld_Unq_Code
7 For Each itmHTML As ASPnet_HTML_Field In
Me._clsASPnet_HTML_Fields.yASPnet_HTML_Fields
8 Dim sitmHTML_dbFld_Unq_Code As String = itmHTML.dbFld_Unq_Code
9 If sdbFld_Unq_Code = sitmHTML_dbFld_Unq_Code Then

Can you show us the declaration of Info.fld_Info class? What type is
yPNameX? Can you show us the ASPnet_HTML_Field class?

Without knowing for sure, I'm guessing that in the code that doesn't
work, on line 3 you are comparing object references so they are not
equal, but in the code that works, you are coercing them to strings
and then comparing them so they then are equal. I'm not sure. Show
us the declarations for the classes and we can probably help more. It
is unlikely that there is a bug.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top