
-----------------------------------
acidburn90
Wed Jun 01, 2005 10:17 pm

question on vb so compilicated... need help!
-----------------------------------
im having this problem with a basic calculator program where i declare num as an integer to be the value of the text in the display and num2 as the value of the text in the display after i have added my second number but everytime i click the equals sign it gives me zero i was wondering if anyone knows why this is happening...
this is the code:
[code]
Private Sub eight_Click()
display.Text = Str$(8)
End Sub
Private Sub seven_Click()
If display.Text = "+" Then
display.Text = Str$(7)
Dim num2 As Integer
num2 = Val(display.Text)
Else
display.Text = Str$(7)
End If
End Sub
Private Sub equals_Click()
Dim t As Integer
t = num
display.Text = Str$(t)
End Sub[/code]
its really simple but i dont know it just isnt working... im a begginner just in case anyone was wondering

p.s. the glasses smiley is really the code for "8 )" so it would be Str$ ( 8 )

-----------------------------------
betaflye
Sat Jun 04, 2005 9:41 am


-----------------------------------
A few comments: 

1. Use option explicit to not declare variables as they are used, explicitly declare all variables. You're creating a variable with a value of 0 and assigning it to t, as far as I can tell.
2. Use descriptive variable names that follow some sort of naming style, I try to follow the Microsoft one.
http://support.microsoft.com/kb/q110264/
3. This isn't a complete calculator. 
4. You don't need a subroutine for each number, use a control array.
5. I don't see where your addition took place.
6. Learn variable scope.

A few notes on the code, txtDisplay can be set to Locked = True, this prevents users from directly editing it. Here's some code I hacked out for you.

' Option explicit requires variables to be declared before they are used
Option Explicit
' Declaring the numbers used in the calculation as public makes things easier and provides
' an example of variable scope
Public dblNum1 As Double, dblNum2 As Double

Private Sub cmdClear_Click()
    dblNum1 = 0
    dblNum2 = 0
    txtDisplay.Text = ""
End Sub

Private Sub cmdEquals_Click()
    Dim lngRetVal As Double

    dblNum2 = Val(txtDisplay.Text)
    lngRetVal = dblNum1 + dblNum2
    txtDisplay.Text = lngRetVal
End Sub

Private Sub cmdNumber_Click(Index As Integer)
    ' Index is the index of the control array that was clicked
    ' Since an array starts at 0, we cast to string the value of the index + 1
    ' and concatinate it to the display
    txtDisplay.Text = txtDisplay.Text & Str((Index + 1))
End Sub

Private Sub cmdPlus_Click()
    dblNum1 = Val(txtDisplay.Text)
    txtDisplay.Text = ""
End Sub

Private Sub Form_Load()
    ' Not really needed if one assigned values to the command boxes at design time
    ' however used to illustrate control array
    
    Dim intCount, intNumber As Integer
    
    For intCount = 0 To (cmdNumber.Count - 1)
        intNumber = intCount + 1
        cmdNumber(intCount).Caption = (intNumber)
    Next
End Sub

Good luck with your project,

-----------------------------------
acidburn90
Sat Jun 04, 2005 4:37 pm


-----------------------------------
thnx it really helped me
