Results 1 to 3 of 3

Thread: command line trouble

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Posts
    24

    Post

    I'm trying to implement a few command line arguments in my program, but I get some real strange errors...
    Here's the code:
    Code:
    Public Function GetCommandLine(Optional MaxArgs As Integer)
    On Error GoTo Error
    Dim chCurrentChar, i
    Dim szCmdLine, szOmitted As String
    Dim nCmdLnLen, nArgs As Integer
    Dim blnInArg As Boolean
     nArgs = Int(0)
     blnInArg = False
    
    If IsMissing(MaxArgs) Then
        MaxArgs = Int(10)
    End If
    
    'Make array the correct size
    ReDim ArgArray(MaxArgs)
    
    szCmdLine = Command()
    MsgBox szCmdLine
    nCmdLnLen = Len(szCmdLine)
    
    'Go through command line one character at a time and check for spaces or _
     tabs, if no space or tab then add it to the current arg string
    For i = 1 To nCmdLnLen
        chCurrentChar = Mid(szCmdLine, i, 1)
    
        If (chCurrentChar <> " " And chCurrentChar <> Chr$(9)) Then
            'Neither space nor tab
            'Test if already in argument
            If Not blnInArg Then
                'New argument begins.
                'Test for too many arguments
                If nArgs >= MaxArgs Then
                    szOmitted = Right(szCmdLine, Int(nCmdLnLen - i))
                    MsgBox "Warning: Too many parameters specified, possible data loss has occurred..." + Chr$(13) + "Omitted: {" + szOmitted + "}", vbCritical, "An error has occurred"
                    Exit For
                End If
                
                nArgs = nArgs + Int(1)
                blnInArg = True
            End If
            'Concatenate character to current argument
            ArgArray(nArgs) = ArgArray(nArgs) + chCurrentChar
        Else
            'Found a space or tab, so set blnInArg flag to False
            blnInArg = False
        End If
    Next i
    
    'Resize array so the UBound is the last parameter
    ReDim Preserve ArgArray(nArgs)
    
    GetCommandLine = ArgArray()
    End Function
    It seems to think that if MaxArgs is not specified it's 0, so it breaks immediately and gives the too many params warning. Please help me, this has really got me stumped.


    Doomstar
    http://surf.to/Doomstar

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    For otptional parameter if you're not using Variant data type then you would need to specify the default value. The reason for that is because Integer has a default value of 0 (zero), so you can't check if it is missing. You can do something like this:

    Code:
    Public Function MyFunction(Optional pParameter As Integer = -1)
        If pParameter = -1 Then 'No Value was passed
           'Do something
        Else
            'Do something else
        End If
    End Function

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2000
    Posts
    24

    Post

    Thanks, I'd already solved the prob myself by removing the As Integer part and checking it with IsNumeric(), but this is even better!


    Doomstar
    http://surf.to/Doomstar

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