I am trying to setup a few fixes in some code that have caught me out, and am trying to get some exceptions to spit out if the developer trys to access a property where rules haven't been met.
nb. Lots of this class has been omitted for Clarity.
myobject = new POSTerminalList(mCommonManagers)
' This should throw an error which it does but is out of scope.
Log.Write(myobject.Current.Description)
Currently I'm throwing an exception, but this loses the calling function (which is really where the problem exists).
Is there some way to protect a property like this so that if the developer incorrectly uses the property, that you know where to look rather than in this class.
Whilst this code is VB.Net i'm interested in the Design approach rather than language so a C# implemention would also work for me.
<DebuggerDisplay("{mCode}", Name:="{mDescription}")> _
Public Class POSTerminalList
Inherits SortedList(Of Integer, POSTerminal)
Private mCurrentTerminal As POSTerminal
Private mCurrentTerminalNumber As Integer
Private Sub New()
mFullList = New SortedList(Of Integer, POSTerminal)
End Sub
Public Sub New(ByVal theManagers As IPSBusiness.CommonManagers)
Me.New()
End Sub
''Internal thread that is assessing things and calling this method.
Private Sub SetCurrentTerminal()
If mCurrentTerminalNumber = 0 Then
mCurrentTerminal = Nothing
Else
If Me.ContainsKey(mCurrentTerminalNumber) Then
mCurrentTerminal = me.Item(mCurrentTerminalNumber)
Else
mCurrentTerminal = Nothing
End If
End If
End Sub
Public ReadOnly Property Current() As POSTerminal
Get
If mCurrentTerminal Is Nothing Then
Throw New POSTerminalMissingException(
"Attempt to use the Current Terminal without a " +
"valid current terminal. This is a developer error.")
Return mCurrentTerminal
End Get
End Property
End Class