Vs2003 -> Vs2005 = unassigned Nothing?

mafrosis

Well-known member
Joined
Jun 5, 2006
Messages
88
Location
UK
Programming Experience
5-10
Hey all, I have a little problem which I have solved but am looking for a touch of advice. The upgrade from '03 to '05 has be smooth enough, but there is a little change which seems stupid to me - and therefore Im wondering if im doing something fundamentally wrong.

A MS-SQL DB method is as follows:

VB.NET:
Public Function Execute(ByVal sql As String) As Int32
        Dim cn As SqlClient.SqlConnection
        Dim cmd As SqlClient.SqlCommand = Nothing
        Dim ret As Int32
        
        Try
            cn = New SqlClient.SqlConnection(Me.ConnectionString)
            cn.Open()
        
            cmd = New SqlClient.SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = sql
            
            ret = cmd.ExecuteNonQuery()
            Return ret
            
        Catch ex As Exception
            msgbox(ex.Message)
            Return Nothing
            
        Finally
            If Not cmd Is Nothing Then cmd.Dispose()
            If Not cn Is Nothing Then cn.Dispose()
        End Try
    End Function
I end up with a warning on the last line of the Try..Catch telling me 'cn' may not have been initialised. Fair enough, but as you can see if I explicitly set 'cmd' to Nothing when it's created the warning on that line disappears.

So what exactly is the correct approach here? Is a variable not null (Nothing) before it is assigned?!? This seems to go against all my experience of C and Java.. :confused:

Thanks all
 
Last edited:
It's just a warning. It can see that your initialization of the variable is inside the Try.... but Finally will ALWAYS fire... error or not. So there's a possibility that is COULD get into the Finaly section BEFORE the variable is properly initialized. Since you are checking for it's Nothingness before actualy doing something with it, the warning can be safely ignored.

-tg
 
I assume that if you had set cn to Nothing in the initializer, that would have been an operation that would not (could not) have failed. But in the first line of your try block, you assign:
VB.NET:
 cn = New SqlClient.SqlConnection(Me.ConnectionString)

So now either 1) New might fails (out of memory), or 2) the constructor for SqlConnection might fail, in which case the throw would happen before cn is actually assigned, and then since you didn't initialize it to nothing, cn.Dispose() might be called in the Finally block (which would of course be an error).

But I wonder if VB automatically sets objects to Nothing, i.e. the following two statements are identical, in which case your code is OK either way:
VB.NET:
Dim cn1 As Sql.Connection ' Automatically assigned to  nothing by the compiler?
Dim cn2 As Sql.Connection = Nothing
 
The difference between these two statements:
VB.NET:
Dim cn1 As Sql.Connection ' Automatically assigned to  nothing by the compiler?
Dim cn2 As Sql.Connection = Nothing
is that in the second one you are explicitly telling the compiler that you want the variable to refer to Nothing and you will take responsibility for making sure that it is not Nothing when required. The first one implicitly sets (or leaves) the variable to Nothing and therefore the compiler doesn't know whether you may have simply forgotten. It is "proper" and "correct" to initialise every variable before it is or might be used, even if you initialise it to Nothing. C# actually enforces this. If you used the equivalent code in C# it would produce an error rather than a warning and the project would not compile. This is an example of how VB 2005 is treading the line between the old easy-to-use-but-easy-to-mess-up VB6 style of coding and the do-it-by-the-book-whether-you-like-it-or-not C/C++ style of coding.
 
Last edited:
Ok guys thanks for that, it still seems stupid to me that you have to initialise a variable as null - take database's as an example, a field with a value of null implies that it has *never had* a value - not just that it doesnt have one.

Im yet to do any C# in 2005, but do you really have to initialise every variable to be equal to null (or something valid)??!

Crazy business
 
In C# the first use of any variable has to be an assignment. It's that simple. It's not necessarily an initialisation because the declaration of the variable and the assignment of the valu ecould be a long way apart. The reason it's done this way is because it is quite easy to write complex code that looks like it will assign a value to a variable but there may be some unexpected case where it doesn't happen. The compilers will detect that and warn you. You should be grateful that the compilers will find possible errors in your code and help you fix them before you release your app and have it possibly crash in use.
 
I am very grateful for the smart(ass)ness of modern compilers - theyve saved me much time and stress since using moving upto OO languages (segmentation fault anyone?!)

Really my only point is that by declaring a variable it should implicitly be equal to null - and therefore the compiler need not warn me that this may fail:

VB.NET:
If Not cmd Is Nothing Then cmd.Dispose()
Of course then when I change my declaration of 'cmd' to this - the compiler stops moaning:
VB.NET:
Dim cmd As SqlClient.SqlCommand = Nothing
Only a little gripe! VB has plenty more things that are backwards than that.. ie, And vs AndAlso and CBool, CInt breaking the object model...

Theres more than 2 cents there! :D
 
Back
Top