473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is this a bug I see before me, or an incomplete understanding of scope?

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

Dec 7 '06 #1
5 1206
The lifetime of a block variable is still the entire procedure, not just
the block. It is only created once, when the procedure is entered, not when
the block is entered. The scope of the block variable is the block; you can't
access it outside of the block. The following code shows how the value of
x lasts the entire lifetime of the procedure. x will take on the values 1
through 6 in each message box.

For outer As Integer = 1 To 2
For counter As Integer = 1 To 3
Dim x As Integer
x += 1
MsgBox(x)
Next counter
Next outer

This fact of a block variable having a lifetime for the whole procedure is
documented in the Visual Studio documentation. Any assignment you attach
to the Dim statement is processed each time it is encountered.

-----
Tim Patrick - www.timaki.com
Start-to-Finish Visual Basic 2005
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

Dec 7 '06 #2
I don't know if it's a bug or not. My guess is the compiler is optimizing
this and moving the variable declaration outside the loop.

However, I would say the real problem is that you are declaring variables
inside a loop. This makes code extremely hard to read and figure out what is
going on. There are if statements, then all of a sudden variable
declarations, etc. Someone reading this code (like me) is likely to going
to have a hard time figuring out what it was your code was trying to
accomplish here and under what circumstances.

You are much better off declaring all your variables outside the loop and
making it as straight forward as possible to see what the purpose of the
loop is.

<te********@hotmail.comwrote in message
news:11********************@f1g2000cwa.googlegroup s.com...
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

Dec 7 '06 #3

Marina Levit [MVP] wrote:
I don't know if it's a bug or not. My guess is the compiler is optimizing
this and moving the variable declaration outside the loop.

However, I would say the real problem is that you are declaring variables
inside a loop. This makes code extremely hard to read and figure out what is
going on. There are if statements, then all of a sudden variable
declarations, etc. Someone reading this code (like me) is likely to going
to have a hard time figuring out what it was your code was trying to
accomplish here and under what circumstances.

You are much better off declaring all your variables outside the loop and
making it as straight forward as possible to see what the purpose of the
loop is.
Absoloopy. With honour and pride I can put up my hands and say "NOT MY
CODE!" I was just engaged to maintain it.

Edward

Dec 8 '06 #4

te********@hotmail.com wrote:
Is this a bug?
<snip>
Do While dr.Read
Dim ClubDay As New ChildrensClubDay
With ClubDay

Dim MPInfo As PaymentInformation
<snip>
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?
<snip>

Nope. This is allways a source of misunderstanding, even though it's
well documented.
If you use block variables, initialize them (unless you want such side
effects, as preserved values between cycles).

See:
http://msdn2.microsoft.com/en-us/library/1t0wsc67.aspx

For a lengthier discussion of the issue and its impact in the
<trollbait>upcoming version of VB</trollbait>, read:

http://www.panopticoncentral.net/arc.../28/11552.aspx

Regards,

Branco.

Dec 8 '06 #5

"Branco Medeiros" <br*************@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
>
te********@hotmail.com wrote:
>Is this a bug?
<snip>
>Do While dr.Read
Dim ClubDay As New ChildrensClubDay
With ClubDay

Dim MPInfo As PaymentInformation
<snip>
>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?
<snip>

Nope. This is allways a source of misunderstanding, even though it's
well documented.
If you use block variables, initialize them (unless you want such side
effects, as preserved values between cycles).

See:
http://msdn2.microsoft.com/en-us/library/1t0wsc67.aspx

For a lengthier discussion of the issue and its impact in the
<trollbait>upcoming version of VB</trollbait>, read:

http://www.panopticoncentral.net/arc.../28/11552.aspx

Regards,

Branco.
Thanks. The trollbait tags made me laugh out loud after
a particularly difficult day.

Robin S.
Dec 9 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: oliver.lin | last post by:
In my simple test code, I tried to define my constructor outside of the class declaration headr file. The header file: file_handler.h ============================================================...
5
by: Paul F. Dietz | last post by:
Is the following legal C? struct foo; struct foo (*p); /* Pointer to array of 10 foo structures */ struct foo { int bar; int baz; }; main() { printf("%d\n", sizeof(*p)); } Paul Dietz...
24
by: ark | last post by:
Hello group, Could you help me with this: static const int x; ............ something ............. static const int x = 17; It looks perfectly legal to me but MSVC/C++ 6.0 gives, on the...
54
by: rohit | last post by:
main() { int i; int marks = {55,65,75,56,78,90}; for (i = 0; i <= 6; i++) disp(&marks); }
2
by: Halid Umar A M | last post by:
Dear All, Please tell me why this error is occuring. The following is the code snippets which i have typed. struct mystructure{ struct list_head m; //error: field m has incomplete...
2
by: gk245 | last post by:
I have something like this: #include <stdio.h> #include <ctype.h> #include <strings.h> struct sentence get_sentence (char string) { struct sentence my_sentence {
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
1
by: Bharath | last post by:
Hello everyone, I was going through templates section at Bjarne Stroustrup's "C++ Programming Language" (13.2.1 Defining a Template ). I couldn't understand some part of it. Need your help in...
5
by: mike.polyakov | last post by:
I have trouble understanding incomplete types and their interplay with shared_ptr. Consider the following code, composed of two source files and one header: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.