mvaculisteanu
mvaculisteanu

Reputation: 167

Public member 'Contains' on type 'String()' not found

I have 3 string arrays but the third, 'a3', throws:

Public member 'Contains' on type 'String()' not found.

at a3.Contains("a")

Public Class Form1

    Dim a1 As String() = {"a", "b", "c"}
    Dim a3 = {"a", "b", "c"}

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a2 = {"a", "b", "c"}
        a1.Contains("a")
        a2.Contains("a")
        a3.Contains("a")
    End Sub
End Class

All 3 are of type System.String[].

Upvotes: 0

Views: 3398

Answers (1)

MatSnow
MatSnow

Reputation: 7527

This has to do with Local Type Inference.

The Visual Basic compiler uses type inference to determine the data types of local variables declared without an As clause.

The variable-type is only inferred on local variables. Because a3 is a class-level variable, it is of type Object which has no Contains-method.

Whenever possible, you should set Option Strict On and declare all variables with the correct type.

Upvotes: 1

Related Questions