473,785 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.R ows(e.RowIndex) .Cells(e.Column Index).Value.To String,
VbStrConv.Upper case)
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.RowInde x).Cells(e.Colu mnIndex).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 4313
=?Utf-8?B?UmljaA==?= <Ri**@discussio ns.microsoft.co mwrote in
news:67******** *************** ***********@mic rosoft.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.Inde xOf(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.Inde xOf(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
42430
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 = Array(\, /, :, *, ?, ", <, >, |) If Me.TextBox Contains illegal Then MsgBox "You entered illegal characters. Please try again." Me.TextBox = Null
15
2150
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 automatically to the end of the string. Let say char name="123456789" If entered the name in a loop;
8
4496
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 with weird stuff, just strings, int, bools and DataTime values) My fields values for this record, for convenience are also in an array of objects. Now I am trying to write code like the following. private void processData (object d, object a)...
4
7590
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
29472
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
22
1512
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 should be 101.
4
7816
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 given a different colour. if it has a hydrop then it should be assigned a different colour. if the sequence does not have a character that is neither in hydrob and hydrop then they shuld be given a separate colour. for (int...
3
2006
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 = "A:01|A:03|A:05|B:01|B:02|B:03"; var selectedArray = selectedSeat.split("|");
17
3478
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; while( data != *((short*) "DA") ) j++; But this doesn't work. The char obviously has an equivalent 16-bit value so how do I get that info in a simple way?
0
10346
Oralloy
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...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10096
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8982
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7504
isladogs
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...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
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
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
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...

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.