6

In VB.NET I am trying to determine in a given string exists in a String Array. According to my research the Array has a 'Contains' method that I can use, so the Code looks something like this:

Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}

If (fileTypesZ.Contains(tempTest)) Then

End If

However, VB.NET is saying 'Contains' is not a member of 'System.Array'. Is there another method that I can use?

Art F
  • 3,992
  • 10
  • 49
  • 81
  • 6
    You could use `List.Contains` if you're on .NET 2: `Dim fileTypesZ = new List(Of String)({"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"})` – Tim Schmelter Sep 27 '13 at 17:49
  • 2
    @varocarbas It will only work if you've got `Imports System.Linq` in place (see my answer) - that's the default, so most people do, but if it was removed, this won't work as-is – Reed Copsey Sep 27 '13 at 17:50
  • @ReedCopsey I am not importing System.Linq. If you open a new project (VS 2008 or 2010) and write this code, it would work. – varocarbas Sep 27 '13 at 17:51
  • @varocarbas That's most likely because you have `System.Linq` setup as a default import: http://msdn.microsoft.com/en-us/library/64c84czf(VS.80).aspx – Reed Copsey Sep 27 '13 at 17:57
  • @ReedCopsey You are right. – varocarbas Sep 27 '13 at 18:02
  • @varocarbas: Even if he's using a new .NET version it is possible that the project is older. The you need to upgrade it and add the `System.Core dll` manually. – Tim Schmelter Sep 27 '13 at 18:04
  • @TimSchmelter all clear now. I have used Array.Contains many times and haven't ever thought that it wasn't actually an "array method". Now that this is clear; I do understand that Linq has to be supported. – varocarbas Sep 27 '13 at 18:07
  • 2
    @varocarbas: Another method which could be used even with .NET 2 is [`Array.Find`](http://msdn.microsoft.com/en-us/library/d9hy2xwa(v=vs.80).aspx). – Tim Schmelter Sep 27 '13 at 18:09
  • 1
    @TimSchmelter you are making quite a few valid points, why don't you write an answer? It might be an alternative to Reed Copsey's one (other options if you cannot use "Contains"). – varocarbas Sep 27 '13 at 18:11

2 Answers2

13

There is no Contains on Array, but there is Enumerable.Contains, which is an extension method that works on arrays.

Make sure to include Imports System.Linq at the top of your file, and that you're referencing System.Core.dll in your project references.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

What framework are you working with? I ran this in 4 Full and it worked:

Sub Main()
    Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}

    If (fileTypesZ.Contains("PDF")) Then
        MsgBox("Yay")
    End If
End Sub

Keep in mind array.contains uses equality, so "PDF" works, "PD" does not. You may need to iterate with indexof if you are looking for partial matches.

In that case try: Dim fileTypesZ As String() = {"PDF", "TXT", "DOC", "DOCX", "XLS", "XLSX", "JPG", "JPGE", "BMP", "GIF"}

    If (fileTypesZ.Contains("PD")) Then
        MsgBox("Yay")
    Else
        For i = 0 To fileTypesZ.Length - 1
            If fileTypesZ(i).IndexOf("PD") = 0 Then
                MsgBox("Yay")
            End If
        Next
    End If
Jason Hughes
  • 748
  • 6
  • 18
  • The first part of your answer is already explained by Reed Copsey's one (and his comments above): `.Contains` is not one of the methods of an array; you can use it with an array via `Enumerable.Contains` from LINQ. Thus, to make the OP's code work you have to account for LINQ: by adding a reference to LINQ (in .NET 3.5 and higher) or the whole LINQ library in older versions (http://stackoverflow.com/questions/2138/linq-on-the-net-2-0-runtime). – varocarbas Sep 28 '13 at 08:06
  • Contains may be a member of System.Linq.Enumerable but you don't have to add it as a reference, at least not in .Net 4 on up, it comes "preloaded" in references. I answered the OP's question with code I tested myself in VS rather than just comment on it, maybe I should have been more clear about LINQ. For the OP, check out [link](http://msdn.microsoft.com/en-us/library/bb352880(v=vs.100).aspx) or message me if you have problems. – Jason Hughes Sep 30 '13 at 19:00
  • Various hours before your wrote your answer, I was thinking like you (it works without reference) and that's why I refer to the comments above and to the Reed Copsey answer. Even though you can do: Dim myArray() As String myArray.Contains. This Contains is a LINQ extension method which can only be used if the LINQ reference is set in the project. You might not need to write Imports System.Linq because is preloaded (as explained in one of the aforementioned comments). The safest thing is writing Imports System.Linq anyway. If you are in a .NET before 3.5 you would have to add LINQ. – varocarbas Sep 30 '13 at 19:06
  • 1
    Please, read my comment above "I am not importing System.Linq. If you open a new project (VS 2008 or 2010) and write this code, it would work. –" and the answer I got. – varocarbas Sep 30 '13 at 19:08