LordPhantom
LordPhantom

Reputation: 41

Is there a shortened method to loop through conditions in IF in VB.NET

So I am making a program that needs to check if a file is of Video / Audio format. To do this I have this code (Song is a System.IO.FileInfo):

If Song.Extension.ToUpper = ".MP4" Or Song.Extension = ".AVI" Or Song.Extension = ".MP3" Or Song.Extension = ".AA" Or Song.Extension = ".M4A" Or Song.Extension = ".AIFF" Or Song.Extension = ".WAV" Or Song.Extension = ".M4V" Or Song.Extension = ".AAC" Or Song.Extension = ".VOB" Then

The problem is that this only checks the first 2 conditions. Do I need to split this into separate Ifs or is there some way to test all the conditions in one line?

Upvotes: 0

Views: 90

Answers (2)

Patrick
Patrick

Reputation: 196

It seems to me that it would be easier to use a select case construct with just one case:

    Select Case Song.Extension.ToLower
        Case ".mp4", ".avi", ".mp3", ".aa", ".m4a", ".aiff", ".wav", ".m4v", ".aac", ".vob"
            'Do something
    End Select

Upvotes: 1

Ry-
Ry-

Reputation: 225281

You can make a set with all the extensions in it:

Dim extensions As New HashSet(Of String) From {
    ".mp4", ".avi", ".mp3", ".aa", ".m4a", ".aiff", ".wav",
    ".m4v", ".aac", ".vob"
}

and check if a value is in it with Contains:

If extensions.Contains(Song.Extension.ToLower()) Then
    ⋮
End If

Upvotes: 6

Related Questions