472,989 Members | 3,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,989 software developers and data experts.

Can I write something like: If MyString In ("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

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

Thanks,
Ron
Jul 30 '07 #1
9 2091
"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
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
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
"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
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
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
"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

"(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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: p brian | last post by:
Dear all, Weirdly enough i do not think I have come across this before. Perhaps I do not use enough comments.... mystring = "hello " + someVariable + \ "multiple lines 4 clarity" + \...
5
by: F. Janse Kok | last post by:
I have to write an array of little-endian shorts to the registry of my pocket-pc for example the value's 2 , 5 and 10 but I don't know how to work with chr(0). In my example below the string...
2
by: Wow | last post by:
Do I have to replace all new lines with \n and document.write one line? Is there something like perl's print lines print << 'body'; body in javascripts?
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
11
by: matsi.inc | last post by:
I am looking to make something like a delegate that i can use in my projects but am having a hard time getting started. The behavior I am most interested in is how a delegate changes it's Invoke...
3
stealwings
by: stealwings | last post by:
Hello everyone, I'm kind of dummy in this thing of programing but I have to do it so. Well I have this problem; I wrote a read and write program, it reads the file well but when it writes is it only...
4
by: Paulo da Silva | last post by:
Hi. I am writing a "string" class and I would like to be able to do things like MyString s; cin >s; cout << s; How can I do this? I think the way to go is something like this:
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
27
by: duli | last post by:
Hi: I would like recommendations for books (in any language, not necessarily C++, C, python) which have walkthroughs for developing a big software project ? So starting from inception, problem...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.