Tuesday, March 18, 2008

Upgrading from Visual Studio 2003 .NET 1.1 to Visual Studio 2005 .NET 2.0 Project

No matter how great your Application works with .NET 1.0 or 1.1, sooner or later people around you are going to start talking about the many advantages of converting it to a 2.0 Project. Not just because the grass is always greener on the other side, but because it is a known fact that the .NET 2.0 Development and Runtime Environment are a lot more stable then 1.0 or 1.1. In addition, Windows Vista does not by default ship with the .NET 1.0 or 1.1 Framework, and although it is possible to run .NET 1.1 on Vista it is NOT recommended to do so (which usually means you can expect plenty of bugs if you do use 1.1).


The actual conversion process from a Visual Studio 2003 (.NET 1.1) Project to a Visual Studio 2005 (.NET 2.0) Project is in fact an automated process. That still however does NOT mean that you won't have any errors and warnings.


So what are some of the Errors and Warnings you are likely to encounter during this conversion process? Bellow is a short list of some of the more common ones and a suggested way to solve the problem. This list is a list of common errors along with a suggested solution that “should” work in most cases. Please note that each occurrence of the warning or error needs to be evaluated on a case by case basis to make sure that it does not change the overall flow, meaning or functionality of the application:

1. Option Strict On disallows operands of type Object for operator ‘=’

Before:

Select Case loComboBox.SelectedItem
Case "Value 1"
' Code Here
Case "Value 2"
' Code Here
...
End Select


Corrected:

Select Case CType(loComboBox.SelectedItem)
Case "Value 1"
' Code Here
Case "Value 2"
' Code Here
...
End Select


2. Variable ‘VariableName’ is used before it has been assigned a value

Before:

Dim loVariable As Object

Corrected:

Dim loVariable As Object = Nothing

3. Variable ‘VariableName’ is passed by reference before it has been assigned a value

Before:

Dim lsVariable As String
Me.Add(New User(lsVariable))


Corrected:

Dim lsVariable As String = Nothing
Me.Add(New User(lsVariable))

4. Unused local variable: ‘VariableName’

In most cases if the variable is actually not used anywhere it can be deleted. If public and inside of object, it will need to be initiated to “Nothing”.

5. Unreachable code detected

War This warning simply means that you have written a code segment which can not ever be reached. For example:

Before:

Public Function MyFunction (ByVal lsParameter As String) As String
Dim lsString as String = lsParameter
Return lsString

' Do some more work here that doesn't get executed ever
Return lsString2
End Function

Corrected:

Public Function MyFunction (ByVal lsParameter As String) As String
Dim lsString as String = lsParameter
Return lsString
End Function


6. Type of Parameter ‘ListViewName’ is not CLS-compliant

Mark Parameter as CLS Non-Compliant using “ _”.

7. Type of Member ‘MemberName’ is not CLS-compliant

Mark Member as CLS Non-Compliant. using “ _”.

8. Name ‘VariableName’ is not CLS-compliant

Mark Variable as CLS Non-Compliant using “ _”.

9. Return type of function ‘FunctionName’ is not CLS-compliant

Mark Function as CLS Non-Compliant using “ _”.

10. Variable declaration without an ‘As’ clause; type of Object assumed.

Before:

Private LandingLockObject = New Object

Corrected:

Private LandingLockObject As Object = New Object

11. Function ‘FunctionName’ does not return a value on all code paths

Before:

Public Function SpecialKey(ByVal keydata As Keys) As String Implements iTerminalEmulator.SpecialKey
Dim lsReturn As String

Select Case keydata
Case Keys.Left
lsReturn = Chr(8)
Case Keys.Down
lsReturn = Chr(10)
Case Keys.Back
lsReturn = Chr(8)
Case Keys.Escape
lsReturn = Chr(27)
Case Keys.Tab
lsReturn = Chr(9)
Case Else
lsReturn = ""
End Select
End Function


Corrected:

Public Function SpecialKey(ByVal keydata As Keys) As String Implements iTerminalEmulator.SpecialKey
Dim lsReturn As String

Select Case keydata
Case Keys.Left
lsReturn = Chr(8)
Case Keys.Down
lsReturn = Chr(10)
Case Keys.Back
lsReturn = Chr(8)
Case Keys.Escape
lsReturn = Chr(27)
Case Keys.Tab
lsReturn = Chr(9)
Case Else
lsReturn = ""
End Select
Return lsReturn
End Function

12. Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Happens when either a Shared member in a class is not referenced through the class name, or two programming elements have the same name (for example you have a function and a variable both of which have the same name). To solve, either use the name of class or structure that defines the Shared Member to access it, or look through the code and either rename the variable or the function to something different.


Pete Soheil
DigiOz Multimedia
http://www.digioz.com

2 comments:

Thriveni said...

I have got an error due to conversion for .net 1.1 to 2.0. Could u help me with them as well:
1) Implicit conversion from 'Object' to 'String' in copying the value of '------' parameter '----' back to the matching argument.

Thriveni said...

Question 2) Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.