PDA

View Full Version : Converting variables



Awkwardsaw
04-25-2009, 10:17 AM
i'm starting visual basics, and i'm trying to make a simple calculator. so far i have


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num1, Num2, Answer As Integer
Dim OP As String

Num1 = TextBox1.Text
Op = TextBox4.Text
Num2 = TextBox2.Text
Answer = TextBox3.Text

If Op = "+" Then
Answer = Num1 + Num2
ElseIf Op = "-" Then
Answer = Num1 - Num2
ElseIf OP = "*" Then
Answer = Num1 * Num2
ElseIf OP = "/" Then
Answer = Num1 / Num2
ElseIf OP = "^" Then
Answer = Num1 ^ Num2
End If




End Sub
End Class

and i get the error: Conversion from string "Answer" to type 'Integer' is not valid.

what do i do to convert it properly?

senrath
04-25-2009, 10:38 AM
I dunno if it'll work, but try this:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num1, Num2, Answer As Integer
Dim OP As String

Num1 = TextBox1.Text
Op = TextBox4.Text
Num2 = TextBox2.Text
Answer = TextBox3.Text

If Op = "+" Then
Answer = Str(Num1 + Num2)
ElseIf Op = "-" Then
Answer = Str(Num1 - Num2)
ElseIf OP = "*" Then
Answer = Str(Num1 * Num2)
ElseIf OP = "/" Then
Answer = Str(Num1 / Num2)
ElseIf OP = "^" Then
Answer = Str(Num1 ^ Num2)
End If




End Sub
End Class

Awkwardsaw
04-25-2009, 10:45 AM
I dunno if it'll work, but try this:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num1, Num2, Answer As Integer
Dim OP As String

Num1 = TextBox1.Text
Op = TextBox4.Text
Num2 = TextBox2.Text
Answer = TextBox3.Text

If Op = "+" Then
Answer = Str(Num1 + Num2)
ElseIf Op = "-" Then
Answer = Str(Num1 - Num2)
ElseIf OP = "*" Then
Answer = Str(Num1 * Num2)
ElseIf OP = "/" Then
Answer = Str(Num1 / Num2)
ElseIf OP = "^" Then
Answer = Str(Num1 ^ Num2)
End If




End Sub
End Class

nope, didnt work =\ thanks though

senrath
04-25-2009, 11:03 AM
I'm an idiot. I misread the code. This should work:


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num1, Num2, Answer As Integer
Dim OP As String

Num1 = Val(TextBox1.Text)
Op = TextBox4.Text
Num2 = Val(TextBox2.Text)
Answer = Val(TextBox3.Text)

If Op = "+" Then
Answer = Num1 + Num2
ElseIf Op = "-" Then
Answer = Num1 - Num2
ElseIf OP = "*" Then
Answer = Num1 * Num2
ElseIf OP = "/" Then
Answer = Num1 / Num2
ElseIf OP = "^" Then
Answer = Num1 ^ Num2
End If




End Sub
End Class

bballer
04-29-2009, 11:09 AM
instead of:



If Op = "+" Then
Answer = Num1 + Num2
ElseIf Op = "-" Then
Answer = Num1 - Num2
ElseIf OP = "*" Then
Answer = Num1 * Num2
ElseIf OP = "/" Then
Answer = Num1 / Num2
ElseIf OP = "^" Then
Answer = Num1 ^ Num2
End If


do:




select case op
case is "+"
answer = num1+num2
case is "-"
answer = num1-num2
case is "*"
answer = num1*num2
case is "/"
answer = num1/num2
case is "^"
answer = num1^num2
end select


here is a source from a school project I did last semester:


Public Class Form1

Dim operand1 As Double = 0
Dim operand2 As Double = 0
Dim op As String = Nothing
Dim newOperand As Boolean = True

'Updates the calculator display
'
'pre: Global variable newOperand is True when the current display should be changed to a
'new number and False when the current display should be updated with a new digit.
'The Tag property of the button clicked contains the number corresponding to the
'number on the button.
'post: The calculator display has been updated to show a new number or an
'additional digit.
'
Private Sub Number_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
btnDot.Click, btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, _
btn6.Click, btn7.Click, btn8.Click, btn9.Click
Dim btnNumberClicked As Button = sender
If newOperand Then
Me.txtDisplay.Text = btnNumberClicked.Tag
newOperand = False
Else
Me.txtDisplay.Text &= btnNumberClicked.Tag
End If
End Sub

'Clears the calculator display
'
'post: The calculator display has been changed to 0 and the global variables
'have been reinitialized.
'
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles btnClear.Click
Me.txtDisplay.Text = "0"
operand1 = 0
operand2 = 0
newOperand = True
op = Nothing
End Sub

'Ends the applications
'
'post: The application is ended
'
Private Sub btnOff_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOff.Click
Application.Exit()
End Sub

'Updates the value of the operands and updates the calculator display to show the result of a
'calculation if two operands have been entered
'
'post: The calculator display has been changed to show the result of a calculation if a second
'operator had been clicked, otherwise the display has not been changed.
'
Private Sub btnOperator_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlus.Click, _
btnMinus.Click, btnTimes.Click, btnDivide.Click, btnEqual.Click
Dim operatorSelected As Button = sender

'No operator previously clicked or a calculation just performed
If (operand1 = 0 And op = Nothing) Or op = "=" Then
operand1 = Val(Me.txtDisplay.Text)
Else 'second operand entered and a second operator clicked
operand2 = Val(Me.txtDisplay.Text)
operand1 = Calculate(operand1, operand2, op)
Me.txtDisplay.Text = operand1
End If

'Store operator for use when the next operator is clicked
op = operatorSelected.Tag
newOperand = True
End Sub

'Performs a calculation using two operands and an operator
'
'post: The result of a calculation has been returned
'
Function Calculate(ByVal firstOperand As Double, ByVal secondOperand As Double, _
ByVal op As String) As Double
Select Case op
Case "+"
Return (firstOperand + secondOperand)
Case "-"
Return (firstOperand - secondOperand)
Case "x"
Return (firstOperand * secondOperand)
Case "/"
Return (firstOperand / secondOperand)
End Select
End Function
End Class

Kyle Undefined
04-29-2009, 12:05 PM
This should work. I don't have VB on this computer, so I can't tell you exactly. You set Answer as an integer, when all you have to do is set it to a string and convert it. You should also do a select case like bballer said, because each "if" makes a process run and so you're making 5 process run which equals memory hog which equals inefficient code.


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Num1, Num2 As Integer
Dim OP, Answer As String

Num1 = TextBox1.Text
Op = TextBox4.Text
Num2 = TextBox2.Text
Answer = TextBox3.Text

select case Op
case is "+"
Answer = CStr(Num1 + Num2)
case is "-"
Answer = CStr(Num1 - Num2)
case is "*"
Answer = CStr(Num1 * Num2)
case is "/"
Answer = CStr(Num1 / Num2)
case is "^"
Answer = CStr(Num1 ^ Num2)
end select

End Sub
End Class

Let me know if it works.

~Camo