3

I've been trying to do a little project on Visual Basic 4 (I know, old version. But it needs to work with old computers so), and I have this code for a calculator. It's for the equals command:

Private Sub Command12_Click()
    Dim sd As Integer
    Dim operator As Integer
    Dim result As Integer
    Dim Val As String

    If operator = 1 Then
        result = Val(num) + Val(txtOutput.Text)
        txtOutput.Text = result
    ElseIf operator = 2 Then
        result = Val(num) - Val(txtOutput.Text)
        txtOutput.Text = result
    ElseIf operator = 3 Then
        result = Val(num) * Val(txtOutput.Text)
        txtOutput.Text = result
    ElseIf operator = 4 Then
        result = Val(num) / Val(txtOutput.Text)
        txtOutput.Text = result
    ElseIf operator = 5 Then
        result = Val(num) Mod Val(txtOutput.Text)
        txtOutput.Text = result
    ElseIf operator = 6 Then
        result = Val(num) * Val(num)
        txtOutput.Text = result
    End If
End Sub

The problem is, once I run it, it shows this error:
Problem.

I tried searching on the Internet, and the only solution I have found online is to "Close and reopen Visual Basic and hope it works", but it doesn't... Any suggestions?

1
  • 2
    You have declared Val as string: Dim Val As String. Why? Commented Aug 28, 2021 at 12:42

4 Answers 4

5

As others have said, the problem that's directly causing the error is the Dim Val as String statement, but I don't see where anyone has mentioned the reason...

The issue is that by Diming the identifier Val, you've effectively masked the intrinsic Val() function. Later in your original code, when you refer to the identifier, you're doing so using a syntax that requires either an array or a function; since you've Dimed it as a variable, the parser then assumes that it must be an array variable, which it's not - hence the Expected array error.

Simple fix - just delete the Dim Val as String line. The code could be shortened somewhat by moving the txtOutput.Text = result statement outside of the If...End If block, which would be an improvement in both size and clarity (but not performance), but it would otherwise work as is.

Sign up to request clarification or add additional context in comments.

Comments

2

As @Steeeve says, remove the 'Val as String' line, but I suspect that the error is actually appearing because all the lines starting 'result' have two statements on the line and that's confusing VB. You only need

txtOutput.Text = result

once after the If ... End If statement:

Private Sub Command12_Click()
    
    Dim sd As Integer
    Dim operator As Integer
    Dim result As Integer
    
    If operator = 1 Then
      result = Val(num) + Val(txtOutput.Text)
    
    ElseIf operator = 2 Then
      result = Val(num) - Val(txtOutput.Text)
    
    ElseIf operator = 3 Then
      result = Val(num) * Val(txtOutput.Text)
    
    ElseIf operator = 4 Then
      result = Val(num) / Val(txtOutput.Text)
    
    ElseIf operator = 5 Then
      result = Val(num) Mod Val(txtOutput.Text)
    
    ElseIf operator = 6 Then
      result = Val(num) * Val(num)
    
    End If
    
    txtOutput.Text = result
 End Sub

2 Comments

Not to critique your solution, but where do you see multiple statements on one line? In my browser their code is correctly formatted (if non-optimal)
The original posting was edited and reformatted after I posted my answer. Originally the lines just had a space after the 'result' statement and were immediately followed on the same line by the txtOutput.txt lines.
2

The obvious problem is the redefining Val, but you can also clean things up a bit to make it easier to understand by doing the conversions just once.

Private Sub Command12_Click()
    Dim sd As Integer
    Dim operator As Integer
    Dim result As Integer
   
    Dim vNum = Val(num)
    Dim vOuput = Val(txtOutput.Text)

    If operator = 1 Then
        result = vNum + vOuput
    ElseIf operator = 2 Then
        result = vNum - vOuput
    ElseIf operator = 3 Then
        result = vNum * vOuput
    ElseIf operator = 4 Then
        result = vNum / vOuput
    ElseIf operator = 5 Then
        result = vNum Mod vOuput
    ElseIf operator = 6 Then
        result = vNum * vNum  
    End If
   
    txtOutput.Text = result
End Sub

Comments

1

Val is a built-in function which returns a number read from a string.

Example:

value% = Val("abc123") 'returns 123

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.