Results 1 to 14 of 14

Thread: [RESOLVED] Sending a Null argument to function

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Resolved [RESOLVED] Sending a Null argument to function

    I have the function that takes two parameters;

    Code:
    Public Function FInitEngineThread(argc As Integer, ByRef argv() As String) As Boolean
      '
      '
    End Function
    And I also have another function that calls the above but it has Null as one of it's arguments;

    Code:
    Public Sub VReadFromWinboard()
      '
      '
      '
      If Not FInitEngineThread(0, vbNull) Then
        '
        '
        '                                                           
      End If
      '
      '
    End Sub
    Of course this doesn't work. My question is how can I make it a valid call statement without sending a string array


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Sending a Null argument to function

    vbNull has a value of 1. When asked to send a null, you will send a 0& ByVal not ByRef

    Though with your function declaration, that won't work either, since it is a String array. Gonna need a bit more thought, but regardless, that function's last parameter probably will have to be changed to something else besides a String Array, i.e., Byte Array, Long, etc

    Is that function for an API call or local to your project? If local to your project, you can make the last parameter Variant. And either pass the String array or pass anything else & then test the variant contents like so:
    Code:
    If (VarType(Parameter) And (vbArray Or vbString)) then 
       ....
    Else
       ' not a string array, assume null or use another value
    End If
    
    ' sample then could be: If Not FInitEngineThread(0, Nothing) Then
    Last edited by LaVolpe; Oct 25th, 2014 at 12:47 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3
    Hyperactive Member
    Join Date
    Oct 2013
    Posts
    389

    Re: Sending a Null argument to function

    One word, Optional.
    But since you can't optional an array, make it a Variant, that way it can receive any variable type.

    Code:
    Public Function FInitEngineThread(argc As Integer, Optional ByRef argv As Variant) As Boolean
      '
      Debug.Print CStr( argv(0) )
      Debug.Print CStr( argv(1) ) 
      Debug.Print CStr( argv(2) ) 
    
    End Function
    Code:
    Private Sub Form_Load()
    
      Dim s(2) As String
          s(0) = "hey"
          s(1) = "hai"
          s(2) = "hao"
    
      Call FInitEngineThread(1, s)
    
    End Sub

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Sending a Null argument to function

    Quote Originally Posted by stum View Post
    One word, Optional.
    But since you can't optional an array, make it a Variant, that way it can receive any variable type.
    Good idea, but to test to see if it was passed: If IsMissing(argv) Then ...
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Sending a Null argument to function

    Thanks to both of you. This solves my problem


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  6. #6
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Sending a Null argument to function

    There are two options that don't use an optional variant:

    1) Pass an uninitialized dynamic string array.
    2) Pass a string array returned from Split("").

    Code:
    Private Sub Form_Load()
      Dim s() As String
      
      Test1 s
      s = Split("")
      Test2 s
    End Sub
    
    Private Sub Test1(arg() As String)
      If (Not arg) = -1 Then
        Debug.Print "arg is not initialized"
      Else
        ' ...
      End If
    End Sub
    
    Private Sub Test2(arg() As String)
      If LBound(arg) > UBound(arg) Then
        Debug.Print "arg is empty array"
      Else
        ' ...
      End If
    End Sub
    In Test1, (Not arg) treats arg as a pointer. For an uninitialized array, this value is zero so (Not arg) evaluates to -1.

    For Test2, you will get a compile error (type mismatch) if you try to pass Split directly. You can, however, wrap the Split in a function such as this:
    Code:
    Function EmptyStringArray() As String()
      EmptyStringArray = Split("")
    End Function

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: [RESOLVED] Sending a Null argument to function

    Logophobic, you really don't even need to use the Split function. The following will return an empty dynamic undimensioned string array:

    Code:
    Function EmptyStringArray() As String()
    End Function
    EDIT: Then jmsrickland can do something like:

    Code:
    Public Sub VReadFromWinboard()
      If Not FInitEngineThread(0, EmptyStringArray()) Then
        '
        '
        '
      End If
    End Sub
    Last edited by Elroy; Oct 26th, 2014 at 12:14 PM.

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] Sending a Null argument to function

    Quote Originally Posted by Elroy View Post
    Logophobic, you really don't even need to use the Split function. The following will return an empty dynamic undimensioned string array:
    Code:
    Function EmptyStringArray() As String()
    End Function
    Leaving the Split("") - or Split(vbNullString) - out of the function, will cause a differently
    filled return-value - and thus an entirely different behaviour in the calling code.

    In Logophobics function the String-Array-Variable will be filled with a valid allocation of a
    SafeArray-Struct (to be precise: with the value of a pointer to that allocation).

    In your version there's no such thing ensured - the String-Array-Variable will still contain a
    NullPointer (no assigned SafeArray) - and thus choke when asked to hand out LBound or UBound.

    Olaf

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: [RESOLVED] Sending a Null argument to function

    *nods at Olaf*

    But in my defense, he didn't say anything about checking bounds. And the bounds functions don't work an an undimensioned dynamic array either.

    Here's a simple function that should keep everyone out of trouble:

    Code:
    Function IsDimensioned(vArray As Variant) As Boolean
        On Error Resume Next
        If Not IsArray(vArray) Then Exit Function
        If LBound(vArray) > UBound(vArray) Then Exit Function
        If Err <> 0 Then Exit Function
        IsDimensioned = True
    End Function
    EDIT: Hmmm, and just thinking about this. What if his FInitEngineThread function was expecting a two dimensional array in the argv() variable? The LBound and UBound would still fail, even with the use of Split. UBound(argv, 2) = error. *shrugs*
    Last edited by Elroy; Oct 26th, 2014 at 01:46 PM.

  10. #10
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Sending a Null argument to function

    I gave two examples: Test1 checks for an undimensioned array (null pointer) and Test2 checks for invalid array bounds.

    I think this is moot, though, as jms' other threads suggest that his code, translated from C, should have the second argument as string, not array of strings.

  11. #11

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Sending a Null argument to function

    Quote Originally Posted by Logophobic View Post
    I gave two examples: Test1 checks for an undimensioned array (null pointer) and Test2 checks for invalid array bounds.

    I think this is moot, though, as jms' other threads suggest that his code, translated from C, should have the second argument as string, not array of strings.
    I'm beginning to think you are correct about the string. I'm going to change all var_name(nn) as String to var_name as String * nn although the functions in the C code take the argument as char * argv[]


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  12. #12
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] Sending a Null argument to function

    Quote Originally Posted by jmsrickland View Post
    I'm beginning to think you are correct about the string. I'm going to change all var_name(nn) as String to var_name as String * nn although the functions in the C code take the argument as char * argv[]

    The notation: char * argv[]
    is normally used in the main-entry-point of an application, to specify
    commandline-arguments (in a String-Array).

    In case you do a complete porting of the C-Code to VB6, then you could stick
    with just passing a VB6-StringArray - which is selfdescribing its current
    member-content over LBound and Ubound, so you wouldn't need to specify
    the additional argc-argument the C-Version requires to describe the array-member-count:
    ( int argc, char * argv[])

    Only if you plan to address a *compiled* C-Dll (making Calls into its API per VB-Declared functions),
    you would have to do some preparations on your VB-String-Array before passing it to a
    C-API-method which got its signature defined in that way.

    To be able to help in that latter case - I'd like to see your VB-Declare for that function beforehand.


    Olaf

  13. #13

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: [RESOLVED] Sending a Null argument to function

    Where ever I saw char * argv[] as an argument in the C parameter list of a function I made it ByRef argv() As String (I know ByRef is VB default I just do it anyway)

    Where ever I saw char * argv[256] defined in the code I made it Dim argv(256) As String


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,253

    Re: [RESOLVED] Sending a Null argument to function

    Quote Originally Posted by jmsrickland View Post
    Where ever I saw char * argv[] as an argument in the C parameter list of a function I made it ByRef argv() As String (I know ByRef is VB default I just do it anyway)

    Where ever I saw char * argv[256] defined in the code I made it Dim argv(256) As String
    I take it, that we talk about a complete port of the C-Sources and not about "wrapping a C-Dlls-API"...

    As for your above definition:
    Dim argv(256) As String

    allocating 256 Array-Elements in VB (and to match C's "ZeroBoundNess"),
    you would need to define a Ubound one less than the Element-Count:
    Dim argv(0 to 255) As String

    The Elementcount of VBs SafeArrays can be calculated commonly as:
    Code:
    Function ElementCount(Arr) As Long
    On Error Goto ReturnZero
      ElementCount = UBound(Arr) - LBound(Arr) + 1
    ReturnZero:
    End Function
    The above function would hand out 256 (as in the C-definition) to you.

    Olaf

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