Question New to operator overloading. Am I doing it right?

munkyeetr

Member
Joined
Sep 28, 2007
Messages
15
Location
Canada
Programming Experience
Beginner
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:
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.
 
I would
  • define it as a Structure, not a Class,
  • implement Parse/TryParse methods for String conversions, and remove String constructor, but also add CType operator for String,
  • override ToString method, and remove Numeral property.
  • avoid math operators between String operands.
 
Thanks for the input.

Here's a Pastebin of my class file RomanNumeral.vb
The strings are parsed and evaluated by an internal function [ ps_MalformedNumeral ] that is called by the constructor and Property Set. Correct me if I am wrong, but construction and assignments like the following wouldn't be applicable to TryParse because it tries to create an Integer out of a string of numeric digits, no?:
VB.NET:
Dim rn as RomanNumeral = New RomanNumeral("XXII")  ' Create a RN with a value of 22
rn.Numeral = "CCCLV"                               ' Reassign to 355

After I get the operator overloads finished I would like to post the class in a separate thread to get feedback and suggestions.
Thanks for looking and please correct me if i am wrong in my assumptions.

Munky
 
Last edited:
Consider the BigInteger structure. It has Parse and TryParse methods for conversion of Strings but no constructor that accepts a String argument. I would also agree that a structure is more appropriate than a class. In that case, setting the Numeral property wouldn't make sense because creating a new value would have the same effect. Notice that all numeric types in the Framework are structures, as are various other small types that represent values, e.g. DateTime and Boolean.
 
Consider the BigInteger structure. It has Parse and TryParse methods for conversion of Strings but no constructor that accepts a String argument. I would also agree that a structure is more appropriate than a class. In that case, setting the Numeral property wouldn't make sense because creating a new value would have the same effect. Notice that all numeric types in the Framework are structures, as are various other small types that represent values, e.g. DateTime and Boolean.
I will take a look at that. Thanks.
 
Back
Top