VB Code problem

Discussion in 'Software' started by ClaretAlan, Sep 17, 2004.

  1. ClaretAlan

    ClaretAlan Private E-2

    I am trying to write a calculator in VB4. The code I have used is below. When I run the calculator the answer it gives me is always the same as the last value I typed in. Can anyone help?

    Sub Form_load()
    Dim n1 As Integer
    Dim n2 As Integer
    Dim opt As String
    n1 = "0"
    n2 = "0"
    opt = " "
    End Sub


    Sub commandplus_click()
    n1 = Textanswer.Text
    opt = "+"
    Textanswer.Text = " "
    End Sub


    Sub commandmulti_click()
    n1 = Textanswer.Text
    opt = "*"
    Textanswer.Text = " "
    End Sub

    Sub commandminus_click()
    n1 = Textanswer.Text
    opt = "-"
    Textanswer.Text = " "
    End Sub


    Sub commanddivide_click()
    n1 = Textanswer.Text
    opt = "/"
    Textanswer.Text = " "
    End Sub


    Sub commandequals_click()
    n2 = Textanswer.Text
    If opt = "+" Then
    Textanswer.Text = n1 + n2
    ElseIf opt = "-" Then
    Textanswer.Text = n1 - n2
    ElseIf opt = "/" Then
    Textanswer.Text = n1 / n2
    ElseIf opt = "*" Then
    Textanswer.Text = n1 * n2
    End If
    End Sub
     
  2. Wookie

    Wookie Sergeant Major

    send the source code to wookmaster@gmail.com


    I cant see anything wrong from here but let me try it out and see what I can get for you
     
  3. GregoryDalton

    GregoryDalton Private E-2

    Just looking at that I wonder if your first variable is going out of scope...

    You assign the variable in a function, which is fair enough. But then the function ends. The variable is essentially unassigned because at the 'End Sub' line the interpreter will discard its value. Although Visual Basic is considerably different in its approach to somethign like C++ or Java so I could be wrong... but here we go anyway ;-)

    Ways around it.... hmmmm... well the easiest, but by no means the nicest way around it is to define your 'n1' and 'opt' variables as global. This means that they will never go out of scope. This is however often not referred to as good programming style, to have a multitude of variables declared as global.

    I also struggle a fair amount with the modularity of visual basic - that is how to write functions that receive parameters. A way around this is to design your form with a couple of invisible labels in them for holding variables. (set the visible property to false and they wont show up when you run).

    Lets pretend you create 2 of these on your calculator form and call one "n1_label" and the other "opt_label" and set them both to be invisible.
    Then your function at tthe start wants to be something like:

    Sub commandplus_click()
    n1_label.text = Textanswer.Text
    opt_label.text = "+"
    Textanswer.Text = " "
    End Sub

    and the same down. The variables will be effectively stored in these labels which no-one can see.

    Then change your equals function to be something like :

    If opt_label.text = "+" Then
    Textanswer.Text = n1_label.text + n2


    Just my suggestions... I dont have a copy of VB installed so cant test but I did a fair amount when I was younger.

    Hope that helps!!!!

    Cheers,
    Greg
     
  4. Pony

    Pony Private E-2

    Your not keeping the first number in memory so it only sees the last number when your pressing =
    Declare a new string for holding the first number like
    Dim remeberthis As string

    then at the end
    remeberthis * textanswer.text
     
  5. Pony

    Pony Private E-2

    Yours works just need to move the Dim statments to the general section

    I used numbers buttons also on mine so you can type or use the button pads but this is what it looks like


    *you will need to set opt , n1 & n2 back to 0 0 and "" in the end of this

    Dim n1 As Integer
    Dim n2 As Integer
    Dim opt As String
    Dim remember As String

    Private Sub Command1_Click()
    Textanswer.Text = Textanswer.Text + "1"
    End Sub

    Private Sub Command2_Click()
    Textanswer.Text = Textanswer.Text + "2"
    End Sub

    Private Sub Command3_Click()
    Textanswer.Text = Textanswer.Text + "3"
    End Sub

    Private Sub Command4_Click()
    Textanswer.Text = Textanswer.Text + "4"
    End Sub

    Private Sub Command5_Click()
    Textanswer.Text = Textanswer.Text + "5"
    End Sub

    Private Sub Command6_Click()
    Textanswer.Text = Textanswer.Text + "6"
    End Sub

    Private Sub Command7_Click()
    Textanswer.Text = Textanswer.Text + "7"
    End Sub

    Private Sub Command8_Click()
    Textanswer.Text = Textanswer.Text + "8"
    End Sub

    Private Sub Command9_Click()
    Textanswer.Text = Textanswer.Text + "9"
    End Sub

    Sub Form_load()
    End Sub


    Sub commandplus_click()
    n1 = Textanswer.Text
    opt = "+"
    Textanswer.Text = " "
    End Sub


    Sub commandmulti_click()
    n1 = Textanswer.Text
    opt = "*"
    Textanswer.Text = " "
    End Sub

    Sub commandminus_click()
    n1 = Textanswer.Text
    opt = "-"
    Textanswer.Text = " "
    End Sub


    Sub commanddivide_click()
    n1 = Textanswer.Text
    opt = "/"
    Textanswer.Text = " "
    End Sub


    Sub commandequals_click()
    n2 = Textanswer.Text
    If opt = "+" Then
    Textanswer.Text = n1 + n2
    ElseIf opt = "-" Then
    Textanswer.Text = n1 - n2
    ElseIf opt = "/" Then
    Textanswer.Text = n1 / n2
    ElseIf opt = "*" Then
    Textanswer.Text = n1 * n2
    End If
    End Sub
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds