473,327 Members | 2,025 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,327 software developers and data experts.

switch a string to an integer

I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx
Nov 20 '05 #1
8 1393
Try this funtion:
Public Funtion LetterValue(c as char) as integer
if Asc(c) >96 then
return Asc(c) - 96
else
return Asc(c)- 64
end if
End Function

HTH

Jim Mirra
"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #2
If you expect uppercase letters then how about

intValue = ASC(strLetter) - 64

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #3
Got it if anybody is interested..

AscW(strLetter - 64)

will convert to its appropriate number
-----Original Message-----
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx
.

Nov 20 '05 #4
How about...

Public Function GetNum(ByVal c As Char) As Integer
Return Asc(c.ToUpper(c)) - 64
End Function

This handles both upper and lowercase

Regards,

Martin.

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

Nov 20 '05 #5
Martin,
I would recommend two changes:
Public Function GetNum(ByVal c As Char) As Integer
Return AscW(Char.ToUpper(c)) - 64
End Function
Seeing as Char.ToUpper is a shared method, its more obvious in your code if
you give the Class name (Char) when you call it as oppose to the variable.
Although you can use a Char variable to call the method, it appears that you
are acting on that instance of the variable, when you are not, you are
acting on the parameter...

Also using AscW function will not attempt to translate the char in relation
to the current code page.

Otherwise I think your sample is the "most flexible".

Hope this helps
Jay

"Martin Horn" <mv****@ntlworld.com> wrote in message
news:10****************@ersa.uk.clara.net... How about...

Public Function GetNum(ByVal c As Char) As Integer
Return Asc(c.ToUpper(c)) - 64
End Function

This handles both upper and lowercase

Regards,

Martin.

"El Camino" <an*******@discussions.microsoft.com> wrote in message
news:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx


Nov 20 '05 #6
Thank you all for your very good advice response
:-)
-----Original Message-----
If you expect uppercase letters then how about

intValue = ASC(strLetter) - 64

"El Camino" <an*******@discussions.microsoft.com> wrote in messagenews:01****************************@phx.gbl...
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4

wihtout goiing threw and extensive selection process....
Maybe a function that im not aware of....
and so on...
Thanx

.

Nov 20 '05 #7
* "El Camino" <an*******@discussions.microsoft.com> scripsit:
I need to switch a string to an integer

if its strLetter = "A"
integer value of 1

if its strLetter = "B" then
integer value of 2

if its strLetter = "C" then
integer value of 3

if its strLetter = "D" then
integer value of 4


\\\
Dim s As String = "A"
Dim i As Integer = Asc(s) - 64
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #8
Cor
Hi Martin,

Nice one, to make it more generic I would add

\\\
Public Function GetNum(ByVal c As Char) As Integer
If AscW(Char.ToUpper(c)) - 64 > 0 AndAlso AscW(Char.ToUpper(c)) - 64 < 27
Then
Return AscW(Char.ToUpper(c)) - 64
Else
Return 0
End if
End Function
///

Just my thought.

Cor
Nov 20 '05 #9

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

Similar topics

3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
18
by: Torben Laursen | last post by:
Hi Can anyone tell me what is the short-cut to switch between a .h and .cpp file, thanks Torben Laursen ---
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
7
by: zwasdl | last post by:
When I swtich to design view from data view, the query results are lost, and I have to run again to get it. Is there an option in access to keep the data when I switching views? I know I can copy...
0
by: Sudz28 | last post by:
Greetings! I am attempting to write a program that will allow a user to manipulate data read from a file, and am obviously in no way near finished. However, one problem I'm having that I don't...
3
by: Drowningwater | last post by:
I'm writing a program and I've made a switch statement. I have several quesions: 1. I have a string variable and when I try to use that string variable with the switch function I get a compiling...
21
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
9
by: Cybex | last post by:
I am trying to get this to work but when ever I enter an proper integer it just hangs. The Switch default seems to catch the improper integers but the right ones are not triggering the way I...
27
by: sinbad | last post by:
how to implement a switch case for stringse...switch should happen depending on the strings... thanks sinbad
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.