T
teddysnips
Is this a bug?
I open a DataReader dr from a SQL Server SPROC
Do While dr.Read
Dim ClubDay As New ChildrensClubDay
With ClubDay
Dim MPInfo As PaymentInformation
If Not dr.GetDateTime(1) = Date.MinValue Then
MPInfo = New PaymentInformation(dr.GetDateTime(1), New
IndividualKey(dr.GetString(2)))
End If
.MorningPaymentInfo = MPInfo
' Get afternoon paymentinfo
Dim APInfo As PaymentInformation = Nothing
If Not dr.GetDateTime(3) = Date.MinValue Then
APInfo = New PaymentInformation(dr.GetDateTime(3), New
IndividualKey(dr.GetString(4)))
End If
.AfternoonPaymentInfo = APInfo
End With
ChildrensClubDayList.Add(ClubDay)
Loop
I've removed a lot of the lines for conciseness. (The
PaymentInformation class shown below)
The datareader contains information about a ChildrensClubDay, with a
separate row for each day.
The way this bug expressed itself (if it is a bug) was thus. For a
particular set of data the first two rows contained no booking
information, and so
dr.GetDateTime(1) returned nothing, which did not satisfy the If
condition. Similarly, dr.GetDateTime(3) returned nothing.
On the third day, there WAS a booking, and so the conditions were met,
and the lines
MPInfo = New PaymentInformation(dr.GetDateTime(1), New
IndividualKey(dr.GetString(2)))
APInfo = New PaymentInformation(dr.GetDateTime(3), New
IndividualKey(dr.GetString(4)))
were executed leading to the lines
..MorningPaymentInfo = MPInfo
..AfternoonPaymentInfo = APInfo
being correctly populated.
HOWEVER. When the next two days were processed, the conditions were
NOT met, BUT the state of MPInfo and APInfo had persisted from the
previous iteration of the loop, thus effectively bypassing Dim MPInfo
As PaymentInformation etc.
I solved the problem by writing Dim MPInfo As PaymentInformation =
Nothing.
But surely I shouldn't have had to. Don't variables scoped in loops
get collected and destroyed on each iteration of the loop?
Jess Askin
Public Class PaymentInformation
Public PurchaseDate As Date
Public PurchasedBy As IndividualKey
Sub New()
End Sub
Sub New(ByVal purchaseDate As Date, ByVal purchaseBy As
IndividualKey)
Me.PurchaseDate = purchaseDate
Me.PurchasedBy = purchaseBy
End Sub
#Region "Helper"
Public Shared Sub GetValues(ByVal pi As PaymentInformation, _
ByRef purchaseDate As Date, ByRef
purchasedBy As String)
' Get payment info with null validation
If Not pi Is Nothing Then
purchaseDate = pi.PurchaseDate
If Not pi.PurchasedBy Is Nothing Then purchasedBy =
pi.PurchasedBy.Key
End If
End Sub
#End Region
End Class
I open a DataReader dr from a SQL Server SPROC
Do While dr.Read
Dim ClubDay As New ChildrensClubDay
With ClubDay
Dim MPInfo As PaymentInformation
If Not dr.GetDateTime(1) = Date.MinValue Then
MPInfo = New PaymentInformation(dr.GetDateTime(1), New
IndividualKey(dr.GetString(2)))
End If
.MorningPaymentInfo = MPInfo
' Get afternoon paymentinfo
Dim APInfo As PaymentInformation = Nothing
If Not dr.GetDateTime(3) = Date.MinValue Then
APInfo = New PaymentInformation(dr.GetDateTime(3), New
IndividualKey(dr.GetString(4)))
End If
.AfternoonPaymentInfo = APInfo
End With
ChildrensClubDayList.Add(ClubDay)
Loop
I've removed a lot of the lines for conciseness. (The
PaymentInformation class shown below)
The datareader contains information about a ChildrensClubDay, with a
separate row for each day.
The way this bug expressed itself (if it is a bug) was thus. For a
particular set of data the first two rows contained no booking
information, and so
dr.GetDateTime(1) returned nothing, which did not satisfy the If
condition. Similarly, dr.GetDateTime(3) returned nothing.
On the third day, there WAS a booking, and so the conditions were met,
and the lines
MPInfo = New PaymentInformation(dr.GetDateTime(1), New
IndividualKey(dr.GetString(2)))
APInfo = New PaymentInformation(dr.GetDateTime(3), New
IndividualKey(dr.GetString(4)))
were executed leading to the lines
..MorningPaymentInfo = MPInfo
..AfternoonPaymentInfo = APInfo
being correctly populated.
HOWEVER. When the next two days were processed, the conditions were
NOT met, BUT the state of MPInfo and APInfo had persisted from the
previous iteration of the loop, thus effectively bypassing Dim MPInfo
As PaymentInformation etc.
I solved the problem by writing Dim MPInfo As PaymentInformation =
Nothing.
But surely I shouldn't have had to. Don't variables scoped in loops
get collected and destroyed on each iteration of the loop?
Jess Askin
Public Class PaymentInformation
Public PurchaseDate As Date
Public PurchasedBy As IndividualKey
Sub New()
End Sub
Sub New(ByVal purchaseDate As Date, ByVal purchaseBy As
IndividualKey)
Me.PurchaseDate = purchaseDate
Me.PurchasedBy = purchaseBy
End Sub
#Region "Helper"
Public Shared Sub GetValues(ByVal pi As PaymentInformation, _
ByRef purchaseDate As Date, ByRef
purchasedBy As String)
' Get payment info with null validation
If Not pi Is Nothing Then
purchaseDate = pi.PurchaseDate
If Not pi.PurchasedBy Is Nothing Then purchasedBy =
pi.PurchasedBy.Key
End If
End Sub
#End Region
End Class