Introduction to VB 6 Week 5 3302004 PPCC

Introduction to VB 6 Week 5 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 1

Review: Select Case n n We have seen that the IF statement can be used to check a value, and that we can use ELSEIF constructs to create multiple independent criteria. This works well for one or two comparisons, but gets more difficult when we want to compare a single expression to multiple values. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 2

Review: n Select Case The statement Select Case does just that: Select Case x Case 0 'Code to run if x=0 Case 1 'Code to run if x=1 Case 2 'Code to run if x=2 Case else 'Code to run otherwise End Select 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 3

Review: Select Case n Variations: Select Case x Case 0, 3 '… Case 4 to 8 '… Case Is < 0 '… End Select 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 4

Review: Select Case n Full syntax: n SELECT CASE <Expression> n n Case <Expression>, <Expression> To <Expression>, Is <Comparison. Operator> <Expression> There can be any number (1+) of Case statements in a Select Case A single Case statement can have a list of match conditions, separated by commas The <Expression> in the Select Case is compared to each of the Case expressions until a match is found 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 5

Review: n String Comparisons Like Operator n result = string Like pattern If string matches pattern, result is True; if there is no match, result is False. If either string or pattern is Null, result is Null. n The behavior of the Like operator depends on the Option Compare statement. The default stringcomparison method for each module is Option Compare Binary. n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 6

Review: n String Comparisons Pattern Matching with the Like operator n string Like pattern n 3/30/2004 pattern may contain Characters in pattern Matches in string ? Any single character * Zero or more characters (any) # Any single digit [0 -9] [charlist] Any single character in charlist [!charlist] Any single character NOT in charlist PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 7
![Review: n String Comparisons Pattern Matching, contd. [charlist] can use ranges: [A-Za-z 0 -9] Review: n String Comparisons Pattern Matching, contd. [charlist] can use ranges: [A-Za-z 0 -9]](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-8.jpg)
Review: n String Comparisons Pattern Matching, contd. [charlist] can use ranges: [A-Za-z 0 -9] n If you want to match against any of the special characters ? , *, #, [, or ] n Place them in []: n s. Text Like "[[][*][]]" n n n Returns TRUE if s. Text is "[*]" Useful for partial matching: n 3/30/2004 If s. Last. Name Like "Bost*" Then PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 8

String Comparisons n Create a small test app to play with pattern matching: n New Project n n n n Label, Caption: "String" Text. Box, Name: "txt. String", Text: "1 for all* and all for 1" Label, Caption: "Pattern" Text. Box, Name: "txt. Pattern", Text: "#*#" Command. Button, Name: "cmd. Like", Caption: "Like? " Label, Name: "lbl. Result", Caption: "" Private Sub cmd. Like_Click() n n 3/30/2004 lbl. Result. Caption = CStr(txt. String. Text Like txt. Pattern. Text) End Sub PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 9

String Manipulation n LSet Statement n The LSet statement Left aligns a string within a string variable when the two variables are strings n n …Or copies a variable of one user-defined type to another variable of a different user-defined type when the variables are user-defined types n n LSet stringvar = string LSet varname 1 = varname 2 LSet is not commonly used. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 10

String Functions n Asc(string), Chr(charactercode) n i. Ascii. Code = ASC("A") n n Gives us the ASCii code of the first character in the parameter passed to the function. s. Char = Chr$(65) n 3/30/2004 Returns a string containing the character whose ASCII code is specified in the parameter to the function. PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 11

String Functions n CStr(expression), Str(number) n n These convert the expressions in their parameters to String expressions CStr takes any type except Null as a parameter Str only works with numbers, and adds a leading space when the number is positive. Str only accepts the decimal point (. ) as decimal separator – Use CStr() for applications that should work with other languages/in other countries. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 12

String Functions n CStr, continued: If expression is CStr returns Boolean A string containing True or False Date A string containing a date in the short date format of your system Null A run-time error Empty A zero-length string ("") Error A string containing the word Error followed by the error number Other numeric A string containing the number 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 13
![String Functions n Filter(sourcearray, matchstr[, include[, compare]]) Compares each element in the sourcearray against String Functions n Filter(sourcearray, matchstr[, include[, compare]]) Compares each element in the sourcearray against](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-14.jpg)
String Functions n Filter(sourcearray, matchstr[, include[, compare]]) Compares each element in the sourcearray against the matchstr, and returns an array that contains only the elements that include (include = True or not specified) or do not include matchstr. n The compare parameter specifies the type of comparison to be performed n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 14

String Function n Filter, Contd. n compare can be: Constant Val Description vb. Use. Compare. Option NO LONGER AVAIL! -1 Performs a comparison using the setting of the Option Compare statement vb. Binary. Compare 0 Performs a binary comparison vb. Text. Compare 1 Performs a textual comparison vb. Database. Compare 2 Microsoft Access only. Performs a comparison based on information in your database 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 15

String Functions n Format, Format. Date. Time, Format. Currency, Format. Number, Format. Percent n n VB provides several functions to format data of various types as strings. The most versatile is Format() itself – it gives the user control over the format to be presented With an explicit format specification, international considerations are not considered Format. Date. Time, Format. Currency, Format. Number, Format. Percent use the configured local Date/Time formats, currency symbol, decimal and thousands separators, etc. PPCC - Introduction to VB 6 3/30/2004 Copyright © 2004, Tore Bostrup 16
![String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) expression is the value to convert to String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) expression is the value to convert to](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-17.jpg)
String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) expression is the value to convert to a string n format is the string that specifies the n formatting of the returned string n 3/30/2004 This can be a "named" format, for instance "Long Date", "Currency", or a user-specified string where different characters have special meaning PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 17
![String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) n firstdayofweek use local setting (vb. Use. String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) n firstdayofweek use local setting (vb. Use.](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-18.jpg)
String Functions n Format(expression, format[, firstdayofweek[, firstweekofyear]]]) n firstdayofweek use local setting (vb. Use. System), or specify weekday number (vb. Sunday to vb. Saturday) n n firstweekofyear These are mostly used in week number calculations, etc. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 18

String Functions n Format, contd. n firstdayofweek 3/30/2004 vb. Use. System 0 Use NLS API Setting (local default) vb. Sunday 1 Sunday (Default) vb. Monday 2 Monday vb. Tuesday 3 Tuesday vb. Wednesday 4 Wednesday vb. Thursday 5 Thursday vb. Friday 6 Friday vb. Saturday 7 Saturday PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 19

String Functions n Format, contd. n firstweekofyear 3/30/2004 vb. Use. System 0 Use NLS API setting (local default) vb. First. Jan 1 1 Start with week in which January 1 occurs (default) vb. First. Four. Days 2 Start with the first week that has at least four days in the year vb. First. Full. Week 3 Start with the first full week of the year PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 20
![String Functions n Instr([startpos, ] string, findstring[, compare]) Instr searches for findstring in string String Functions n Instr([startpos, ] string, findstring[, compare]) Instr searches for findstring in string](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-21.jpg)
String Functions n Instr([startpos, ] string, findstring[, compare]) Instr searches for findstring in string starting from startpos (if specified), and returns the character position where a match starts. n compare specifies the type of comparison to use n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 21
![String Functions n Instr. Rev(string, findstring[, startpos[, compare]]) n n n Instr. Rev is String Functions n Instr. Rev(string, findstring[, startpos[, compare]]) n n n Instr. Rev is](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-22.jpg)
String Functions n Instr. Rev(string, findstring[, startpos[, compare]]) n n n Instr. Rev is similar to Instr, but it searches for findstring in string starting from the end of the string and working itself forward, and returns the character position where a match starts. . If startpos is specified, matching starts with that position. startpos = -1 or not specified means start from end of string (same as default). compare specifies the type of comparison to use 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 22

String Functions n LCase(expression), UCase(expression) n These convert the characters in the expression strings to Lower Case and Upper Case respectively. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 23

String Functions n Left(expression, charactercount), Right(expression, charactercount) n These return the substring consisting of the charactercount leftmost/rightmost characters in expression 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 24

String Functions n Len(expression), Len. B(expression) Len returns the length (number of characters) in the string specified n Len. B returns the number of BYTES in the string specified. If the string is a Unicode string, then Len. B(s. Unicode. Str) = 2*Len(s. Unicode. Str) n Len. B is only used in special cases, typically with a few API's n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 25
![String Functions n Str. Comp(string 1, string 2[, compare]) compare is the same as String Functions n Str. Comp(string 1, string 2[, compare]) compare is the same as](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-26.jpg)
String Functions n Str. Comp(string 1, string 2[, compare]) compare is the same as seen before n Str. Comp compares the two string and returns n If Str. Comp returns string 1 is less than string 2 -1 string 1 is equal to string 2 0 string 1 is greater than string 2 1 string 1 or string 2 is Null 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup Null 26

String Functions n LTrim(string), RTrim(string), Trim(string) LTrim removes leading spaces from a text (Left Trim) n RTrim removes trailing spaces from a text (Right Trim) n Trim removes both leading and trailing spaces n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 27
![String Functions n Mid(string, start[, length]) n Mid returns a substring of string, starting String Functions n Mid(string, start[, length]) n Mid returns a substring of string, starting](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-28.jpg)
String Functions n Mid(string, start[, length]) n Mid returns a substring of string, starting with the character at position start, and containing length number of characters. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 28
![String Functions n Replace(expression, find, replace[, start[, count[, compare]]]) n n Replaces the first String Functions n Replace(expression, find, replace[, start[, count[, compare]]]) n n Replaces the first](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-29.jpg)
String Functions n Replace(expression, find, replace[, start[, count[, compare]]]) n n Replaces the first count occurrences of the find substring in expression with the replace string, starting from the start position and using the compare type of match, returning the string starting from the start position. Default start is 1 Default count is – 1 (meaning all) Default compare is Binary if Option Compare is not defined, follows Option Compare otherwise. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 29

String Functions n Space(number), String(number, character) Space creates a string containing the specified number of spaces n String creates a string containing the specified number of the character parameter. The character parameter can be a string (uses the first character of the string) or a character code. n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 30
![String Functions n Split(expression[, delimiter[, limit[, compare]]]) n Split creates an array of expression String Functions n Split(expression[, delimiter[, limit[, compare]]]) n Split creates an array of expression](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-31.jpg)
String Functions n Split(expression[, delimiter[, limit[, compare]]]) n Split creates an array of expression text fragments that are separated by delimiter in expression. The limit parameter specifies the (maximum) number of array elements to be returned, and therefore limit – 1 splits actually takes place. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 31
![String Functions n Join(sourcearray[, delimiter]) Join creates a string from the sourcearray of strings String Functions n Join(sourcearray[, delimiter]) Join creates a string from the sourcearray of strings](http://slidetodoc.com/presentation_image_h2/9f2917188bc21e035a69cfc23eadd4b9/image-32.jpg)
String Functions n Join(sourcearray[, delimiter]) Join creates a string from the sourcearray of strings by placing delimiter between each element in the array. n An array (arr. Tell) ("Tell", "Me", "More") joined by the statement n s. Joined = Join(arr. Tell, " ") n Results in the string n "Tell Me More" in s. Joined. n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 32

String Functions n Str. Reverse(expression) n The string result of expression is reversed, I. e. "abc" becomes "cba". 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 33

String Manipulation Exercise n n You have received a mailing list with names entered in different ways. There are no middle names in the list, but some names include titles, etc. n The titles may be: n n n "Mr. ", "Mrs. ", "Ms. " at the beginning of the string ", Jr. " or ", Sr. " after the name Names may be entered as n n [Title] First Last[Title] Last, First[Title] 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 34

String Manipulation Exercise n Our job is to write a subroutine that receives the name string as one parameter, and outputs the name into four separate Text. Boxes n n txt. Prefix, txt. First, txt. Last, txt. Suffix We also need to create a "test harness" for the subroutine n In this case, a project with a form that allows us to type in an arbitrary name entry, calls the subroutine, and contains the Text. Boxes for the output. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 35

String Manipulation Exercise n n You will at least use Like with pattern matching as well as Instr, Left, and Mid, and you can also make good use of Select Case See Sample Project Split. Name 2. vbp for code 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 36

String Manipulation Exercise n Form Label 1(0) txt. Full. Name cmd. Split. Name txt. Prefix 3/30/2004 txt. First. Name PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup txt. Last. Name txt. Suffix 37

String Manipulation Exercise n The Code n Starting with cmd. Split. Text_Click(): Private Sub cmd. Split_Click() txt. Prefix. Text = "" txt. First. Text = "" txt. Last. Text = "" txt. Suffix. Text = "" Split. Name txt. Full. Name. Text End Sub 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 38

String Manipulation Exercise n We need a n Sub Split. Name(By. Val s. Full. Name As String) n 3/30/2004 This function will determine what the original string looks like, and split out the different parts. PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 39

String Manipulation Exercise n The different "allowed" name layouts n n n n Prefix First Last, Suffix Prefix Last, First, Suffix Prefix First Last Prefix Last, First Last, Suffix Last, First, Suffix First Last, First 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 40

String Manipulation Exercise n Allowed Prefixes Mr. n Mrs. n Ms. n n Allowed Suffixes Jr. n Sr. n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 41

String Manipulation Exercise n By using the Pattern Matching, we can simplify this some: n Remember * matches any string n [xy] matches one character that is an x or a y n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 42

String Manipulation Exercise n So we have the following match conditions n n n n M[rs]. *, [JS]r. Mrs. *, [JS]r. M[rs]. * Mrs. * *, [JS]r. * * can actually be "First Last" or "Last, First" 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 43

String Manipulation Exercise n Construct a Select Case statement that tests against those cases using the following approach: n Select Case True 3/30/2004 n Case s. Full. Name Like "Pattern" n … PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 44

String Manipulation Exercise n n Show the initial code sample (Name. Split. vbp) Show the changes to arrive at the final code sample (Name. Split 2. vbp) 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 45

More controls (Option Button, Check. Box, Frame) n Look at a typical Print dialog box: Option Buttons Checkboxes Frames 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 46

More controls (Option Button, Check. Box, Frame) n A Check. Box can be Checked, Unchecked, and Disabled n n Any number of Check. Boxes can be in any state An Option Button can be selected or unselected n n Only ONE Option Button within the same container can be selected Selecting another Option Button will unselect the previously selected Option Button in the group (on the same container) 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 47

More controls (Option Button, Check. Box, Frame) n Frames are used for "visual grouping" of related input controls. n Frames are Containers – meaning that you can place other controls on it 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 48

Containers n Containers are controls (or windows) that can contain other controls (members) n n Place a control on a container, and move the container – the member controls follow the container. Container Controls: n n 3/30/2004 Picture. Box Frame Tabbed Dialog Cool. Bar PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 49

Excercise n n n Create a New Project (Standard EXE) Place three Option Buttons on the form Run the project Select one of the option buttons n Select another n n Now stop the application 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 50

Excercise n n Add a Frame to the form Place three Option Buttons on the Frame Drag the frame to a different position Run the project n n n Select one of the option buttons outside the Frame Select one of the option buttons inside the Frame Select another option button outside the Frame Select another option button inside the Frame Now stop the application 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 51

Excercise n n Select the Frame in Design view Copy and Paste onto form n Notice how the Member Controls were included 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 52

Excercise n Option Buttons Best Practice n Name Option Buttons that are in a single group with the same name (i. e. make them a control array) n 3/30/2004 The easiest way to do this is by creating the first Option Button in the group, naming it and setting any other common properties, and then use Copy & Paste to create the other Option Buttons in the group. PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 53

Control Arrays n We already saw an example of a Control Array in the Calculator user interface Multiple controls (of same type) with the same name, distinguished by an Index property n Share a single set of Event Handler procedures with an Index parameter supplied n Index does not need to be a set of contiguous values! n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 54

Control Array Exercise n n Create a New Project Add a Label, a Text. Box and a Command. Button Label Name: "Label 1", Caption: "Character" n Text. Box Name: "txt. Char", Text: "" n Command. Button Name: "cmd. Is. Bit. Set", Caption: "Is Bit n Set? " n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 55

Control Array Exercise n Add an Option Button n n Copy the Option Button and Paste n n Name: "opt. Bit. No", Caption: "Bit 0" Answer YES to create a Control Array Move the new Option Button to its location Modify Caption to "Bit 1" Repeat Paste & Move new option button n n Modify Caption to "Bit X" where X is 1 greater than in the previous option button's caption. Repeat until you have 8 option buttons 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 56

Control Array Exercise n Add a second label Name: lbl. Result n Caption: "" n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 57

Control Array Exercise n Double Click the button in design mode n n You should be in the code window inside n Private Sub cmd. Is. Bit. Set_Click() n End Sub Enter the code: n If mi. Bit. Mask and ASC(txt. Char. Text) <> 0 Then n n Else n n 3/30/2004 lbl. Result. Caption = "True" lbl. Result. Caption = "False" End If PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 58

Control Array Exercise n But we don't have a variable named mi. Bit. Mask… Drop down the control selection in code window and select (General) n Type in (under Option Explicit): n n 3/30/2004 Private mi. Bit. Mask As Integer PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 59

Control Array Exercise n n Next, we need some code to set the bitmask: Double click one of the Option Buttons in the form designer, or drop down the control selection combo in the code window and select opt. Bit. No n This brings up the Click event for the options 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 60

Control Array Exercise n Add the following code: Private Sub opt. Bit. No_Click(Index As Integer) mi. Bit. Mask = 2 ^ Index End Sub n This results in the bitmask being set when we click one of the options. 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 61

Control Array Exercise n Two things remain: Initializing the bitmask and option n Input Validation n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 62

Control Array Exercise n Initializing the bitmask and option We should set one of the options as selected n By setting it in the Form_Load event (code), we ensure that mi. Bit. Mask also gets initialized n Private Sub Form_Load() n n n opt. Bit. No(0). Value = True End Sub 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 63

Control Array Exercise n Input Validation Make sure txt. Char. Text is not empty n A space is a valid character for this app, so we should not Trim() the text n n Private Sub cmd. Is. Bit. Set_Click() n n n 3/30/2004 If txt. Char. Text = "" Then n Msg. Box "You must first specify a character" n Exit Sub End If … the remainder of the code is already entered PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 64

Control Array Exercise n n n Run the application Enter a character Select a bit number Click "Is Bit N Set? " Try a few subsequent characters with Bit 0 selected We get TRUE for every one! n We have a BUG in our APP!!! n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 65

Control Array Exercise n What is the problem: Is our mi. Bit. Mask value incorrect? n Is our IF-test wrong? n 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 66

Control Array Exercise n It turns out that our IF-test is wrong because of OPERATOR PRECEDENCE. n If mi. Bit. Mask And Asc(txt. Char. Text) <> 0 n n Remember: Arithmetic operators are evaluated first, next the Comparison operators, and last the Logical Operators So we get: n n 1: Asc(txt. Char. Text) <> 0 => True 2: mi. Bitmask (=1) And True (-1) => 1 3: 1 is different from False (0), and the expression is therefore considered True Add parentheses to enforce order of operations n 3/30/2004 If (mi. Bit. Mask And Asc(txt. Char. Text)) <> 0 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 67

Control Array Exercise n Instead of using the statement mi. Bit. Mask = 2 ^ Index n in the opt. Bit. No_Click event, we could set the Index properties of each of the options to the corresponding value: 1, 2, 4, 8, 16, 32, 64, 128 and use mi. Bit. Mask = Index Try it! 3/30/2004 PPCC - Introduction to VB 6 Copyright © 2004, Tore Bostrup 68
- Slides: 68