Home > Articles > Web Development

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Step 1: Choose a Small Piece of Code

This first step is the most important. The problem of upgrading an application is mostly one of scope. The prospect alone can be daunting! The best place to start is small. Dig into your code; find a piece that's comfortable, maybe a spot where you've already made fixes. The smaller, the better—starting with a function is better than replacing a whole class. I know, I know—it would be so much easier to attack a whole class or set of functionality, right? But we don't want to take the easy approach; we want to take the repeatable approach. Unless you're very lucky, you'll be working on this upgrade while you're maintaining the old code; small chunks will get you there faster.

"But I've got dependencies!" you cry. "My chosen code won't work without classes x and y!" This is a valid argument, but classes x and y will have their own turns, thank you very much. Use placeholder and dummy data when you're upgrading your snippets. Trust your architecture, get the code done, and worry about dependencies when the necessary pieces are in place.

For this example, I've chosen a small piece of pretty common code—a function to map a drive in Visual Basic 6. We're going to upgrade it to C#. Nice and simple. Listing 1 shows the full function.

Listing 1—Function to map a drive in Visual Basic 6.

Private Const RESOURCE_CONNECTED As Long = &H1&
Private Const RESOURCE_GLOBALNET As Long = &H2&
Private Const RESOURCETYPE_DISK As Long = &H1&
Private Const RESOURCEDISPLAYTYPE_SHARE& = &H3
Private Const RESOURCEUSAGE_CONNECTABLE As Long = &H1&

Private Const CONNECT_UPDATE_PROFILE = &H1

Private Declare Function WNetAddConnection2 Lib "mpr.dll" _
  Alias "WNetAddConnection2A" (lpNetResource As NETCONNECT, _
  ByVal lpPassword As String, ByVal lpUserName As String, _
  ByVal dwFlags As Long) As Long

Private Type NETCONNECT
   dwScope As Long
   dwType As Long
   dwDisplayType As Long
   dwUsage As Long
   lpLocalName As String
   lpRemoteName As String
   lpComment As String
   lpProvider As String
End Type


Public Function MapDrive(LocalDrive As String, _
  RemoteDrive As String, Optional Username As String, _
  Optional Password As String) As Boolean

'Example:
'MapDrive "Q:", "\\RemoteMachine\RemoteDirectory", _
'"MyLoginName", "MyPassword"

   Dim NetR As NETCONNECT

   NetR.dwScope = RESOURCE_GLOBALNET
   NetR.dwType = RESOURCETYPE_DISK
   NetR.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
   NetR.dwUsage = RESOURCEUSAGE_CONNECTABLE
   NetR.lpLocalName = Left$(LocalDrive, 1) & ":"
   NetR.lpRemoteName = RemoteDrive

   MapDrive = (WNetAddConnection2(NetR, Username, Password, _
       CONNECT_UPDATE_PROFILE) = 0)


If MapDrive = True Then
        lstScripts.AddItem "Drive " & LocalDrive & " mapped to " & RemoteDrive
Else
    lstScripts.AddItem "Unable to Map " & LocalDrive & " to " & RemoteDrive
End If
End Function
  • + Share This
  • 🔖 Save To Your Account