none
Argument 'Length' must be greater or equal to zero. RRS feed

  • Question

  • This makes no since to me I have a textbox that the user will put in information that I need to cut off so I wrote this code

    txtMatID.Text = Trim(Mid(txtMatID.Text, 1, (InStr(txtMatID.Text,

     

    "-") - 1)))

    The user will imput a number value then a - then some words after that. I just want the number value.
    But when I try to debug the app it breaks on this line of code with this error.
    Argument 'Length' must be greater or equal to zero.
    Would someone PLEASE explain why this line of code is incorrect.
    Thanks

    Friday, February 12, 2010 4:53 AM

Answers

  • What Acamar is saying is that in your statement txtMatID.Text = Trim(Mid(txtMatID.Text, 1, (InStr(txtMatID.Text, "-") - 1))) when you give the length to the Mid() method the value you're passing must be less than zero. What's most likely happening is that InStr(txtMatID.Text) is returning either 0 or -1 which means you trying to pass -1 or -2 to the Mid() method. The reason why is that if when you call the statement the TextBox either does not contain a ‘-‘ or it finds it as the first character.


    Bill Gates look out!
    • Marked as answer by mutlyp Friday, February 12, 2010 5:52 AM
    Friday, February 12, 2010 5:31 AM
  • You are calculating the Length argument as

    InStr(txtMatID.Text, "-") - 1

    If the "-" is the first character in the string, that expression will return -1.  A value less than zero is invalid as the Length argument of the Mid function.

    Get the position of the '-' into a variable, check that it's not Zero, and only if it's greater than zero go ahead and subtract one and get the character you want using the Mid function.

     

     

    • Marked as answer by mutlyp Friday, February 12, 2010 5:52 AM
    Friday, February 12, 2010 5:32 AM
  • Maybe use the same code without loading the string in the textbox ???

    So take that line out (I was just showing an example of what you said the format of the string would be).

    And you'll want to put in a check that the textbox text contains a  -

    • Marked as answer by mutlyp Friday, February 12, 2010 5:51 AM
    Friday, February 12, 2010 5:34 AM

All replies

  • Any reason you posted your question twice?
    Bill Gates look out!
    Friday, February 12, 2010 5:02 AM
  • I didn't?????
    Friday, February 12, 2010 5:09 AM
  • Don't know what happened I only clicked the submit button once.

    Anyways I descovered something else I only get this error in the private sub of a ComboBox SelectedIndexChange.
    Still this does not makes since to me.

    Would someone please tell me why this is???
    Thanks
    Friday, February 12, 2010 5:15 AM
  • It means as it says - the 'Length' argument must be zero or greater.  You are creating a 'Length' argument of -1. Break your expression down into searate statements so that you can check that the character exists and that there is a preceeding character to get, before doing the Mid().
    • Edited by Acamar Friday, February 12, 2010 5:25 AM
    Friday, February 12, 2010 5:16 AM
  • Can you explain what the connection between the ComboBox and TextBox is???

    Why are you doing this in a ComboBox SelectedIndexChanged?

    Anyway, try this using Substring and IndexOf:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
            TextBox1.Text = "12345-some other text"
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.IndexOf("-"c))
        End Sub
    Friday, February 12, 2010 5:22 AM
  • Thank you for your answer and forgive my ignorance but what do you mean when you say " You have specified a 'Length' argument of -1"
    Also how do I get the index?
    Thanks again
    Friday, February 12, 2010 5:22 AM
  • Thank you for the resonse
    That did work but I still don't understand why. The TextBox1 can not have a value in it when the page loads. What can I do??
    Thanks again
    Friday, February 12, 2010 5:26 AM
  • What Acamar is saying is that in your statement txtMatID.Text = Trim(Mid(txtMatID.Text, 1, (InStr(txtMatID.Text, "-") - 1))) when you give the length to the Mid() method the value you're passing must be less than zero. What's most likely happening is that InStr(txtMatID.Text) is returning either 0 or -1 which means you trying to pass -1 or -2 to the Mid() method. The reason why is that if when you call the statement the TextBox either does not contain a ‘-‘ or it finds it as the first character.


    Bill Gates look out!
    • Marked as answer by mutlyp Friday, February 12, 2010 5:52 AM
    Friday, February 12, 2010 5:31 AM
  • You are calculating the Length argument as

    InStr(txtMatID.Text, "-") - 1

    If the "-" is the first character in the string, that expression will return -1.  A value less than zero is invalid as the Length argument of the Mid function.

    Get the position of the '-' into a variable, check that it's not Zero, and only if it's greater than zero go ahead and subtract one and get the character you want using the Mid function.

     

     

    • Marked as answer by mutlyp Friday, February 12, 2010 5:52 AM
    Friday, February 12, 2010 5:32 AM
  • Maybe use the same code without loading the string in the textbox ???

    So take that line out (I was just showing an example of what you said the format of the string would be).

    And you'll want to put in a check that the textbox text contains a  -

    • Marked as answer by mutlyp Friday, February 12, 2010 5:51 AM
    Friday, February 12, 2010 5:34 AM
  • Thanks guys now it makes since I was looking at it in a differnet light and just could not see it. But you turned the light on and I now see it.
    Thanks Again
    Friday, February 12, 2010 5:51 AM