Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,
 
I have a text box...I need to validate the textbox value against multiple values like "4567","6754","7842"
 
How do i do them without using OR or elseif
Dim validCodes As String() = New String() {"5962", "5966", "5967", "7995")
 
I tried storing those multile values in an array and then txtSICCode.Contains(validCodes)...
 
This does not work.Please help.
Thanks.
Posted 22-Feb-13 8:39am
vidkaat528
Edited 22-Feb-13 8:46am
v2
Comments
BC @ CV at 22-Feb-13 15:13pm
   
Is txtSICCode a string pulled from someTextBox.Text, if not then the problem is that you are using the Contains from the TextBox object and it is looking through child controls not the text property value. See Solution 3.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Use a Select Case statement:
Select Case myTextBox.Text
	Case "1234"
		Console.WriteLine("1")
		Exit Select
	Case "2345"
		Console.WriteLine("2")
		Exit Select
End Select
  Permalink  
Comments
vidkaat at 22-Feb-13 14:47pm
   
I dont want to go for a switch case as all the checks has the same error message.
ThePhantomUpvoter at 22-Feb-13 14:55pm
   
Why did you not tell us in your original posting that you was getting an error message?
OriginalGriff at 22-Feb-13 15:03pm
   
I suspect he means if it matches, it's an error. I could well be wrong though, he doesn't seem too clear.
vidkaat at 22-Feb-13 15:10pm
   
i just wanted to know the logic of comparing the textbox value against multiple values. I tried putting the multiple values in an array and then comparing it.But it does not seem to work.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You did it backwards. You asked if the text in the textbox Contains a character.
 
You need to do it the other way around.
    If validCodes.Contains(myTextBox.Text)
Keep in mind that this is not a "production quality" piece of code as this method is case- and whitespace-sensitive. If the user types " 2345", it will not be found in the valid code list.
  Permalink  
Comments
vidkaat at 22-Feb-13 14:56pm
   
i tried that...But i am not getting Contains when i place a dot after Validcodes...No clue y?
Dave Kreskowiak at 22-Feb-13 15:30pm
   
Yeah, like I said, if the user doesn't type the code EXACTLY as it appears in your list, it's not going to match. If your code is 4 characters long and they type 5 characters, bzzzzzz, it's no longer a match.
 
You haven't provided enough detail about the input you're dealing with because you just said if they type "1234.". More detail about your input, like it's expected format and any options, is needed to come up with any kind of solution.
vidkaat at 22-Feb-13 15:34pm
   
Dave,
 
My input is an integer which is my text box value. I am already doing the validation for my textbox to be an intger and of length 4...So no worries abt that. Now i need to compare my textbox value which is an integer against multiple values like 5432,6521,7654...Hope u get my point.
 
Thanks.
Dave Kreskowiak at 22-Feb-13 17:15pm
   
Oh, I see what happened. My mistake. I never use arrays for much anymore. I usually use List<type>. If you do this, it works:
List<string> validCodes = new List<string> { "5962", "5966", "5967", "7995" };
if (validCodes.Contains(sometext.text)) ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use the Contains of the Text property like so:
 

For Each code As String In validCodes
	If txtSICCode.Text.Contains(code) Then
		DoSomething()
	End If
Next
 
  Permalink  
v2
Comments
vidkaat at 22-Feb-13 15:20pm
   
i TRIED THIS .iT SHOWS AN Error"Valueof 1-dimensional array of string cannot be converted to String"...
BC @ CV at 22-Feb-13 15:43pm
   
Check the updated solution.
vidkaat at 22-Feb-13 16:20pm
   
Thank u so much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 383
1 OriginalGriff 304
2 George Jonsson 258
3 Animesh Datta 130
4 Shemeemsha RA 128
0 OriginalGriff 6,099
1 Sergey Alexandrovich Kryukov 5,411
2 CPallini 4,770
3 George Jonsson 3,400
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100