Is there a mechanism in VB.NET that allows something like:
If myVar In ("A","B","C" ) Then...
The way I'm doing it now is:
Select Case myVar
Case "A","B","C"
Or like this:
If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
I'm just wondering if there's some handy new syntax in .NET to handle this. 5 37867
"Jason" <so*****@micros oft.com> schrieb: Is there a mechanism in VB.NET that allows something like:
If myVar In ("A","B","C" ) Then...
The way I'm doing it now is:
Select Case myVar Case "A","B","C"
Or like this:
If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
I'm just wondering if there's some handy new syntax in .NET to handle this.
There is no new syntax to do that. I prefer the 'Select Case' solution.
Alternatively you can check if an object is contained in an array using
'Array.IndexOf' .
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Jason,
In addition to the other comments: If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
Consider using OrElse instead of Or, as Or will cause all the equations to
be evaluated before returning a result, OrElse will return the result after
the first match. For example, if myVar = "A":
If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
Will compare myVar, to "A", then to "B", the to "C" before it returns True.
Where as:
If myVar = "A" OrElse myVar = "B" OrElse myVar = "C" Then...
Will compare myVar to "A", and return True! (checking for "B" or "C" is not
needed as "A" is True!)
---x--- begin earlier post of mine ---x---
I would use the RegEx as its IMHO the "simplest" & "cleanest"
implementation! However! you need to understand RegEx to be comfortable
using it, luckily this regex is easy. Also you may need to get over the
perception that RegEx is slow. Yes it has some inherit overhead the other
methods do not, however that overhead may be warranted for the simplicity of
the routine. Also based on the context the overhead of the RegEx may be
lower, significantly lower, then other routines.
Note: I would only use the RegEx method for checking Strings, if I was
creating an "In" for other types I would pick an algorithm that was more
friendly for that type.
Here's one possibility for an "In" function based on RegEx.
Public Shared Function [In](ByVal input As String, ByVal ParamArray
words() As String) As Boolean
Dim pattern As String = String.Join("|" , words)
Return System.Text.Reg ularExpressions .Regex.IsMatch( input, pattern)
End Function
Then to use it you simple need to:
strTest = "The cat jumped over the sleepy dog."
If [In](strTest, "cat","dog" , "sleep") Then
Debug.WriteLine ("Found strings")
Else
Debug.WriteLine ("Did NOT find strings!")
End If
If the above RegEx version proved to be a performance problem I would then
consider using a For Each loop.
Public Shared Function [In](ByVal input As String, ByVal ParamArray
words() As String) As Boolean
For Each word As String In words
If input.IndexOf(w ord) <> -1 Then
Return True
End If
Next
Return False
End Function
I would consider creating an object that contained a list of words, which
had a method the checked to see if a string had one of those words.
Public Class ValidValues
Private ReadOnly m_regex As System.Text.Reg ularExpressions .Regex
Public Sub New(ByVal ParamArray words() As String)
Dim pattern As String = String.Join("|" , words)
m_regex = New System.Text.Reg ularExpressions .Regex(pattern,
System.Text.Reg ularExpressions .RegexOptions.C ompiled)
End Sub
Public Function IsMatch(ByVal value As String) As Boolean
Return m_regex.IsMatch (value)
End Function
End Class
Dim values As New ValidValues("ca t", "dog", "sleep")
If values.IsMatch( strTest) Then
Debug.WriteLine ("Found strings")
Else
Debug.WriteLine ("Did NOT find strings!")
End If
Return
Dim statusValues As New ValidValues("AC TIVE","OTHER")
If statusValues.Is Match(Status) Then
...
Notice in both cases that the actual method (algorithm) of matching is
hidden (encapsulated) within either the object or the function itself. Which
allows you to replace the Algorithm with a more efficient one if needed...
Whether I used the Function or Class would depend on how the function or
class was being within my program... Using Refactoring http://www.refactoring.com I can change between the two...
As the others pointed out there are other equally valid ways to implement
the above. There are also methods that have yet to be mentioned, such as
using a HashTable or using a DataSet, plus still others. Which one you
should use REALLY depends on the context of what you are doing!
For example if your status value is in a DataTable, you can use a filter
statement that include an "In" statement, on either DataTable.Selec t or
DataView.RowFil ter. Like wise of the list of valid statues are in a
DataTable, you could use DataTable.Rows. Find assuming that the status was
the primary key to that table, or use filter & either DataTable.Selec t or
DataView.RowFil ter...
Hope this helps
Jay
---x--- end earlier post of mine ---x---
Another example routine:
Public Shared Function IsInSet(ByVal item As Object, ByVal ParamArray
setItems() As Object) As Boolean
Return (Array.IndexOf( SetItems, Item) >= 0)
End Function
Hope this helps
Jay
"Jason" <so*****@micros oft.com> wrote in message
news:eg******** ******@TK2MSFTN GP14.phx.gbl... Is there a mechanism in VB.NET that allows something like:
If myVar In ("A","B","C" ) Then...
The way I'm doing it now is:
Select Case myVar Case "A","B","C"
Or like this:
If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
I'm just wondering if there's some handy new syntax in .NET to handle this.
I have been trying to figure this out too. The regex you have for
testing individual words is what I came up with too. Unfortunately, it
also matches partial strings like catdog, dogcat, catty. Any tricks to
forcing "whole word" logic?
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<O5******* *******@TK2MSFT NGP10.phx.gbl>. .. Jason, In addition to the other comments: If myVar = "A" Or myVar = "B" Or myVar = "C" Then... Consider using OrElse instead of Or, as Or will cause all the equations to be evaluated before returning a result, OrElse will return the result after the first match. For example, if myVar = "A":
If myVar = "A" Or myVar = "B" Or myVar = "C" Then... Will compare myVar, to "A", then to "B", the to "C" before it returns True.
Where as:
If myVar = "A" OrElse myVar = "B" OrElse myVar = "C" Then... Will compare myVar to "A", and return True! (checking for "B" or "C" is not needed as "A" is True!)
---x--- begin earlier post of mine ---x--- I would use the RegEx as its IMHO the "simplest" & "cleanest" implementation! However! you need to understand RegEx to be comfortable using it, luckily this regex is easy. Also you may need to get over the perception that RegEx is slow. Yes it has some inherit overhead the other methods do not, however that overhead may be warranted for the simplicity of the routine. Also based on the context the overhead of the RegEx may be lower, significantly lower, then other routines.
Note: I would only use the RegEx method for checking Strings, if I was creating an "In" for other types I would pick an algorithm that was more friendly for that type.
Here's one possibility for an "In" function based on RegEx.
Public Shared Function [In](ByVal input As String, ByVal ParamArray words() As String) As Boolean Dim pattern As String = String.Join("|" , words) Return System.Text.Reg ularExpressions .Regex.IsMatch( input, pattern) End Function
Then to use it you simple need to:
strTest = "The cat jumped over the sleepy dog." If [In](strTest, "cat","dog" , "sleep") Then Debug.WriteLine ("Found strings") Else Debug.WriteLine ("Did NOT find strings!") End If
If the above RegEx version proved to be a performance problem I would then consider using a For Each loop.
Public Shared Function [In](ByVal input As String, ByVal ParamArray words() As String) As Boolean For Each word As String In words If input.IndexOf(w ord) <> -1 Then Return True End If Next Return False End Function
I would consider creating an object that contained a list of words, which had a method the checked to see if a string had one of those words.
Public Class ValidValues
Private ReadOnly m_regex As System.Text.Reg ularExpressions .Regex
Public Sub New(ByVal ParamArray words() As String) Dim pattern As String = String.Join("|" , words) m_regex = New System.Text.Reg ularExpressions .Regex(pattern, System.Text.Reg ularExpressions .RegexOptions.C ompiled) End Sub
Public Function IsMatch(ByVal value As String) As Boolean Return m_regex.IsMatch (value) End Function
End Class
Dim values As New ValidValues("ca t", "dog", "sleep") If values.IsMatch( strTest) Then Debug.WriteLine ("Found strings") Else Debug.WriteLine ("Did NOT find strings!") End If Return
Dim statusValues As New ValidValues("AC TIVE","OTHER") If statusValues.Is Match(Status) Then ...
Notice in both cases that the actual method (algorithm) of matching is hidden (encapsulated) within either the object or the function itself. Which allows you to replace the Algorithm with a more efficient one if needed...
Whether I used the Function or Class would depend on how the function or class was being within my program... Using Refactoring http://www.refactoring.com I can change between the two...
As the others pointed out there are other equally valid ways to implement the above. There are also methods that have yet to be mentioned, such as using a HashTable or using a DataSet, plus still others. Which one you should use REALLY depends on the context of what you are doing!
For example if your status value is in a DataTable, you can use a filter statement that include an "In" statement, on either DataTable.Selec t or DataView.RowFil ter. Like wise of the list of valid statues are in a DataTable, you could use DataTable.Rows. Find assuming that the status was the primary key to that table, or use filter & either DataTable.Selec t or DataView.RowFil ter...
Hope this helps Jay
---x--- end earlier post of mine ---x---
Another example routine:
Public Shared Function IsInSet(ByVal item As Object, ByVal ParamArray setItems() As Object) As Boolean Return (Array.IndexOf( SetItems, Item) >= 0) End Function
Hope this helps Jay
"Jason" <so*****@micros oft.com> wrote in message news:eg******** ******@TK2MSFTN GP14.phx.gbl... Is there a mechanism in VB.NET that allows something like:
If myVar In ("A","B","C" ) Then...
The way I'm doing it now is:
Select Case myVar Case "A","B","C"
Or like this:
If myVar = "A" Or myVar = "B" Or myVar = "C" Then...
I'm just wondering if there's some handy new syntax in .NET to handle this.
post messages,
The string "catdog" includes both "cat" & "dog" I don't see it as an error
per se... Any tricks to forcing "whole word" logic?
You could build the pattern such that each specific word needs non word \W
delimiters around it, or be at the beginning or end of the string.
Try something like:
Public Function [In](ByVal input As String, ByVal ParamArray words() As
String) As Boolean
Dim pattern As String = "(^|\W+)(" & String.Join("|" , words) &
")($|\W+)"
Return System.Text.Reg ularExpressions .Regex.IsMatch( input, pattern)
End Function
Hope this helps
Jay
"post messages" <po***********@ gmail.com> wrote in message
news:c4******** *************** ***@posting.goo gle.com...I have been trying to figure this out too. The regex you have for testing individual words is what I came up with too. Unfortunately, it also matches partial strings like catdog, dogcat, catty. Any tricks to forcing "whole word" logic?
<<snip>> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Roland Hall |
last post by:
Is there a way to return multiple values from a function without using an
array? Would a dictionary object work better?
--
Roland Hall
/* This information is distributed in the hope that it will be useful, but
without any warranty; without even the implied warranty of merchantability
or fitness for a particular purpose. */
Technet Script Center - http://www.microsoft.com/technet/scriptcenter/
WSH 5.6 Documentation -...
|
by: Chaz |
last post by:
Hello, I hope someone can help me out. I am going to be taking the
third step in a programming class soon(I took the previous two a while
ago at a different school) and in an effort to get back up to speed, I
bought the Dietel book How To Program 5th ed. I am going through some
examples trying to work them out, and have been doing pretty good, but
came across one exercise that has me stumped. It's a card shuffling
program that deals 5...
|
by: RJN |
last post by:
Hi
My web service receives an object of type say MyObject. I want to
serialize this object,and then validate the xml against the main xsd.
When validation happens,
it should also validate against the included schema.
The main schema includes one more schema and the actual types are
described in the included schema.
|
by: RJN |
last post by:
hi
My web service receives an object of type say MyObject. I want to
serialize this object,and then validate the xml against the main xsd.
When validation happens,
it should also validate against the included schema.
The main schema includes one more schema and the actual types are
described in the included schema.
|
by: Chris A via AccessMonster.com |
last post by:
I have an interesting problem that I have yet to come accross that I can't
change data structure on because it is an export from filemaker I am
reformatting for another dept. anyway. I have a table like so...
Table 1
Field1 Field2 Field3
E1 April 2006 AA, BB, CC
E2 April 2006 AA, BB, CC,DD, EE
E3 April 2006 AA, BB
| |
by: paul_zaoldyeck |
last post by:
does anyone know how to validate an xml file against multiple defined
schema?
can you show me some examples?
i'm making here an xml reader..
thank you
|
by: wswilson |
last post by:
In python, I could write:
a = 1
if a in :
do something...
In c (and many other languages):
|
by: sarankarthik |
last post by:
Hi,
I have some array of values,say for example
Attribute1 contains 1,2,3,4,5,6,7 values..
Please tell me how can i store multiple values in an xml file for a single variable.
Thanks,
SK>
|
by: Anil Verma |
last post by:
How to compare a SQL variable against multiple values:
Here is the scenario;
Declare @JobTitle varchar(10)
Select @JobTitle = JobTitle from tableName where xxx=xxx
Now I want to compare @JobTitle value against multiple strings without using OR in the IF statement?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |