Asc, AscW Functions

   
Asc, AscW Functions

Class

Microsoft.VisualBasic.Strings

Syntax

 Asc(   string   ) AscW(   str   ) 
string , str (required; String or Char)

Any expression that evaluates to a nonempty string

Return Value

An Integer that represents the character code of the first character of the string. The range for the returned value is 0 - 255 on nonDBCS systems, and -32768 to 32767 on DBCS systems.

Description

Returns an Integer representing the character code for the first character of the string passed to it. All other characters in the string are ignored

Rules at a Glance

  • The string expression passed to the function must contain at least one character or a runtime error is generated.

  • Only the first character of the string is evaluated by Asc or AscW .

Example

 Dim sChars As String Dim iCharCode As Integer      sChars = TextBox1.Text If Len(sChars) > 0 Then    iCharCode = Asc(sChars)    If iCharCode >= 97 And iChar <= 122 Then       MsgBox "The first character must be uppercase"    End If End If 

Programming Tips and Gotchas

  • Check that the string you are passing to the function contains at least one character using the Len function, as the following example shows:

     If Len(sMyString) > 0 Then     iCharCode = Asc(sMyString) Else     MsgBox("Cannot process a zero-length string") End If 
  • Use Asc within your data-validation routines to determine such conditions as whether the first character is upper- or lowercase and whether it is alphabetic or numeric, as the following example demonstrates :

     Private Sub Button1_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _                           Handles Button1.Click Dim sTest As String Dim iChar As Integer      sTest = TextBox1.Text If Len(sTest) > 0 Then    iChar = Asc(sTest)    If iChar >= 65 And iChar <= 90 Then       MsgBox "The first character is UPPERCASE"    ElseIf iChar >= 97 And iChar <= 122 Then       MsgBox "The first character is lowercase"    Else       MsgBox "The first character isn't alphabetical"    End If Else    MsgBox "Please enter something in the text box" End If      End Sub 
  • Use the Asc function and the related Chr function to create rudimentary encryption methods . Once you have obtained the character code for a particular character, you can perform calculations on this code to come up with a different number and then convert this to a character using the Chr function. To decrypt your string, simply reverse the calculation. You may want to avoid character codes less than 20, however, since these can be interpreted as special nonprinting characters and cause undesirable effects if displayed or printed.

     Private Sub CommandButton2_Click(  ) Dim MyEncryptedString, MyDecryptedString As String Dim MyName As String = "Paul Lomax" Dim i As Integer For i = 1 To Len(MyName)    MyEncryptedString = MyEncryptedString & _                        Chr(Asc(Mid(MyName, i, 1)) + 25) Next i MsgBox("Hello, my name is " & MyEncryptedString) For i = 1 To Len(MyName)    MyDecryptedString &= Chr(Asc(Mid(MyEncryptedString, i, 1)) - 25) Next i MsgBox("Hello, my name is " & MyDecryptedString) End Sub 

See Also

Chr, ChrW Functions

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net