Hi All,

Apologies if this has been mentioned before, but haven't come across anything from my search...

I have a strange issue at the moment where when I debug and step through my code, as soon as the first declaration (DIM statement) is stepped over, the value is being set to something that doesn't exist! This is even before it has got to the code that sets the variable :-(

Now I can see when stepping through that it highlights the section afterwards, so it is almost like it is doing the next step when it carries out the initial declaration, which is fine, but it is setting the value to a value that is different to what my code says!

I have come to the conclusion that it is "holding" something in memory, as the value being set "used" to be the value before I changed it, so it isn't a completely random value...

I did find some reference to strange behaviours with this and it was acknowledged by MS as a bug which was addressed in Update 1, however I have installed that update and it is still the same.

If I run the executable, I can see that the correct values are reflected, so this does seem to be an issue when debugging.

Maybe I have some setting somewhere that "holds" on to values in memory??

Anyone else had this, and know what I need to do to fix?

Thanks

Andrew

Recommended Answers

All 7 Replies

Can you show the code and the values at various stages so we can try it for ourselves?

Hi Jim,

Thanks for the reply. There is quite a lot of code to paste, but I have done a screenshot showing the start of the function.

82fea50b65147125fae66b6039083b96

I just stepped through then to take the screenshot and hovered over the variable before it stepped in to the Dim statements and all the variables were 0. As soon as it stepped through the first Dim, it set the value of startPosX to 12 (which at that point should be 0) although lower down, it does set to 12. But at this point before even stepping through startPosY, all of the Dim statements show the values set and startPosY is set to 200, when in fact, it is coded to set to 250. Here is another screenshot showing both Dim and where i set the values.

0eb9df0c35de4c4855b25f127ccc71d7

It is also very weird as when stepping through all the code, it randomly decides to skip lines (or so it would appear) and misses out chunks of code, yet I can see that it has run it, as it has carried out the actions.

I have stepped through right from the beginning to make sure that it isn't being set elsewhere, I as I say, I have hovered over just before it carried out the Dim section and verified that the value wasn't set (well, it was set to 0) before it started on that Dim section.

Even restarted my PC in case something was stuck in memory or a stack somewhere!

It is driving me insane :-(

Thanks

Andrew

Also, just to show the end result and difference between debugger and running as an exe, here are the screenshots of the form itself...

Using the debugger...

8aeff03368eb28f0bb424fb9b2426889

Using the executable...

d9e9dbdddf5dc322750f1b60ea81c27b

So you can see the effects of the labels are reflecting the value of 250 which is what I changed it to to bring the labels lower, but in debugger it still insists on retaining what USED to be set before I changed to 250.

Thanks

Andrew

Here is the code for this particular section...

` Private Sub getData()

    ' This function configures and displays the label(s) for each team that is currently on-call.
    ' It extracts the data from the SQL database in to a DataTable so that the values of the
    ' person on-call for each team can be pulled in to each label.  The query pulls out the team,
    ' team member and schedule details by querying the current date/time and examing who is
    ' on-call at the current time for each team.

    ' Holds data retrieved from database

    Dim retDataTable As New DataTable

    ' Hold all of the new labels

    Dim lblList As New List(Of Label)

    Dim rowCount As Integer = 0

    ' Declare Label positions and sizes

    Dim startPosX
    Dim startPosY = 0
    Dim labelWidth = 0
    Dim labelHeight = 0
    Dim gapToLeave = 0

    startPosX = 12
    startPosY = 250
    labelWidth = 110
    labelHeight = 110
    gapToLeave = 6

    ' Declare values to be used in the label from the query

    Dim teamName As String = ""
    Dim teamMemberFirstName As String = ""
    Dim teamMemberSurname As String = ""
    Dim teamMemberMobile As String = ""
    Dim teamMemberLocation As String = ""

    ' Create SQL Connection and query using SQL Command based on Connection String

    Console.WriteLine("Connection String: " + SQLExpressDB.connectStr)

    Using cn As New SqlConnection(SQLExpressDB.connectStr)

        Dim sqlText As String = String.Empty

        sqlText = "SELECT Team.name as 'TeamName', TeamMember.firstname, TeamMember.Surname, TeamMember.onCallPhone, TeamMember.Location, Schedule.Start, Schedule.finish "
        sqlText += "FROM Team INNER JOIN TeamMember ON Team.teamID = TeamMember.teamID INNER JOIN Schedule ON TeamMember.employeeID = Schedule.TeamMemberEmpID "
        sqlText += "WHERE (Schedule.start < { fn NOW() }) AND (Schedule.finish > { fn NOW() })"

        Try

            ' Open Connection

            cn.Open()

            ' Use SqlDataAdapter to get data from query

            Using da As New SqlDataAdapter(sqlText, cn)

                Try

                    ' Use SqlDataAdapter to store data from query

                    da.Fill(retDataTable)

                Catch ex1 As Exception

                    Throw New Exception("Error in dataAdapter. " + ex1.Message)

                End Try

                ' Initialise rowCount

                rowCount = 0

                For Each row As DataRow In retDataTable.Rows

                    ' Keep track of number of rows retrieved

                    rowCount += 1

                    ' Set values to those from the DataTable

                    teamName = row("TeamName").ToString()
                    teamMemberFirstName = row("firstName").ToString()
                    teamMemberSurname = row("surname").ToString()
                    teamMemberMobile = row("onCallPhone").ToString()
                    teamMemberLocation = row("location").ToString()

                    Console.WriteLine("TeamName: " & teamName)
                    Console.WriteLine("firstName: " & teamMemberFirstName)

                    ' Declare variable for new label

                    Dim lblTeam As New Label

                    ' Initialise the Text property of the new label

                    lblTeam.Text = Nothing

                    With lblTeam

                        ' Set the label properties

                        .Name = "lblTeam_" & teamName
                        .Font = New Font(lblTeam.Font.FontFamily, 10)
                        .Size = New System.Drawing.Size(labelWidth, labelHeight)
                        .Location = New System.Drawing.Point(startPosX, startPosY)
                        .ForeColor = System.Drawing.Color.Black
                        .Text = teamName & vbCrLf & teamMemberFirstName & " " & teamMemberSurname & vbCrLf & teamMemberMobile & vbCrLf & teamMemberLocation
                        .AutoSize = False
                        .BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D

                        ' Keep track of labels that were created and add lblTeam to list

                        lblList.Add(lblTeam)

                    End With

                    ' Set up the starting position for the next label

                    startPosX = startPosX + labelWidth + gapToLeave

                    ' Initialise/clear all variables

                    teamName = Nothing
                    teamMemberFirstName = Nothing
                    teamMemberSurname = Nothing
                    teamMemberMobile = Nothing
                    teamMemberLocation = Nothing

                Next

            End Using

        Catch ex As Exception

            ' Prevents lots of error messagebox windows if an error occurs

            errorCount += 1

            If errorCount < 2 Then

                System.Windows.Forms.MessageBox.Show("Error (Open Connection): " & ex.Message & System.Environment.NewLine & System.Environment.NewLine & "Connection String: " & System.Environment.NewLine & SQLExpressDB.connectStr, "Error - Select", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End If

        End Try

    End Using

    ' Compare lists to see if anything has changed.  Removing and re-drawing the labels ONLY when there are changes will reduce
    ' the re-drawing frequency of the labels and minimise the flashing effects that re-drawing creates.

    If compareLabelLists(previousLblList, lblList) = False Or previousLblList.Count <= 0 Then

        ' Remove old labels

        For Each lbl As Label In previousLblList

            Me.Controls.RemoveByKey(lbl.Name)

        Next

        ' Add labels to form

        For Each lblItem As Label In lblList

            Me.Controls.Add(lblItem)

        Next

    End If

    ' Update status

    ToolStripStatusLabel1.Text = "Status: " & rowCount & " records retrieved (last update: " & DateTime.Now & ")"

    ' Refresh the StatusStrip1 to ensure the ToolStripStatusLabel1 text is updated

    StatusStrip1.Refresh()

    ' Update previousLabelName

    previousLblList = lblList

    ' Stop the timer if errors are occurring to prevent too many messagebox messages

    If errorCount > 2 Then

        stopTimer()
        ToolStripStatusLabel1.Text = "Status: Automatic updates disabled. Click 'Tools' -> 'Refresh' to re-enable."

        ' Refresh to ensure ToolStripStatusLabel1.Text is updated

        StatusStrip1.Refresh()

    End If

End Sub`

I can't think of anything. Based on that first screen cap I can't see why startPosY has the value 200 rather than 0.

Dim startPosX As Integer
Dim startPosY As Integer = 0
Dim labelWidth As Integer = 0
Dim labelHeight As Integer = 0
Dim gapToLeave As Integer = 0
startPosX = 12
startPosY = 250
labelWidth = 110
labelHeight = 110

Try like this.!

Hi Sulaiman 1,

Yeah, it was originally defined like that. In fact, I originally had it defined like

Dim startPosX As Integer = 12 etc etc and only split it all up and then removed the Integer after it started behaving weirdly.

I can only think that I have some sort of setting defined that is not releasing old values, or there is some other code behind that might be generated that you don't see when stepping through that is overriding it.

Is there a way to show ALL code including such code generated by VS? I guess it has to be somewhere, either inmemory or internal code, as like I say, it used to be set to 200.

But, and this is now really crazy!! It was also doing the same with the other values. Previously i changed the width and height to 120 and it also retained the old 110 setting BUT I have just loaded this again now to take screenshots of that, and I changed them to 120, and when i stepped through the code, the X and Y variables set to 0!!!! and then when i stepped through the next bit, they are now showing the right values.

I have absolutely no idea why, as I have done nothing to this other than just load VS up again (which I had also done several times before including restarting my PC) The only thing I can think of now is that I had some breakpoints set yesterday, which I took off whilst I ran a full debug to capture the screenshot of the form, and therefore the only thing that has changed is removing those and closing down VS, so VS has started just now without any breakpoints selected. So maybe breakpoints were making it go a bit strange. And of course the only other thing is that I now just changed width and height to 120....could that really have made it correct itself for everything else? I am favouring the breakpoints at the moment :-)

I now notice that every step is being performed and not skipped and it is no longer going over blank lines when stepping through.

So appears to be solved but still not sure why or what caused it :-(

Anyway, I will keep my eye on it and try and see if I can manage to reproduce the behaviour to see what causes it.

Thanks all for your replies.

Andrew

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.