Functions in vb 2005

jvcoach23

Centurion
Joined
May 22, 2003
Messages
192
Location
Saybrook, IL
I'm trying to write a function in a class. when I create the function, before I even put any real meat in the function,

i'm getting the warning that the function doesn't return a value on all code paths. A null reference exception could occur at run time when teh result is used.

I've done some digging but can't turn anything up. can anyone help me out on by explaining what is going on. I did this in vb 2003 and didn't run into it... hope someone can explain it to me.

thanks
shannon
 
Basically VisualStudio is warning you of a potential problem in your program. I don't know much about vb, but heres a c# example hopefully it will be self explanatory.

C#:
// function that returns a boolean
private bool MyFunction()
{
     if(a == b)
     {
            return true;
     }
}

In this example a simple function checks if two objects are equal, if they are it returns true, if they aren't the function doesn't return a value. VisualStudio is simply informing you that a logical code path through your function doesn't return a value. This can cause crashes in your application if for example you do..

Visual Basic:
Dim myBool as Boolean
myBool = MyFunction()

If myBool = True Then
'do something
End If

If MyFunction ends up following the logic path in MyFunction that doesn't return a value this could cause your application to crash.
 
In the underlying .Net engine, a function must place its return value on the stack. If it does not, the code will not be verifiable. Depending on the end user's settings, this will cause the program to either not run at all, or simply crash, so it is important that a value is returned.

If you are using VB, a default value is always placed on the stack. Allowing it to be returned could cause your program to crash. Most likely it will just produce odd or unexpected results. When you call a function it is expected behavior that the function will inspect input and provide a meaningful output. It is, therefore, considered good programming practice (or required programming practice in C#) that every possible code path explicitly returns a result so that a default value is not returned by accident.

With reference types, a code path that returns a default returns a null reference, which may cause a NullReferenceException to be thrown, and without an exception handler in place, this will crash your application. With value types, these unexpected default values could cause exceptions due to overflow, out of bounds array access, division by zero, etc.

In C# (at least 2.0, I'm not positive about 1.x) there is no provision for default values, therefore a value must be explicitly returned. Otherwise the resulting IL would be unverifiable and if allowed to run would crash the CLR.
 
Last edited:
So if i'm using a function that is making a database call and returning a dataset, then I need to make sure that that dataset has at least a table and row in the table.. so if the db call returns nothing, i need to manually add a row.. am i getting this..

thanks
shannon

marble_eater said:
In the underlying .Net engine, a function must place its return value on the stack. If it does not, the code will not be verifiable. Depending on the end user's settings, this will cause the program to either not run at all, or simply crash, so it is important that a value is returned.

If you are using VB, a default value is always placed on the stack. Allowing it to be returned could cause your program to crash. Most likely it will just produce odd or unexpected results. When you call a function it is expected behavior that the function will inspect input and provide a meaningful output. It is, therefore, considered good programming practice (or required programming practice in C#) that every possible code path explicitly returns a result so that a default value is not returned by accident.

With reference types, a code path that returns a default returns a null reference, which may cause a NullReferenceException to be thrown, and without an exception handler in place, this will crash your application. With value types, these unexpected default values could cause exceptions due to overflow, out of bounds array access, division by zero, etc.

In C# (at least 2.0, I'm not positive about 1.x) there is no provision for default values, therefore a value must be explicitly returned. Otherwise the resulting IL would be unverifiable and if allowed to run would crash the CLR.
 
Using the example you specified you need to ensure that a dataset is always returned. If your database call returns null then you either have to return a null dataset (in which case you should check for this in the place you call the function to prevent null reference exceptions) or as you say you could create a new dataset manually and return that. What you shouldn't do is naturally exit the function without a return Dataset statement.
 
playing around some more.. i have figured out that when i use a try and catch that that is when I'm getting my error. So i need to make sure I return a dataset in the catch and the error goes away... not sure if i want to do that.. but that is why i'm getting the warning.. thanks again for your help

shannon

Cags said:
Using the example you specified you need to ensure that a dataset is always returned. If your database call returns null then you either have to return a null dataset (in which case you should check for this in the place you call the function to prevent null reference exceptions) or as you say you could create a new dataset manually and return that. What you shouldn't do is naturally exit the function without a return Dataset statement.
 
If you don't want to return a dataset do something like this...

Visual Basic:
    Private Sub myFunction()

        Dim mySet As DataSet
        mySet = getDataSet()

        If mySet Is Nothing Then
            ' handle null value
        Else
            ' proceed with dataset
        End If

    End Sub

    Private Function getDataSet() As DataSet

        Dim mySet As DataSet

        Try
            mySet = New DataSet
            Return mySet
        Catch ex As Exception
            Return Nothing
        End Try

    End Function
 
Back
Top