Reputation: 2014
How do I check for two possible strings else... (see example code)
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Input path.
Dim p As String = TextBox1.Text
' Get extension.
Dim extension As String = Path.GetExtension(p)
If extension = ".abc" Or ".xyz" Then
'nothing
Label1.Text = "file type suported"
Else
Label1.Text = "Unsuported file type"
End If
End Sub
End Class
Upvotes: 1
Views: 790
Reputation: 27322
@Tim Schmelter has already answered this quite thoroughly but:-
A Simple alternative is a Select
statement - this will make it easier to update your code if you decide to support some other file type in the future:
Dim extension As String = Path.GetExtension(p)
Select Case extension
Case ".abc", ".xyz"
Label1.Text = "file type suported"
Case Else
Label1.Text = "unsupported file type"
End Select
Upvotes: 1
Reputation: 5885
Store all the supported extensions in a HashSet<string>
structure and use the Contains
method to check whether a specified extension is supported or not.
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim supportedExtensions As New System.Collections.Generic.HashSet(Of String)
supportedExtensions.Add(".abc")
supportedExtensions.Add(".xyz")
' Input path.
Dim p As String = TextBox1.Text
' Get extension.
Dim extension As String = Path.GetExtension(p)
If (supportedExtensions.Contains(extension)) Then
'nothing
Label1.Text = "file type suported"
Else
Label1.Text = "Unsuported file type"
End If
End Sub
End Class
UPDATE: As the number of supported extensions is increased, the OR conditions needed for extension support checking would go up and this would affect the legibility and maintainability of your code. Consequently, this code snippet offers you more readability.
Upvotes: 0
Reputation: 6948
You could also put your supported extensions in a string
Dim ValidExtensions As String = ".abc.xyz"
Then
If ValidExtensions.Contains(extension) Then
Label1.Text = "file type suported"
Else
Label1.Text = "Unsuported file type"
End If
This way it won't matter how many extensions you support or what changes you make to the list you only change the main string for the code to work.
Plus down the road if you decide to let the user change the supported file types you can make the changes programatically.
Upvotes: 2
Reputation: 460158
Just add the condition:
If extension = ".abc" OrElse extension = ".xyz" Then
'nothing
Label1.Text = "file type suported"
Else
Label1.Text = "Unsuported file type"
End If
I have used OrElse
because this checks the second condition only if the first condition returned false. For the same reason you should normally use AndAlso
instead of And
.
Consider this example:
Dim extension As String = Nothing
If extension Is Nothing Or extension.ToLower() = ".abc"
' ...
This throws an exception because extension
is nothing but the Or
causes the second condition to be evaluated anyway. This is safe:
If extension Is Nothing OrElse extension.ToLower() = ".abc"
Another aproach is to use a positive-list and Enumerable.Contains
:
Dim allowedExtensions = { ".abc", ".xyz" }
If allowedExtensions.Contains(extension) Then
' ...
Upvotes: 4
Reputation: 11188
I think you want alter one line to this:
If extension = ".abc" Or extension = ".xyz" Then
Upvotes: 0