|
-
Sep 18th, 2008, 01:21 PM
#1
Thread Starter
New Member
[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!
-
Sep 18th, 2008, 03:20 PM
#2
Re: Beginner requesting help!

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.
-
Sep 18th, 2008, 10:37 PM
#3
Hyperactive Member
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
-
Sep 19th, 2008, 10:57 AM
#4
Thread Starter
New Member
Re: Beginner requesting help!
 Originally Posted by MartinLiss
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!
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
|