Results 1 to 8 of 8

Thread: [RESOLVED] Parameter name: length

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [RESOLVED] Parameter name: length

    I have never understood these messages so i am attempting to work it out.

    Does the below message say that a paremeter in the module LogFile has a zero length?
    On my pc clsStream.Write(bFile, 0, bFile.Length) holds values but on 2 w10 pc's this has caused errors.

    Code:
    ************** Exception Text **************
    System.ArgumentOutOfRangeException: Length cannot be less than zero.
    Parameter name: length
       at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
       at System.String.Substring(Int32 startIndex, Int32 length)
       at TakeoffExpress.LogFile.gGetInfo()
       at TakeoffExpress.Form1.Form1_Load(Object sender, EventArgs e)
       at System.EventHandler.Invoke(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    Code:
    Module LogFile
    
       Public Sub uUpLoadLogFile()
    
            'deleted code here to save your eyes
    
              clsRequest.Method = System.Net.WebRequestMethods.Ftp.AppendFile
                'read into existing log.txt file...
                Dim bFile() As Byte = System.IO.File.ReadAllBytes(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "/TakeoffExpress/Log/Log.txt")
                ' upload file...
                Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
                clsStream.Write(bFile, 0, bFile.Length)
                clsStream.Close()
                clsStream.Dispose()
    
          'deleted code here to save your eyes
      End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Parameter name: length

    That stack trace is telling you that, in the Load event handler of Form1, you are calling LogFile.gGetInfo and, in that method, you're calling String.Substring and passing a negative number for the 'length' parameter. Obviously you can't get a substring with a negative length. I suggest that you place a breakpoint on that method call in the Load event handler and then you can step to the Substring call and see where that negative length is coming from.

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Parameter name: length

    on my pc it hold values and unfortunately i cannot test on the clients pc's so would this work?

    in runs though the code fine on my machine....

    Code:
     If bFile.Length = 0 Then
                Else
                    clsStream.Write(bFile, 0, bFile.Length)
                End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Parameter name: length

    Read my post again and look at the stack trace you posted. That line of code has got exactly zero to do with the issue.

  5. #5

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Parameter name: length

    sorted thx.

    i have code to count how many times a file is opened each month and on 2 pc's it was causing an error for some reason when it hit a month that has a zero opened times.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Parameter name: length

    I would guess that you were subtracting 1 from that number because indexes are zero-based. In that case, zero opened times would result in an index of -1, which is obviously less than zero and would cause the error message you're seeing.

  7. #7

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: [RESOLVED] Parameter name: length

    not sure why it happens on just 2 pc's

    this is the code to count the times a jpg is opened....
    Code:
            If lLogOnDate.ToString.Contains("/01/") Then
                My.Settings.jpg_01_Started = My.Settings.jpg_01_Started + 1
            End If
            If lLogOnDate.ToString.Contains("/02/") Then
                My.Settings.jpg_02_Started = My.Settings.jpg_02_Started + 1
            End If
            If lLogOnDate.ToString.Contains("/03/") Then
                My.Settings.jpg_03_Started = My.Settings.jpg_03_Started + 1
            End If
            If lLogOnDate.ToString.Contains("/04/") Then
                My.Settings.jpg_04_Started = My.Settings.jpg_04_Started + 1
            End If
            If lLogOnDate.ToString.Contains("/05/") Then
                My.Settings.jpg_05_Started = My.Settings.jpg_05_Started + 1
            End If
            If lLogOnDate.ToString.Contains("/06/") Then
                My.Settings.jpg_06_Started = My.Settings.jpg_06_Started + 1
            End If
    and this is were the error occurred when adding to a list, writing to a txt file then uploading to a ftp

    Name:  code.jpg
Views: 118
Size:  47.5 KB

    a try catch fixed it...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Parameter name: length

    Don't use an exception handler to catch an exception that you easily prevent in the first place.

    You are calling LastIndexOf and then subtracting from the result. LastIndexOf will return -1 if the specified substring is not found and 0 if the last occurrence of the substring is at the beginning of the original string. In either case, the result would be negative and cause the exception you're seeing. The proper course of action would be to validate the input to make sure it conforms to your expectations. If you're expecting a string of a particular minimum length then test the Length of the String to make sure it is at least that much. If you're assuming that a String won't be empty then test that. Etc. Think about what your app requires in order to do what it is supposed to do and then, if those requirements aren't guaranteed to be met, write code to test whether they are or not and proceed according to the result.

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