[RESOLVED] Beginner requesting help!
Hi, I'm new to visual basic and I've attempted to make a very simple calculator but I have no idea why it's not working. Of course I've done something stupid but I just can't find it.
The layout of the program is 3 text boxes:
FirstNumber
Operation
SecondNumber
1 Button:
Answer
1 Label:
Answer
The idea is to type in the first number, the operation (*/+-) and the second number, clicking the answer button. Then the label should hold the answer. This is my code:
Dim answerr, operation As String
Dim Firstnumber, Secondnumber As Double
Private Sub cmdAnswer_Click()
Firstnumber = Val(txtFirstnumber.Text)
Secondnumber = Val(txtSecondnumber.Text)
operation = txtOperation.Text
answerr = firstnumber operation secondnumber
Answerbox.Caption = answerr
End Sub
Thanks!
Re: Beginner requesting help!
http://www.vbforums.com/attachment.p...id=47243&stc=1
First, while it's not your main problem, when you dim things this way
Dim Firstnumber, Secondnumber As Double
only Secondnumber is a Double - Firstnumber remains a Variant.
The main problem however is that while there are a limited number of things you can do if you want to use strings like "operation", but math operators is not one of them.
You can create a calculator using the MS scripting runtime and if you serach for "calculator" I'm sure you'll find examples.
Re: Beginner requesting help!
To do it your way you will need to check the operation string and have a condition for each possible operation you want to be able to perform.
Example:
Select Case operation
Case "+"
answer = firstnumber + secondnumber
Case "-"
answer = firstnumber - secondnumber
'etc
End Select
Re: Beginner requesting help!
Quote:
Originally Posted by MartinLiss
http://www.vbforums.com/attachment.p...id=47243&stc=1
First, while it's not your main problem, when you dim things this way
Dim Firstnumber, Secondnumber As Double
only Secondnumber is a Double - Firstnumber remains a Variant.
The main problem however is that while there are a limited number of things you can do if you want to use strings like "operation", but math operators is not one of them.
You can create a calculator using the MS scripting runtime and if you serach for "calculator" I'm sure you'll find examples.
I never knew that!
Thanks!
[QUOTE:danecook21]
To do it your way you will need to check the operation string and have a condition for each possible operation you want to be able to perform.
Example:
Select Case operation
Case "+"
answer = firstnumber + secondnumber
Case "-"
answer = firstnumber - secondnumber
'etc
End Select
[/QUOTE]
Yeah, in the end I just used radio buttons (I think they're called) and it worked a charm, thanks!