Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21090

[RESOLVED] Need help to understand what this simple code does

$
0
0
Hi!

I need your help to truly undestand the following code:
Code:

Function NullNum(ByRef X As Object) As Object
        Dim s As Object
     
        On Error GoTo NullNum_Err
                NullNum = 0
                s = CStr(X)
                NullNum = X
                If NullNum Is Nothing Then NullNum = 0
        NullNum_Err:
End Function

My questions are:
1. What reason is the cast of X from Object to String for, if the result is assigned to an Object "s"? Is this where we would expect an Exception(Error) to be thrown if X cannot be cast to string? Or is this totally redundant, as "s" is not used anywhere?
2. Unfortunately none of the functions in the code are commented. So I am not even sure I undestand all possible use of this function. My primary idea would be that this function checks if the argument is a number, and would return the number if true and 0 if false. But if this function is called with a boolean expression, then it would return the boolean value of the expression, if the function is called with any object that can be casted to string, then it would return that object. So what does this function do really? I know it sounds stupid and I'm new to VB, I just want to understand this function as it is used massively in the whole project.

Besides, I am now upgrading the project to VB.NET, and the database connection will be upgraded to ADO.NET. So if this function just needs to check if a string argument is a number, then I will figure out a better way to do it in VB.NET.

In the project the function is called everywhere like in this function:
Code:

Function GetValue(ByRef my_id As Object) As Object
        Dim r As Object
               
        r = conn.Execute("select firstcol from tablename where id = " & CStr(my_id))
       
        If Not r.EOF Then
                       
                GetValue = NullNum(r("firstcol"))
                       
                If IsDbNull(r("firstcol")) Then
                               
                        GetValue = -1
                End If
        End If
               
        r.Close()
        r = Nothing
End Function


Viewing all articles
Browse latest Browse all 21090

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>