Can I write something like: If MyString In ("Apple", "Orange", "Pear") Then...

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

Can I write

If MyString = "Apple" Or MyString = "Orange" Or MyString = "Pear" Then

to something more compact like

If MyString In ("Apple", "Orange", "Pear") Then

My last line obviously doesn't work but maybe I'm close?

Thanks,
Ron
 
Ronald S. Cook said:
Can I write

If MyString = "Apple" Or MyString = "Orange" Or MyString = "Pear" Then

to something more compact like

If MyString In ("Apple", "Orange", "Pear") Then

My last line obviously doesn't work but maybe I'm close?

Thanks,
Ron

There is nothing built in to do this. You could, however, qrite a function to implement something quite similar to that.

Here is a quick and dirty example:

Public Function IsIn(ByVal Find As String, ByVal ParamArray InList() As String) As Boolean
Dim intIndex As Integer
For intIndex = LBound(InList) To UBound(InList)
If InList(intIndex) Is Find Then
Return True
End If
Next
Return False
End Function

It would be called as:

If MyString IsIn (MyString, "Apple", "Orange", "Pear") Then ...

I hope this helps.
 
Or you could use a Select case :

Select Case MyString
Case "Apple","Orange","Pear"
' Do something
Case Else
' Do something else
End Select

Depending on how fixed are those value you have also AFAIK a set oriented
class (don't remember the name right now) or a shared StringCollection
object.

If MyFavoriteFruits.Contains(MyString) Then etc... with MyFavoriteFruits
being initialized once for all...

etc...
 
This one is probably a bit faster ( and easier in my opinion , but this
might be a mather of personal preference )
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim Checkvals As String() = {"Apple", "Orange", "Pear"}

Dim ValueToCheck As String = "Orange"

MsgBox(IsString(ValueToCheck, Checkvals))

End Sub

Private Function IsString(ByVal Value As String, ByVal CheckVals As
String()) As Boolean

For Each Checkval As String In CheckVals

If String.Equals(Value, Checkval) Then Return True

Next

End Function



End Class

also
 
Al Reid said:
There is nothing built in to do this. You could, however, qrite a function to implement something quite similar to that.

Here is a quick and dirty example:

Public Function IsIn(ByVal Find As String, ByVal ParamArray InList() As String) As Boolean
Dim intIndex As Integer
For intIndex = LBound(InList) To UBound(InList)
If InList(intIndex) Is Find Then
Return True
End If
Next
Return False
End Function

It would be called as:

If MyString IsIn (MyString, "Apple", "Orange", "Pear") Then ...

This should have been:

If IsIn (MyString, "Apple", "Orange", "Pear") Then ...
 
Can I write

If MyString = "Apple" Or MyString = "Orange" Or MyString = "Pear" Then

to something more compact like

If MyString In ("Apple", "Orange", "Pear") Then

If Instr("Apple,Orange,Pear", MyString) Then

(use a different separator character if it is possible for MyString to
contain commas).

HTH
Phil.
 
Phil said:
If Instr("Apple,Orange,Pear", MyString) Then

(use a different separator character if it is possible for MyString to
contain commas).

If you go with this approach, I'd suggest:

\\\
If Instr(",Apple,Orange,Pear,", "," & MyString &",") Then
///

Otherwise substrings can be returned as being contained within the list
(e.g., pass in "range" and it would have returned true).
 
Ronald S. Cook said:
Can I write

If MyString = "Apple" Or MyString = "Orange" Or MyString = "Pear"
Then

to something more compact like

If MyString In ("Apple", "Orange", "Pear") Then

My last line obviously doesn't work but maybe I'm close?

Dim items As New List(Of String)(New String() {"Apple", "Orange", "Pear"})

'...

If items.Contains(mystring) Then


'Items' hast to be initialized once only (static local variable or shared
field)


Armin
 
(O)enone said:
If you go with this approach, I'd suggest:

\\\
If Instr(",Apple,Orange,Pear,", "," & MyString &",") Then
///

Otherwise substrings can be returned as being contained within the list
(e.g., pass in "range" and it would have returned true).

Yes :-)
 
If Regex.IsMatch(MyString, "Apple|Orange|Pear") Then
Call Debug.Print("True")
Else
Call Debug.Print("False")
End If
 
Back
Top