Results 1 to 4 of 4

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

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

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

    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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Need help to understand what this simple code does

    1) Yes, it is to cause an error if the value can't be converted to a string.


    2) I agree with your analysis of it, so it is a rather strange function.

    Based on various quality issues in the code for GetValue, I don't think you can rely on the behaviour of NullNum matching the intended design (and you can't even tell what the design is ).

    Assuming that the field firstcol is numeric, the GetValue routine should be rewritten to something like this (which doesn't need NullNum):
    Code:
    Function GetValue(ByRef my_id As Object) As Integer
            GetValue = -1
    	Dim r As ADODB.Recordset
    
            r = conn.Execute("select firstcol from tablename where id = " & CStr(my_id))
    	
    	If Not r.EOF Then
    		If Not r("firstcol") Is Null Then
    			GetValue = r("firstcol")
    		End If
    	End If
    		
    	r.Close()
    	Set r = Nothing
    End Function
    ...if for some reason you need to determine the difference between no record existing and there being a record with a null value, it will need to be changed slightly.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    134

    Re: Need help to understand what this simple code does

    Thank you very much! I needed to know that there is no special VB mechanism in this function that I dont undestand, before I change it.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

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

    The typical method to test a string to see if is numeric in VB6 is to use the IsNumeric() function
    In VB.Net you would use the Integer.TryParse() method or whatever type of number you are looking to use.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width