I am creating a RomanNumeral class (which I will post soon for people to look at), and I currently implementing Operator Overloading so math operations can be perform using the objects. I have never done this before and would like some feedback whether I am doing or correctly or not.
Each object has the following properties (among others) that return a value:
And the class has 3 constructors:
So far I have only overloaded the + (addition) operator. and I have tried to make it so operations can be performed between multiple RN's, Integers and RN's, Strings and RN's. They work as I expect in testing so far, but I just want to see if I am making any glaring faux pas in their implementation.
Here are my current overloads:
Thanks, any input is appreciated.
Each object has the following properties (among others) that return a value:
VB.NET:
Object.Number as Integer
Object.Numeral as String ' Roman numeral representation of .Number
And the class has 3 constructors:
VB.NET:
New() ' Values are set to 0
New(number as Integer) ' Values are set to number
New(numeral as String) ' Values are set to numeral
So far I have only overloaded the + (addition) operator. and I have tried to make it so operations can be performed between multiple RN's, Integers and RN's, Strings and RN's. They work as I expect in testing so far, but I just want to see if I am making any glaring faux pas in their implementation.
Here are my current overloads:
VB.NET:
Public Shared Operator +(ByVal rn1 As RomanNumeral, ByVal rn2 As RomanNumeral) As RomanNumeral
Try
Return New RomanNumeral(rn1.Number + rn2.Number)
Catch ex As System.OverflowException
Dim rv As RomanNumeral = New RomanNumeral
rv.m_Malformation = MalformationError.ValueOverflow
Return rv
End Try
End Operator
Public Shared Operator +(ByVal rn As RomanNumeral, ByVal number As Integer) As RomanNumeral
Try
Return New RomanNumeral(rn.Number + number)
Catch ex As System.OverflowException
Dim rv As RomanNumeral = New RomanNumeral
rv.m_Malformation = MalformationError.ValueOverflow
Return rv
End Try
End Operator
Public Shared Operator +(ByVal number As Integer, ByVal rn As RomanNumeral) As RomanNumeral
Try
Return New RomanNumeral(rn.Number + number)
Catch ex As System.OverflowException
Dim rv As RomanNumeral = New RomanNumeral
rv.m_Malformation = MalformationError.ValueOverflow
Return rv
End Try
End Operator
Public Shared Operator +(ByVal rn As RomanNumeral, ByVal numeral As String) As RomanNumeral
Dim rv As RomanNumeral = New RomanNumeral(numeral)
Try
Return New RomanNumeral(rn.Number + rv.Number)
Catch ex As System.OverflowException
rv.Number = 0
rv.m_Malformation = MalformationError.ValueOverflow
Return rv
End Try
End Operator
Public Shared Operator +(ByVal numeral As String, ByVal rn As RomanNumeral) As RomanNumeral
Dim rv As RomanNumeral = New RomanNumeral(numeral)
Try
Return New RomanNumeral(rn.Number + rv.Number)
Catch ex As System.OverflowException
rv.Number = 0
rv.m_Malformation = MalformationError.ValueOverflow
Return rv
End Try
End Operator
Thanks, any input is appreciated.