Fraaaaaa
Fraaaaaa

Reputation: 47

Visual Basic 4: Expected Array

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?

Upvotes: 3

Views: 241

Answers (4)

user11975130
user11975130

Reputation:

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

Example:

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

Upvotes: 1

Mark Moulding
Mark Moulding

Reputation: 599

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.

Upvotes: 5

jmoreno
jmoreno

Reputation: 13561

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

Upvotes: 2

John Eason
John Eason

Reputation: 571

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

Upvotes: 2

Related Questions