Results 1 to 4 of 4

Thread: [RESOLVED] Beginner requesting help!

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Resolved [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!

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    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.

  3. #3
    Hyperactive Member danecook21's Avatar
    Join Date
    Feb 2008
    Location
    NC, USA
    Posts
    501

    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2008
    Posts
    2

    Re: Beginner requesting help!

    Quote 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
  •  



Click Here to Expand Forum to Full Width