John Adams
John Adams

Reputation: 4823

breakpoints not hit in vb.net property setter

I'm using Visual Studio 2008 and I cannot understand why I can stop at breakpoints in my code but breakpoints in my property setter(s) are not respected (does not step into the property setter). Here is an abbreviated snippet of my class code:

        Dim objReader As SqlDataReader = cmd.ExecuteReader()
        Do While objReader.Read()
            SetObjectData(objReader)
        Loop
        objReader.Close()
        connection.Close()
    Catch ex As Exception
        Throw
    End Try
End Sub
Private Sub SetObjectData(ByVal theObjReader As SqlDataReader)
    Try
        Me._SubscriberID = Convert.ToInt32(theObjReader("SubscriberID"))
        Me._CompletedDate = theObjReader("CompletedDate")
        Me._AcknowledgeDate = theObjReader("AcknowledgeDate")
    Catch ex As Exception
        Dim msg As String
        msg = ex.ToString
    End Try
Private _CompletedDate As Nullable(Of Date)
Private _AcknowledgeDate As Nullable(Of Date)
Public Property CompletedDate() As Date?
    Get
        Return _CompletedDate
    End Get
    Set(ByVal value As Date?)
        If value.HasValue Then
            _CompletedDate = value
        Else
            _CompletedDate = Nothing
        End If
    End Set
End Property
Public Property AcknowledgeDate() As Date?
    Get
        Return _AcknowledgeDate
    End Get
    Set(ByVal value As Date?)
        If value.HasValue Then
            _AcknowledgeDate = value
        Else
            _AcknowledgeDate = Nothing
        End If
    End Set
End Property

Both of the dates can be null in the SQL server table. In my test data, CompletedDate is a valid date and AcknowledgeDate is Nothing. The code above seems to work and I can step through the constructor but cannot step into the property setter code.

EDIT: Here are the breakpoints:

---------------------------------- EDIT no. 2: --------------------------------------------------------

I am now able to see with slightly different test data that something is throwing a System.InvalidCastException on the following line in the constructor:

Me._CompletedDate = theObjReader("CompletedDate")

The datetime column in the database table is in fact NULL. I thought the property setter code could handle this case but I am getting this:

System.InvalidCastException was caught
  Message="Specified cast is not valid."
  Source="TCBCommon"
  StackTrace:
     at TCBCommon.EventItem.SetObjectData(SqlDataReader theObjReader) in C:\Projects\CorporateBrain\TCB-Common\TCBCommon\EventItem.vb:line 48
 InnerException: 

So, I guess the real problem is incorrect handling of null datetime columns as Date datatypes. I will research that but still would like to be able to step into the property setter and "experience" the .HasValue testing on a nullable object.

Upvotes: 0

Views: 572

Answers (2)

Chris Dunaway
Chris Dunaway

Reputation: 11216

It seems to me that you're not calling the setter. The line you posted

Me._CompletedDate  = theObjReader("CompletedDate") 'note the underscore) 

is trying to directly set the value of the private field. If you want the property setter to be executed, you need to actually set the property:

Me.CompletedDate = theObjReader("CompletedDate")  'no underscore

Upvotes: 1

Blorgbeard
Blorgbeard

Reputation: 103447

"I can step through the constructor but cannot step into the property setter" leads me to believe your breakpoint is not actually in the setter, but you're trying to step into it from outside. In that case, there is a Visual Studio setting you may be interested in:

screenshot

If your breakpoint is actually inside the setter code, then this may not be your problem.

Upvotes: 1

Related Questions