473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

compare/check character against an array of chars - best practice?

The procedure below checks if a character entered into a cell of a
datagridview is contained in a string array of valid characters for this
particular cell. It seems kludgy. I am asking what the best practice would
be. I was thinking I could use an arrayList which has the "contains"
property and do this:

If not arr.Contains(s1) then --- don't continue

but my list of codes might be about 50 char combinations. So I was thinking
a string array. But with the string array I can only think of checking the
value using loops in the following kludge procedure

Dim s1 As String, b1 As Boolean
s1 = StrConv(dgrv1.Rows(e.RowIndex).Cells(e.ColumnIndex ).Value.ToString,
VbStrConv.Uppercase)
b1 = False
If s1 <"" Then
Dim arr() As String = New String() {"C", "D", "F", "I", "N", "P", "R", "U"}
For Each str1 As String In arr
If s1.Equals(str1) Then
b1 = True
Exit For
End If
Next
If b1.Equals(False) Then
Beep()
MessageBox.Show("Invalid code for this cell")
dgrModSubDetail.Rows(e.RowIndex).Cells(e.ColumnInd ex).Value = ""
End If
End If

At least with this kludge I don't have 50 lines of code to load the
arraylist. What is the best practice to perform this procedure? Is there
any method like

If s1 Not In {"C", "D", "F", ...}

Thanks,
Rich
Nov 13 '06 #1
3 4279
=?Utf-8?B?UmljaA==?= <Ri**@discussions.microsoft.comwrote in
news:67**********************************@microsof t.com:
At least with this kludge I don't have 50 lines of code to load the
arraylist. What is the best practice to perform this procedure? Is
there any method like
Use Regular Expressions. You can write your check using one line like:

"^[ABCDEFG]" which means not A or B or C ... Regular Expressions can check
everything from patterns, to lengths, to variations of strings. In anycase,
check the RegEx documentation for more details on the syntax.

Also, are you using the cell validating event to handle the check>
Nov 13 '06 #2
>At least with this kludge I don't have 50 lines of code to load the
>arraylist. What is the best practice to perform this procedure? Is there
any method like

If s1 Not In {"C", "D", "F", ...}
Arrays implement IList which has a Contains method, so you can call

If CType(arr, IList).Contains(s1) Then ...

You can also use the Array.IndexOf method.

If you're always checking single characters, you could store them all
in a single String and use the String.IndexOf method

Dim characters As String = "CDFINPRU"
....
If s1.Length = 1 AndAlso characters.IndexOf(s1, 0) = 0 Then ...
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 13 '06 #3
Thank you very much. I knew it was something fairly simple.

Thanks,
Rich

"Mattias Sjögren" wrote:
At least with this kludge I don't have 50 lines of code to load the
arraylist. What is the best practice to perform this procedure? Is there
any method like

If s1 Not In {"C", "D", "F", ...}

Arrays implement IList which has a Contains method, so you can call

If CType(arr, IList).Contains(s1) Then ...

You can also use the Array.IndexOf method.

If you're always checking single characters, you could store them all
in a single String and use the String.IndexOf method

Dim characters As String = "CDFINPRU"
....
If s1.Length = 1 AndAlso characters.IndexOf(s1, 0) = 0 Then ...
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: deko | last post by:
Is there a way to check user input for illegal characters? For example, a user enters something into a text box and clicks OK. At that point I'd like to run code such as this: illegal =...
15
by: ehabaziz2001 | last post by:
Hi, Till now I do not understand how the null character automatically added to the end of the string and it is not an element of the string array . All books said the null character (\0) added...
8
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
4
by: Gaby | last post by:
Hi all, What is the best way to compare 2 (large) ArrayLists filled with an object. Can you please help me? Gaby
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
22
by: gene.ariani | last post by:
I have a string like the following: 10AF101-25 I would like to extract any numerical number that precedes the "-" and stops when it encounters any string character like AF So my result...
4
by: reva | last post by:
hi all!! can any one please help me in checking the two character arrays. in my code i need to compare a character array(seq) to that of hydrob and hydrop . if the seq has hydrob then it should be...
3
by: weetat | last post by:
Hi all, I need some advice regarding wrote a javascript function. The function purpose is to check the variable "selectedSeat", for e.g if the selectedSeat value is var selectedSeat =...
17
by: spasmous | last post by:
I need a way to search through a block of memory for a char array "DA" using a pointer to a short. Ideally I would like to write something like: short *data = ... some data...; int j = 0;...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...

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.