Reputation: 21
In my computing class we have been working on using control arrays to create a basic calculator. Keep in mind I've never worked with control arrays before. This is the code I currently have:
Option Explicit
Private Sub CmdCalc_Click(Index As Integer)
Dim TempSave1 As Double
Dim Answer As Double
Dim Symbol As String
Dim TempSave2 As Double
If CmdCalc(Index) = 10 Then
TempSave1 = LblOutput.Caption
Symbol = "/"
ElseIf CmdCalc(Index) = 11 Then
TempSave1 = LblOutput.Caption
Symbol = "*"
ElseIf CmdCalc(Index) = 12 Then
TempSave1 = LblOutput.Caption
Symbol = "-"
ElseIf CmdCalc(Index) = 14 Then
TempSave1 = LblOutput.Caption
Symbol = "+"
End If
LblOutput.Caption = ""
If Index = 13 Then
TempSave2 = LblOutput.Caption
Answer = TempSave1 & Symbol & TempSave2
End If
If Index = 0 Or 1 Or 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 14 Then
LblOutput.Caption = LblOutput.Caption & CmdCalc(Index).Caption
End If
End Sub
Private Sub CmdClear_Click()
LblOutput.Caption = ""
End Sub
The only issue that I seem to be having is that after you press the "=" button the TempSave2 variable (used to get my answer) is blank instead of having the number the user typed in. I'm unsure how to fix this I've tried rearranging pretty much it all. Any help is appreciated.
First post here so please excuse the bad layout!
Thanks, Keiran.
Excuse the awful colour scheme. My idea was if my teacher is making me learn this awful concept, he can cope with the awful colours :P
Upvotes: 2
Views: 2277
Reputation: 2838
First, you want to check the Index value below, not the CmdCalc(Index):
If Index = 10 Then
TempSave1 = LblOutput.Caption
Symbol = "/"
ElseIf Index = 11 Then
TempSave1 = LblOutput.Caption
Symbol = "*"
ElseIf Index = 12 Then
TempSave1 = LblOutput.Caption
Symbol = "-"
ElseIf Index = 14 Then
TempSave1 = LblOutput.Caption
Symbol = "+"
End If
Next, your line:
If Index = 0 Or 1 Or 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 14 Then
is not doing what you think it is doing. It is evaluating all the Or statements together, then comparing that to Index. It's as if it was written with the following parentheses:
If Index = (0 Or 1 Or 2 Or 3 Or 4 Or 5 Or 6 Or 7 Or 8 Or 9 Or 10 Or 11 Or 12 Or 14) Then
I believe that code will ONLY evaluate to true if Index = 14 but I haven't done the actual bit math, but regardless, it's clearly not what you want.
There are several ways to write this, but the shortest might be:
If (Index >= 0 And Index <= 12) Or Index = 14 Then
Without seeing the actual screen, I can't comment on the rest of the code.
Upvotes: 3