11.07.2015 Views

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

101 Tech Tips - Visual Studio Magazine - One-Stop Source Shop

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>101</strong> TECH TIPSFor <strong>Visual</strong> <strong>Studio</strong> DevelopersVB5, VB6Level: IntermediateUse Hidden Enum BoundsEnumerated parameters don’t prevent you from passing unenumerateddata to the function. Let’s say you have this enumeration:Public Enum geAccessTypeReadOnly = 1WriteOnly = 2ReadWrite = 3NoAccess = 4End EnumAnd you have this function:Public Function DoSomeJob( _eType As geAccessType) As LongMsgBox eTypeEnd FunctionYou can call this function like this:'call #1DoSomeJob ReadOnlyYou can also call it like this, passing a value other than theenumerated constants:'call #2DoSomeJob 45In any case, it works.If you add two variables to the enumeration and modify yourfunction implementation, you can prevent out-of-bounds casessuch as call #2 easily:Public Enum geAccessType[_minAccessType] = 1ReadOnly = 1WriteOnly = 2ReadWrite = 3NoAccess = 4[_maxAccessType] = 4End EnumPublic Function DoSomeJob( _eType As geAccessType) As LongSelect Case eTypeCase geAccessType.[_minAccessType] To _geAccessType.[_maxAccessType]MsgBox eTypeCase Else'raise error or do something elseEnd SelectEnd FunctionBy default, [_minAccessType] or [_maxAccessType] do not appearin the constant list. If you want to see them, open the ObjectBrowser, right-click inside it, and select Show Hidden Members.—Russ Kot, Northbrook, Ill.VB4/32, VB5, VB6Level: IntermediateInternational Test for Illegal CharactersI was interested to read the tip “Test for Illegal Characters” in the10 th Edition of the “<strong>101</strong> <strong>Tech</strong> <strong>Tips</strong> for VB Developers” supplement[<strong>Visual</strong> Basic Programmer’s Journal February 2000]. The tip, however,has two significant drawbacks as published. First, it requiresa function from the SHLWAPI DLL, which requires either Win98/2000 or Win95/NT with Internet Explorer 4.0 or higher. Second, itonly works, as presented, for U.S. (7-bit) character sets, requiringthose of us who work with international character sets (such asaccented characters) to consider which characters will be legalwhere our apps run.Luckily, Windows has the solution: the IsCharAlphaNumericfunction, defined in User32.dll. This function uses the currentlydefined locale when performing comparisons, thereby allowingfull use of accented characters. This sample demonstrates howyou might use this function:Public Declare Function IsCharAlphaNumeric Lib _"user32" Alias "IsCharAlphaNumericA" ( _ByVal cChar As Byte) As LongPublic Function IsAlphaNum(ByVal sInput As String) _As BooleanDim fCheck As BooleanDim i As Integer' Assume non-alphanumericfCheck = False' If we don't have any input, drop outIf Len(sInput) Theni = 0Doi = i + 1fCheck = _CBool(IsCharAlphaNumeric( _Asc(Mid$(sInput, i, 1))))Loop While fCheck And (i < Len(sInput))End IfIsAlphaNum = fCheckEnd FunctionYou may pass any single or multiple character string to thefunction IsAlphaNum. The return value will be True if all charactersare alphanumeric and False otherwise.Windows also has several other useful functions for workingwith characters in the current locale. Note, however, that allfunctions require a byte to be passed, which you can achieve bypassing the Asc( ) value of a given character (see previous example):' Check if a given character is alphabeticPublic Declare Function IsCharAlpha Lib "user32" _Alias "IsCharAlphaA" (ByVal cChar As Byte) _As Long' Check if a given character is lowercasePublic Declare Function IsCharLower Lib "user32" _Alias "IsCharLowerA" (ByVal cChar As Byte) _As Long' Check if a given character is uppercasePublic Declare Function IsCharUpper Lib "user32" _Alias "IsCharUpperA" (ByVal cChar As Byte) _As Long—John Cullen, Pedroucos, Portugal26 Supplement to <strong>Visual</strong> <strong>Studio</strong> <strong>Magazine</strong> SEPTEMBER 2001

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!