Question Obscure problem - optional parameter with empty string treated as Nothing!?

brendan.hill

Member
Joined
Jun 20, 2010
Messages
15
Programming Experience
10+
Weird. This code produces Nothing instead of "":

VB.NET:
Public Function get_value(Optional ByVal alternative_value As String = "") As Object
    Return alternative_value
End Function

dim d as object = get_value()


Why ideas why? It should return "" instead of Nothing as that's the parameter's default value.

Cheers,
-Brendan
 
Last edited:
Welcome to the forums :D

When posting code please use the code tags, while it doesn't make a huge difference when posting a line or two its still good practice and makes the code look prettier :p

It could be because you're returning an object rather a string.
Try supplying a string as a parameter and see if d is still nothing, if so then its not because you're returning an object rather than a string and we'll have to try and figure out what else could be causing this.
If its not that then could you supply the real method as I get the feeling that this is an example piece of code.

Anyway let us know if that helps

Satal :D
 
Welcome to the forums :D

When posting code please use the code tags, while it doesn't make a huge difference when posting a line or two its still good practice and makes the code look prettier :p

Sweet, done.


It could be because you're returning an object rather a string.
Try supplying a string as a parameter and see if d is still nothing, if so then its not because you're returning an object rather than a string and we'll have to try and figure out what else could be causing this.
If its not that then could you supply the real method as I get the feeling that this is an example piece of code.

Anyway let us know if that helps

This wasn't the problem, however I've isolated it further. It's even stranger than I thought (though less of a problem, thankfully). Turns out the function is correctly returning an empty string when run in code, but returns Nothing (or appears to) when returning in the Immediate window. Try this (copied directly from my code):

VB.NET:
Public Class Form1

    Public Function get_value(Optional ByVal alternative_value As Object = "") As String
        Return alternative_value
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim d As Object = get_value()
        MsgBox(d Is Nothing)

    End Sub
End Class

It MsgBoxes "False" - ie. the function has correctly returned an empty string.

However, put a breakpoint on MsgBox, then run this in the Immediate window, and you get:

VB.NET:
?d
"" {String}
    String: ""

?get_value()
Nothing

I can live with it, knowing that it works correctly in code, but it's very, very strange.

Cheers,
-Brendan
 
Well if d was declared as a String then that should sort that problem out. :)

Would that it were... not that simple! This code:

VB.NET:
Public Class Form1
    Public Function get_value(Optional ByVal alternative_value As String = "") As String
        Return alternative_value
    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim d As String = get_value()
        MsgBox(d Is Nothing) 'BREAKPOINT HERE
    End Sub
End Class

Still produces this in the immediate window:

VB.NET:
?d
""

?get_value()
Nothing

Which makes no sense. The Immediate window must be doing something fishy. Try it yourself - it's weird.

Anyway it's little more than a curiosity now that I know it won't affect production.

-Brendan
 
Looks like a bug in VB 2008 debugger/immediate, results are correct in VB 2010.
 
I'm getting a little confused about what you're trying to do.
Why wouldn't you just be doing something like
VB.NET:
        Dim d As String = get_value()
        If d = "" Then
            MsgBox("Zero Length String")
        Else
            MsgBox(d)
        End If
Surely you would be catching the result of the function in a string variable rather than just calling it and outputting the result?
 
I'm getting a little confused about what you're trying to do.
Why wouldn't you just be doing something like
VB.NET:
        Dim d As String = get_value()
        If d = "" Then
            MsgBox("Zero Length String")
        Else
            MsgBox(d)
        End If
Surely you would be catching the result of the function in a string variable rather than just calling it and outputting the result?

The code I posted was just to isolate the problem. The actual function I use is:

VB.NET:
Public Function is_null(ByVal value As Object) As Boolean
    Return (value Is Nothing) OrElse value.Equals(System.DBNull.Value)
End Function
Public Function check_string(ByVal value As Object, Optional ByVal alternative_value As String = "") As String
    Return IIf(is_null(value), alternative_value, value)
End Function

It's just a validation function for untrusted data (eg. "check_string(x)" is guaranteed to be a string, even if x isn't). I was testing the function and puzzled why it kept returning Nothing instead of "" in the Immediate window.

FYI In the past I used to simply say (x & "") to get a guaranteed string, but this has come to offend my delicate sensibilities :p (Now I prefer "check_string" for consistency as part of a broader set of functions check_boolean, check_date, check_numeric etc)

-Brendan
 
Back
Top