kyaqb kyaqb - 1 year ago 62 Question

VB.Net readonly property being automatically initialised without Get code execution

I don't understand this, please help. Here is my code:

Class MyCookie
Private _CookieName As String

ReadOnly Property CookieName As String
If String.IsNullOrWhiteSpace(_CookieName) Then _CookieName = "Test"
Return _CookieName
End Get
End Property
End Class

I put a breakpoint inside the property Get procedure. When I initialise MyCookie class, MyCookie.CookieName already has "Test" in it but the breakpoint is never hit!

What am I missing?

Answer Source

What other value would the debugger display for that property? The debugger has to execute the property to get a value to display in the IDE but your application hasn't executed the property so the breakpoint isn't activated. Breakpoints are only activated by YOU executing the code, not the IDE. If you were to use the debugger to watch the _CookieName field instead of the CookieName property then you'd see exactly what you would expect to see.