I have been a VB developer for couple of years, but strictly using functional programming paradigms — I just started using object-oriented paradigms a few days ago, and want some advice on how to improve.
I'm writing a small calculator application in order to understand VB.NET object-oriented principles. The program is (currently) working perfect the way I've done it (following some books and tutorials), and I want to get some response on how I'm doing regarding following OOP principles, paradigms and practices.
The program flow is pretty simple, and as follows:
- The user enters the first number to add;
- The user enters the second number to add;
- The user clicks on the "calculate" button;
- The UI (through
Label2
) updates to show the result;
The code is mostly done through the main class Cls_addition
:
Public Class Cls_addition
'Variable de Classe
Private term1 As Integer
Private term2 As Integer
Private resultat As Long
'Properties
Public Property termA() As Integer
Get
Return term1
End Get
Set(ByVal value As Integer)
term1 = value
End Set
End Property
Public Property termB() As Integer
Get
Return term2
End Get
Set(ByVal value As Integer)
term2 = value
End Set
End Property
'Methods
Public Function additioner(ByVal term1 As Integer, ByVal term2 As Integer) As Long
resultat = term1 + 0 + term2
Return resultat
End Function
End Class
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim totalAddition As New Cls_addition 'Creation d'un obejct (creation nouvelle instance d'un obeject a l'aide du mot clé "New")
totalAddition.termA = TextBox1.Text.Trim 'Object.Proprieté
totalAddition.termB = TextBox2.Text.Trim 'Object.Proprieté
Label2.Text = totalAddition.additioner(totalAddition.termA, totalAddition.termB)
End Sub
And I have also attached a picture of the form itself:
1 Answer 1
Welcome to the world of OOP — hopefully there are a few things we can teach you. ;)
I'm going to show you an alternative implementation first, to help you see how OOP truly should be used, then I'll discuss your specific issues.
When building a calculator it's hard to effectively build something dynamic, you are always concerned about how things should relate (for example, how do we represent \$a+b+c\,ドル as \$(a + b) + c\,ドル as \$a + (b + c)\,ドル or some other way?), but it's often easiest if you consider each term and operation on it's own.
Let's take \$+\,ドル for example, we should have two terms: a left and a right. But what is a term?
A term can be any of the following:
- A value (\1ドル\,ドル for example);
- Another expression / operation (\$a + b\,ドル for example);
What do they have in common? They all have a Value
property. So, in order to represent this we start with an interface:
Public Interface ITerm
Property Value As Double
End Interface
Pretty simple, and we'll define a ConstantTerm
:
Public Class ConstantTerm
Implements ITerm
Public Sub New(value As Double)
Me.Value = value
End Sub
Public Property Value As Double Implements ITerm.Value
End Class
So far so good, we have a ConstantTerm
which is just set to a value, so we can now build an AdditionTerm
:
Public Class AdditionTerm
Implements ITerm
Public Sub New(left As ITerm, right As ITerm)
Me.Value = left.Value + right.Value
End Sub
Public Property Value As Double Implements ITerm.Value
End Class
So now, to do our math, we just need to do:
Dim result As Double = _
New AdditionTerm( _
New ConstantTerm(CDbl(TextBox1.Text.Trim)), _
New ConstantTerm(CDbl(TextBox2.Text.Trim))).Value
But even better: this sets us up for success later when we want to add a SubtractionTerm
:
Public Class SubtractionTerm
Implements ITerm
Public Sub New(left As ITerm, right As ITerm)
Me.Value = left.Value - right.Value
End Sub
Public Property Value As Double Implements ITerm.Value
End Class
And it also sets us up for parsing things later on down the road. (Since we don't care about values, we only care about terms, a parser just has to define a term as it goes and supply them as arguments to the next term.)
Regarding your specific code, there are a few things to take note of:
- In .NET we pretty much always try to name classes, interfaces, enums, and properties
PascalCase
— instead ofCls_addition
we would useClsAddition
; When you set
totalAddition.TermA
andTermB
, you should try to use a safer parsing system, via{NumericType}.TryParse
:Dim value As Integer = 0 If Integer.TryParse(TextBox1.Text.Trim(), value) Then 'Parse was successful Else 'Show the user an error message, perhaps End If
Always name things what they mean, and not what they are:
TextBox1
is a bad name, it should befirstTermInput
, etc.;- More recent versions of VB.NET support automatically-implemented properties, such as
Property TermA As Integer
, instead of manually specifying aGet
ter andSet
ter;
Overall, good start, and I hope you continue to learn and improve as you experiment and continue. :)
Explore related questions
See similar questions with these tags.