448,799 Members | 1,376 Online
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

# switch a string to an integer

 P: n/a 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 Replies

 P: n/a 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" 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

 P: n/a If you expect uppercase letters then how about intValue = ASC(strLetter) - 64 "El Camino" 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

 P: n/a 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 integerif its strLetter = "A"integer value of 1if its strLetter = "B" theninteger value of 2if its strLetter = "C" theninteger value of 3if its strLetter = "D" theninteger value of 4wihtout goiing threw and extensive selection process....Maybe a function that im not aware of....and so on...Thanx. Nov 20 '05 #4

 P: n/a 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" 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

 P: n/a 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" 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" 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

 P: n/a Thank you all for your very good advice response :-)-----Original Message-----If you expect uppercase letters then how aboutintValue = ASC(strLetter) - 64"El Camino" 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

 P: n/a * "El Camino" 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] Nov 20 '05 #8

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion.