Results 1 to 6 of 6

Thread: "Index and length must refer to a location within the string. Parameter name: length"

  1. #1

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Question "Index and length must refer to a location within the string. Parameter name: length"

    i am creatign a program to get a duration from the user and the amound per second. it calculates the price per second for a phone call but i have an issue with 2 lines

    Code:
    varSeconds = CType(tbCallLength.Text.Substring(Sep2, 2), Double)
    and
    varPence = CType(tbCallPrice.Text.Substring(Sep4, 2), Double)
    with this error message "Index and length must refer to a location within the string. Parameter name: length"

    can u help please, here is the whole of my code

    Code:
    Option Strict On
    Public Class frmCallCalculator
    
        Dim Sep1, Sep2, Sep3, Sep4 As Integer
        Dim varMinutes, varSeconds As Double
        Dim varCal1, varCal2 As Double
        Dim varPounds, varPence As Double
    
        Private Sub btnCalculatePrice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculatePrice.Click
    
            Call Minutes()
            Call Seconds()
            Call Pounds()
            Call Pence()
    
            varCal1 = CType(varSeconds + (varMinutes * 60), Double)
            varCal2 = varPence + (varPounds * 100)
    
            tbAnswer.Text = CType(varCal1 * varCal2, String)
        End Sub
    
        Private Sub Minutes()
            Sep1 = InStr(1, tbCallLength.Text, ":")
            Sep1 = Sep1 - 1
            varMinutes = CType(tbCallLength.Text.Substring(0, Sep1), Double)
        End Sub
    
        Private Sub Seconds()
            Sep2 = InStr(1, tbCallLength.Text, ":")
            Sep2 = Sep2 + 1
            varSeconds = CType(tbCallLength.Text.Substring(Sep2, 2), Double)
        End Sub
    
        Private Sub Pounds()
            Sep3 = InStr(2, tbCallPrice.Text, ".")
            Sep3 = Sep3 - 1
            varPounds = CType(tbCallPrice.Text.Substring(2, Sep3), Double)
        End Sub
    
        Private Sub Pence()
            Sep4 = InStr(2, tbCallPrice.Text, ".")
            Sep4 = Sep4 + 1
            varPence = CType(tbCallPrice.Text.Substring(Sep4, 2), Double)
        End Sub
    End Class

  2. #2
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: "Index and length must refer to a location within the string. Parameter name: len

    hey stuart,any reason you are using a string to represent the time duration? vb.net has in built functions to deal with times, problem with what you are doing is you need to parse the string, what if the user input a time as "Hello world"?

    you could for instance use TimeSpan (http://msdn.microsoft.com/en-us/library/system.timespan_properties%28VS.80%29.aspx) then enter a time in one of several formats
    Code:
    Dim days as Integer = 0
    Dim hours As Integer = 0
    Dim minutes As Integer = 400
    Dim seconds As Integer = 30
    Dim Duration as New TimeSpan(days, hours, minutes, seconds)
    Console.WriteLine(Duration.TotalSeconds)
    This will output to the console the number of seconds in 400m30s, you can then multiply this by the cost per second and divide by 100 to give the value in pounds and pence.
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  3. #3

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: "Index and length must refer to a location within the string. Parameter name: len

    its one of the tasks that i have to get the user to input then time and price themselves using the method that i am using now but i just dont get the reason for the error to appear

  4. #4
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: "Index and length must refer to a location within the string. Parameter name: len

    ok gimme a mo will try the code
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

  5. #5

    Thread Starter
    Registered User
    Join Date
    Sep 2009
    Location
    England
    Posts
    112

    Re: "Index and length must refer to a location within the string. Parameter name: len

    ive only got three text boxes and a button

  6. #6
    Fanatic Member Megalith's Avatar
    Join Date
    Oct 2006
    Location
    Secret location in the UK
    Posts
    879

    Re: "Index and length must refer to a location within the string. Parameter name: len

    ok found a few things. first in your minutes you find the ':' and subtract 1 (should be 3) then you dont use the variable in the correct context (you use it for the length which happens to also be 2) in the seconds routine you find the ':' which is 3 and then you add one, then you look for 2 characters. let me put this better.

    the 2 string functions you use have different index bases. instr is 1 based and Substring is zero based. say you have this as the string
    12:34
    instr provides the result 3 for the ':'
    12:34
    12345
    so to get the first 2 digits you need substring(0,2) and for the last 2 you need (3,2)

    12:34
    01234

    hope this helps, you will need to adjust all your subs. just to help further the seconds and pence subs do not need the line that adds 1
    If debugging is the process of removing bugs, then programming must be the process of putting them in.

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