Results 1 to 8 of 8

Thread: [RESOLVED] Asc(65)

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    59

    Resolved [RESOLVED] Asc(65)

    I have a string that i am cutting up.
    the string is looped through looking at each character (MyStr)
    VB Code:
    1. If MyStr = Asc(65) Or Asc(66) Or Asc(67) Or Asc(68) Or Asc(69) Or
    2.  Asc(70) Or Asc(71) Or Asc(72) Or Asc(73) Or Asc(74) Or Asc(75) Or Asc(76)
    3.  Or Asc(77) Or Asc(78) Or Asc(79) Or Asc(80) Or Asc(81) Or Asc(82) Or
    4. Asc(83) Or Asc(84) Or Asc(85) Or Asc(86) Or Asc(87) Or Asc(88) Or Asc(89)
    5. Or Asc(90) Or Asc(97) Or Asc(98) Or Asc(99) Or Asc(100) Or Asc(101) Or
    6. Asc(102) Or Asc(103) Or Asc(104) Or Asc(105) Or Asc(106) Or Asc(107) Or
    7. Asc(108) Or Asc(109) Or Asc(110) Or Asc(111) Or Asc(112) Or Asc(113) Or
    8. Asc(114) Or Asc(115) Or Asc(116) Or Asc(117) Or Asc(118) Or Asc(119) Or
    9. Asc(120) Or Asc(121) Or Asc(122) Then
    the above code lists the ascii numbers for A-Z & a-z only! So it should only execute if the character in MyStr is A-Z or a-z but when the character in MyStr is space, or tab it is still processed!!
    can anyone tell me why?

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Asc(65)

    You cannot do that (as you are not comparing values, just doing boolean checks with non-zero values [which are therefore all True - so the If is always True]), it would need to be like this instead:
    VB Code:
    1. If (MyStr = Asc(65)) Or (MyStr = Asc(66)) Or (MyStr =Asc(67)) Or ....
    or alternatively, you could use more efficient code like this:
    VB Code:
    1. IF ((Asc(MyStr) >= 65) And (Asc(MyStr) <= 90)) Or ((Asc(MyStr) >= 97) And (Asc(MyStr) <= 122)) Then
    or even better:
    VB Code:
    1. Select Case MyStr
    2. Case "A" to "Z", "a" to "z"
    3.   'code here
    4. End Select

  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Asc(65)

    ?? Asc(65) = 54, Chr(65) = "A"

    I dont understand what u are doing?
    only allow capitals / lowercase letters?

    or u could do this.. but I like the select case better as shown above
    VB Code:
    1. If InStr(UCase(MyStr), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") <> 0 Then
    2.  
    3. End If
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Asc(65)

    but I still dont undertand why Asc() is being used.. thats not the correct way to use it???

    Asc()
    Returns anInteger representing the character code corresponding to the first letter in a string.

    Asc("A") would be correct...
    Chr(65) would be to test for "A"

    right??
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    59

    Re: Asc(65)

    Doh! Thanks people
    to hot to think straight is my excuse today

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Asc(65)

    if you're looping through every character of a string you'd be better off using a byte array.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Asc(65)

    Quote Originally Posted by Static
    but I still dont undertand why Asc() is being used.. thats not the correct way to use it???
    Doh!! Why didn't I spot that!

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Asc(65)

    "to hot to think straight", si.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width