By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,253 Members | 1,689 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,253 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 30 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Ronald S. Cook" <rc***@westinis.comwrote in message news:uJ*************@TK2MSFTNGP06.phx.gbl...
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.

--
Al Reid
Jul 30 '07 #2

P: n/a
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
>If MyString = "Apple" Or MyString = "Orange" Or MyString = "Pear" Then
should be as it is short circuit evaluation and thus faster
>If MyString = "Apple" Orelse MyString = "Orange" Orelse MyString =
"Pear" Then



"Al Reid" <ar*****@reidDASHhome.comschreef in bericht
news:eU**************@TK2MSFTNGP06.phx.gbl...
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
>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.

--
Al Reid


Jul 30 '07 #3

P: n/a
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...

---
Patrice

"Al Reid" <ar*****@reidDASHhome.coma écrit dans le message de news:
eU**************@TK2MSFTNGP06.phx.gbl...
"Ronald S. Cook" <rc***@westinis.comwrote in message
news:uJ*************@TK2MSFTNGP06.phx.gbl...
>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.

--
Al Reid


Jul 30 '07 #4

P: n/a
"Al Reid" <ar*****@reidDASHhome.comwrote in message news:eU**************@TK2MSFTNGP06.phx.gbl...
"Ronald S. Cook" <rc***@westinis.comwrote in message news:uJ*************@TK2MSFTNGP06.phx.gbl...
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 ...
This should have been:

If IsIn (MyString, "Apple", "Orange", "Pear") Then ...
>
I hope this helps.

--
Al Reid


Jul 30 '07 #5

P: n/a
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.
Jul 31 '07 #6

P: n/a
Phil wrote:
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).

--

(O)enone
Jul 31 '07 #7

P: n/a
"Ronald S. Cook" <rc***@westinis.comschrieb
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

Jul 31 '07 #8

P: n/a

"(O)enone" <oe****@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Phil wrote:
>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).
Yes :-)
Jul 31 '07 #9

P: n/a
If Regex.IsMatch(MyString, "Apple|Orange|Pear") Then
Call Debug.Print("True")
Else
Call Debug.Print("False")
End If

On Mon, 30 Jul 2007 10:40:17 -0600, "Ronald S. Cook"
<rc***@westinis.comwrote:
>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
Aug 5 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.