PDA

View Full Version : Case Insensitive input in Visual Basic.


Achán hiNidráne
24th February 2010, 08:05 AM
Hey all:

I'm working on a homework assignment in my Visual Basic class where I have to write a program that determines life expectancy based on series of inputed variables. On of the requirements for the assignment is that the inputs be case insensitive (i.e. When answering a Y/N question, either "Y" or "y" is acceptable.) I've poured over my book and the net and I can't seem to find out how to do this. Any suggestions?

Zax63
24th February 2010, 08:11 AM
Convert to upper or lower case before testing.

Response$ = UCASE$(Response$)
Response$ = LCASE$(Response$)

Test both:
IF Response$ = "Y" OR Response$ = "y" THEN


Several other methods will work also - SELECT CASE or use a bit operation to set/unset the bit that controls case. Both of those seem like overkill for the problem.

ETA: Just realized this is VB6, you are probably using .NET at this point. I'm sure similar functions/methods are available.

Achán hiNidráne
24th February 2010, 08:29 AM
ETA: Just realized this is VB6, you are probably using .NET at this point. I'm sure similar functions/methods are available.

Yeah, we're using VB8.

Zax63
24th February 2010, 08:33 AM
OK found I had Visual Studio .NET 2005 installed. UCASE and LCASE are still there but you can also use ToUpper and ToLower.


If txtInput.Text.ToUpper = "Y" Then
'answered Y or y
ElseIf txtInput.Text.ToUpper = "N" Then
'answered N or n
Else
'Invalid response
End If

Achán hiNidráne
24th February 2010, 11:33 AM
OK found I had Visual Studio .NET 2005 installed. UCASE and LCASE are still there but you can also use ToUpper and ToLower.


If txtInput.Text.ToUpper = "Y" Then
'answered Y or y
ElseIf txtInput.Text.ToUpper = "N" Then
'answered N or n
Else
'Invalid response
End If

Thank you! That worked great! :D