< Day Day Up > |
Branching, looping, and conditional execution are the fundamental building blocks of programming. Almost every one of the mechanisms listed in Table 1.4 is slightly syntactically different in the two languages, although clearly, you can accomplish the same things in either language. Table 1.4. Flow of Control
Following the table, I've made a few notes on how each construct works. IIF and SWITCH are included here as well because they replace what used to be four or five lines of IF / ENDIF or CASE / ENDCASE code. Calling a Function, Procedure, or MethodTo call a procedure called ProcName in FoxPro, you either type DO ProcName or simply ProcName() . The parentheses are required. The second variant is the way you call a subroutine or function in Visual Basic .NET. If you instantiate an object named MyObj that has a method called ProcName , you call it using MyObj.ProcName . If it has parameters, you can add parentheses after the name and put the parameters within them. Otherwise , the parentheses are not required. In Visual Basic .NET the syntax is exactly the same, except that DO ProcName() is not supported. String handlingThe well-known Canadian developer Steven Black has pointed out in several articles that string handling in FoxPro is thousands of times faster than it is in Visual Basic 6. In Visual Basic .NET, the String module contains dozens of methods that are more efficient. Type the word "String" and a period in a code window, and Intellisense will show you what's available. In addition to the string functions found in FoxPro, String.Format can be used like TextMerge: FoxPro: Name = "Fred" Age = 32 SET TEXTMERGE ON NOSHOW TEXT TO X <<Name>> is <<TRANSFORM(age)>> years old. ENDTEXT ? X VB: Dim Name As String = "Fred" Dim Age As Integer = 32 Label1.Text = String.Format("{0} is {1} years old", Name, Age) Ending Your ProgramIn FoxPro, RETURN takes you out of the current procedure, THISFORM.Release closes the current form, and QUIT ends the program. RETURN from the MAIN program also ends the application, unless you're running MAIN from inside the FoxPro IDE. So a compiled application can use RETURN to end the program. In Visual Basic .NET, CLOSE() closes a form; End ends the program. You can't end a program with Return in Visual Basic. Do...ENDDO and Other Loops in Visual FoxProIn FoxPro, DO...ENDDO is used to do something repetitively until a condition is satisfied. In the following FoxPro example, we test a printer object, and if its ErrorOccurred property is set to .T. , or if its PrintedOk property is set to True , we exit the loop; otherwise, we keep trying. DO WHILE .T. oPrinter.Test IF oPrinter.ErrorOccurred or oPrinter.PrintedOk EXIT ENDIF ENDDO DO WHILE is not as frequently used as its cousin FOR ... ENDFOR or FOR...NEXT . The syntax is either FOR I = 1 TO N - do something - ENDFOR NEXT or FOR EACH obj IN Collection - do something - ENDFOR NEXT Finally, in FoxPro, where we have tables and cursors , there is a special command pair to traverse the table or cursor from top to bottom: SCAN - do something - ENDSCAN The Visual Basic equivalent is to use a FOR EACH row IN table loop: Dim dr as datarow For each dr in dataset.tables(0) ... End For Loops in Visual Basic .NETIn Visual Basic .NET, the equivalent of FOR...NEXT is very similar: FOR I = 1 TO N - do something - NEXT I However, DO...LOOP has four variants. The first two check the expression at the beginning of the loop, whereas the last two check the expression at the end of the first loop:
If that isn't enough, there's a While...Wend construct that's similar to DO...LOOP , except that it doesn't support Exit : While condition statements Wend Conditional Execution in Visual FoxProIn Visual FoxPro you can use either of these two mechanisms to execute only a certain block of code: IF Expr - Execute if true - ELSE - Execute if not true - ENDIF or DO CASE CASE Expr1 - Execute if Expr1 is true - CASE Expr2 - Execute if Expr2 is true - ENDCASE It's a peculiarity of FoxPro's DO CASE construct that the expressions evaluated in the CASE statements don't have to refer to the same variable. It's very flexible. Conditional Execution in Visual Basic .NETAs usual, Visual Basic .NET has more constructs. I don't know if that's good or bad: Sometimes although we're offered several ways of doing things, one of them is really the preferred method. Choose returns the numbered entry from a list of choices, for example: Return Choose(idx, "Lions", "Tigers", "Bears") Choose is a primitive construct from years ago, and will seldom if ever be the best choice. It has been deprecated. If Expr Then...Else...End IfIf the evaluated expression is true, the first statement is executed; if Else is present and a second statement follows it, the second statement is executed, for example: If Salary > 1000000.00 Then Bonus = 1000000.00 Else Bonus = 25.00 End If Note that a single line implementation is permitted. If it improves readability, use it. When I'm writing code for publication I use the single-line implementation. You can also include several statements separated by colons, but it gets hard to read pretty fast. If Salary > 1000000 Then Bonus = 1000000 If Salary > 1000000 Then Bonus = 1000000 Else Bonus = 25 If Salary > 10000 Then Salary = Salary * 2 : Bonus = 10000 Else Salary = Salary / 2: Bonus = 25 Select CaseThis command evaluates a single variable's values, for example: Select case StateName Case "CA" Governor = "Arnold" Case Else Governor = "Someone else" End Select Switch( VisualBasic Namespace )This statement evaluates a series of pairs of expressions and their corresponding return values and returns the first value whose expression evaluates to True , for example: Return Microsoft.VisualBasic.Switch( _ CityName = "London", "English", _ CityName = "Rome", "Italian", _ CityName = "Paris", "French") Note that the Switch function name is not unique in the .NET namespaces and so must be qualified. This version is from the Visual Basic compatibility namespace provided for people who are inordinately fond of Visual Basic6. I'd be sparing about using this function; it looks a little dated. In summary, both languages offer very similar capabilities in the flow of control area. I like FoxPro's DO CASE statement, which allows all sorts of expressions, not just different values for the named variable or expression. But the differences are small. |
< Day Day Up > |