Visual BasicR. NET Unleashed

Team-Fly    

 
Visual Basic .NET Unleashed

By Paul Kimmel

Table of Contents
Appendix A.  VB6 Programming Element Changes in VB .NET

Several flow control statements were removed in Visual Basic .NET. I didn't think anyone was still using these statements, but apparently at least one developer was disappointed, perhaps even upset, that GoSub is no longer supported in Visual Basic .NET.

The fact is that GoSubs result in spaghetti code and should not be used anyway. (The last time I wrote one was in GW-BASIC.) Several flow control features have been revised from VB6 to Visual Basic .NET.

Replace GoSub with Function Call

The GoSub is no longer supported. Where you employed a GoSub in your VB6 code, Visual Basic .NET requires that you replace the GoSub with a function call. The following Command button event handler demonstrates a GoSub in VB6.

Private Sub Command1_Click() GoSub RetroCode MsgBox "I'm dizzy!" Exit Sub RetroCode: Return End Sub

When I opened the VB6 project GoSubDemo.vbp in Visual Basic .NET, the migration wizard kicked in and modified the code. The first thing I looked at was the Upgrade Report (shown in Figure A.4). The report clearly indicates that GoSub is no longer supported and the Return keyword has been reassigned. (The report and the upgraded GoSubDemo project are contained on this book's Web site.)

Figure A.4. The migration wizard will modify code and create an upgrade report helping you resolve unsupported problems like anachronistic GoSub statements.

The migration wizard was unable to resolve the code; a comment suggesting a resolution and resource material was provided instead.

Private Sub Command1_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Command1.Click 'UPGRADE_ISSUE: GoSub statement is not supported. 'Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup1014.htm GoSub RetroCode MsgBox("I'm dizzy!") Exit Sub RetroCode: 'UPGRADE_WARNING: Return has a new behavior. 'Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup1041.htm Return End Sub

As you can quickly determine, the migrated code is almost identical to the VB6 code but will not compile. In the migrated code you will have to manually resolve the GoSub problem. If your procedures are short, manually resolving these kinds of problems will not be too difficult. To fix the problem, we need to replace the GoSub subroutine with a function or subroutine. In this case a subroutine is fine for our do-nothing block:

Private Sub Command1_Click(ByVal eventSender As System.Object, _ ByVal eventArgs As System.EventArgs) Handles Command1.Click RetroCode() MsgBox("I'm dizzy!") End Sub Private Sub RetroCode() End Sub

A good place to start if you are having trouble unraveling code is Martin Fowler's book Refactoring: Improving the Design of Existing Code .

On..GoSub and On..Goto Are Not Supported

The computed On..GoSub and On..GoTo are not supported in Visual Basic .NET. You can replace these constructs with Select Case statements in Visual Basic .NET. The following examples demonstrate an On..GoSub statement (Listing A.2) followed by a Visual Basic .NET revision (Listing A.3) that uses the Select Case instead.

Listing A.2 A computed On..GoSub

Private Sub Form_Click() Dim Value As Integer Value = 1 On Value GoSub RetroCode MsgBox "I'm dizzy again!" Exit Sub RetroCode: MsgBox "Retro": Return End Sub

Listing A.3 Replacing computed On..GoSub and On..Goto statements with a Select Case statement in Visual Basic .NET

Private Sub Form1_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Click Dim Value As Integer = 1 Select Case Value Case 1 : MsgBox("Retro") End Select MsgBox("I'm dizzy again!") End Sub

The Select Case statement clause performs the evaluation and branches to the Case block containing the suitable matching value.


Team-Fly    
Top
 

Категории