-
Aug 26th, 2014, 10:51 AM
#1
Thread Starter
Lively Member
Moving code from A Form to a Class
Basically I am working on a calculator in Visual basic. I need to move the code from the Form to the class according to my tutor. However I am unsure on how to do this.
This is the code in my Form:
Code:
Option Strict On
Option Explicit On
Public Class frmCalculator
Private _operatorsAsStrings As List(Of String)
Private _numbersAsStrings As List(Of String)
Private _numbers As List(Of Double)
Dim Multiple1, Multiple2 As Single
Private _calculator As Calculator
Private Sub frmCalculator_Load(sender As Object, e As System.EventArgs) Handles Me.Load
_calculator = New Calculator
End Sub
Private Sub NumberButton_Click(sender As System.Object, e As System.EventArgs) Handles btnNumber0.Click, btnNumber1.Click, btnNumber2.Click, btnNumber3.Click, btnNumber4.Click,
btnNumber5.Click, btnNumber6.Click, btnNumber7.Click, btnNumber8.Click, btnNumber9.Click
If tbDisplay2.Text.Contains("=") Then
ClearDisplay()
End If
Dim stringToAddToDisplay As String
stringToAddToDisplay = CType(sender, Button).Text
tbDisplay.AppendText(stringToAddToDisplay)
End Sub
Private Sub btnAddition_Click(sender As System.Object, e As System.EventArgs) Handles btnAddition.Click, btnSubtraction.Click, btnMultiply.Click, btnDivide.Click
Dim stringToAddToDisplay As String
Dim lastEntryOnDisplay As String
stringToAddToDisplay = CType(sender, Button).Text
lastEntryOnDisplay = Strings.Right(tbDisplay.Text, 1)
If IsNumeric(lastEntryOnDisplay) Then
tbDisplay.AppendText(stringToAddToDisplay)
tbDisplay2.AppendText(tbDisplay.Text)
End If
tbDisplay.Clear()
End Sub
Private Sub btnClearAll_Click(sender As Object, e As EventArgs) Handles btnClearAll.Click
ClearDisplay()
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If tbDisplay.Text.Length <> 0 Then
tbDisplay.Text = tbDisplay.Text.Substring(0, tbDisplay.Text.Length - 1)
End If
End Sub
Private Sub ClearDisplay()
tbDisplay.Clear()
tbDisplay2.Clear()
End Sub
Private Sub btnDecimalPlace_Click(sender As Object, e As EventArgs) Handles btnDecimalPlace.Click
If InStr(tbDisplay.Text, ".") = 0 Then
tbDisplay.Text = tbDisplay.Text & "."
End If
End Sub
Private Sub btnSquareRoot_Click(sender As Object, e As EventArgs) Handles btnSquareRoot.Click
Dim number As Double
Dim rootOfNumber As Double
number = CDbl(tbDisplay.Text)
rootOfNumber = CDbl(_calculator.SquareRoot(number))
tbDisplay.AppendText(tbDisplay2.Text)
tbDisplay.Clear()
tbDisplay.Text = Format(rootOfNumber, "####.###")
If tbDisplay2.Text.Contains("sqr") Then
tbDisplay2.Clear()
tbDisplay.AppendText(tbDisplay2.Text)
End If
End Sub
Private Sub btnPositiveOrNegative_Click(sender As Object, e As EventArgs) Handles btnPositiveOrNegative.Click
If Not Strings.Left(tbDisplay.Text, 1).Contains("-") Then
tbDisplay.Text = "-" + tbDisplay.Text
Else
tbDisplay.Text = tbDisplay.Text.Replace("-", "")
End If
End Sub
Private Sub btnSquared_Click(sender As Object, e As EventArgs) Handles btnSquared.Click
Dim number As Double
Dim SquareOfNumber As Double
number = CDbl(tbDisplay.Text)
SquareOfNumber = CDbl(_calculator.Square(number))
tbDisplay.Text = Format(SquareOfNumber, "####.##")
If tbDisplay2.Text.Contains("sqr") Then
tbDisplay2.Clear()
tbDisplay.AppendText(tbDisplay2.Text)
End If
End Sub
Private Sub btnMemoryClear_Click(sender As Object, e As EventArgs) Handles btnMemoryClear.Click
_calculator.MemoryClear()
ClearDisplay()
End Sub
Private Sub btnMemoryRecall_Click(sender As Object, e As EventArgs) Handles btnMemoryRecall.Click
Dim number As Double
number = _calculator.Memory
tbDisplay.Text = number.ToString
End Sub
Private Sub btnMemorySet_Click(sender As Object, e As EventArgs) Handles btnMemorySet.Click
Dim number As Double
number = CDbl(tbDisplay.Text)
_calculator.Memory = number
End Sub
Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles btnEquals.Click
tbDisplay2.AppendText(tbDisplay.Text)
_operatorsAsStrings = New List(Of String)(tbDisplay2.Text.Split(CChar(("0")), CChar(("1")), CChar(("2")), CChar(("3")), CChar(("4")), CChar(("5")), CChar(("6")), CChar(("7")), CChar(("8")), CChar(("9"))))
_numbersAsStrings = New List(Of String)(tbDisplay2.Text.Split(CChar(("/")), CChar(("*")), CChar(("+")), CChar(("-"))))
_numbers = New List(Of Double)
For iCount As Integer = _operatorsAsStrings.Count - 1 To 0 Step -1
If _operatorsAsStrings.Item(iCount).Length = 0 Then
_operatorsAsStrings.RemoveAt(iCount)
End If
Next iCount
For iCount As Integer = 0 To _numbersAsStrings.Count - 1
_numbers.Add(CDbl(_numbersAsStrings.Item(iCount)))
Next
DoCalculationsFor("/")
DoCalculationsFor("*")
DoCalculationsFor("+")
DoCalculationsFor("-")
tbDisplay2.AppendText("=" & _numbers.Item(0).ToString)
tbDisplay.Clear()
tbDisplay.AppendText(_numbers.Item(0).ToString)
End Sub
Private Sub DoCalculationsFor(OperatorForCalculation As String)
Dim calculationResult As Double
Dim positionInList As Integer
Dim firstNumber As Double
Dim secondNumber As Double
positionInList = WhereIsOperatorInList(OperatorForCalculation)
While positionInList > -1
firstNumber = GetNumber(positionInList)
secondNumber = GetNumber(positionInList + 1)
calculationResult = DoTheRightCalculation(OperatorForCalculation, firstNumber, secondNumber)
SaveCalculationResult(positionInList, calculationResult)
RemoveCalculationFromLists(positionInList)
positionInList = WhereIsOperatorInList(OperatorForCalculation)
End While
End Sub
Private Function WhereIsOperatorInList(ByVal OperatorForCalculation As String) As Integer
Dim positionInList As Integer
positionInList = _operatorsAsStrings.IndexOf(OperatorForCalculation)
Return positionInList
End Function
Private Sub RemoveCalculationFromLists(ByVal positionInList As Integer)
_numbers.RemoveAt(positionInList)
_operatorsAsStrings.RemoveAt(positionInList)
End Sub
Private Sub SaveCalculationResult(ByVal positionInList As Integer, ByVal calculationResult As Double)
_numbers.Item(positionInList + 1) = calculationResult
End Sub
Private Function GetNumber(ByVal positionInList As Integer) As Double
Dim numberFromList As Double
numberFromList = _numbers.Item(positionInList)
Return numberFromList
End Function
Private Function DoTheRightCalculation(searchOperator As String, firstNumber As Double, secondNumber As Double) As Double
Dim calculationResult As Double
Select Case searchOperator
Case "+"
calculationResult = firstNumber + secondNumber
Case "-"
calculationResult = firstNumber - secondNumber
Case "*"
calculationResult = firstNumber * secondNumber
Case "/"
calculationResult = firstNumber / secondNumber
End Select
Return calculationResult
End Function
End Class
And this is the code in my Calculator Class:
Code:
Public Class Calculator
Private _memory As Double
Public Property Memory As Double
Get
Return _memory
End Get
Set(value As Double)
_memory = value
End Set
End Property
Public Sub MemoryClear()
_memory = 0
End Sub
Public Function Add(Number1 As Double, Number2 As Double) As Double
Add = Number1 + Number2
End Function
Public Function Subtract(Number1 As Double, Number2 As Double) As Double
Subtract = Number1 - Number2
End Function
Public Function Multiply(Number1 As Double, Number2 As Double) As Double
Multiply = Number1 * Number2
End Function
Public Function Divide(Number1 As Double, Number2 As Double) As Double
Divide = Number1 / Number2
End Function
Public Function SquareRoot(Number1)
SquareRoot = Math.Sqrt(Number1)
End Function
Public Function Square(Number1)
Square = Number1 * Number1
End Function
End Class
How would I first move the code?
Secondly, how would I call up that code to do the calculation?
Any help is greatly appreciated!
Thanks
-
Aug 26th, 2014, 11:08 AM
#2
Re: Moving code from A Form to a Class
Inside your functions, rather than setting a value such as:
Code:
Add = Number1 + Number2
Actually return those values:
Code:
Return Number1 + Number2
To call the calculations, you'll need to declare a new instance of your calculator and then call any functions:
Code:
Dim cal As Calculator = New Calculator
Console.WriteLine(cal.Add(1, 2).ToString)
Console.WriteLine(cal.Subtract(1, 2).ToString)
'Etc...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|