3 Replies - 1280 Views - Last Post: 07 April 2013 - 09:10 AM Rate Topic: -----

#1 DJ2334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-February 13

startIndex cannot be larger than length of string

Posted 06 April 2013 - 10:26 PM

I'm trying to create a program that when I select an airport from the combo box (like Sacramento Airport) and then select a number from the list box (like 3000), it will go by the string "SAC 1913 1810+15 2118+11 2127+05 2341-08 2355-20 226736 228144 239054 " and display data on the form when I run my program. I think I almost have it, but this code is messing me up:

strRet = strStaWea.Substring(getPos(strAlt), 7)

It keeps displaying an error message saying: "startIndex cannot be larger than length of string"

Can someone tell me how I can fix this? Oh and btw, I can't change my code up too much. It's supposed to be two separate classes and I'm supposed to have certain exceptions applied to my program (it's part of a school assignment). So if somebody can just tell me how I can get around this error, I'll be good to go!

Public Class A18608
    Public m_strStationWeather As String
    ' Declare a class-level array
    Dim m_aryWeather() As String = _
   {"SAC 1913 1810+15 2118+11 2127+05 2341-08 2355-20 226736 228144 239054 ", _
    "SAN 9900 2610+22 1911+15 1811+09 1514-08 1627-17 152333 163142 184952 ", _
    "SBA 9900 9900+20 2314+15 2120+08 2129-08 2169-18 208333 700142 730253 ", _
    "SFO 2607 2505+14 2214+11 2225+05 2339-08 2350-20 226536 227644 238454 ", _
    "SIY      9900+13 1709+06 1716+00 2229-13 2252-25 228638 239247 239353 "}

    Private Sub A18608_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' Displays the current date on form
        lblDate.Text = DateString
        With cboLocations.Items
            .Add("Sacramento Executive Airport")
            .Add("San Diego Lindbergh Field")
            .Add("Santa Barbara Municipal Airport")
            .Add("San Francisco International Airport")
            .Add("Siskiyou County Airport")
        End With
        With lstAltitudes.Items
            .Add("03000")
            .Add("06000")
            .Add("09000")
            .Add("12000")
            .Add("18000")
            .Add("24000")
            .Add("30000")
            .Add("34000")
            .Add("39000")
        End With
    End Sub
    Private Sub lstAltitudes_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstAltitudes.SelectedIndexChanged, cboLocations.SelectedIndexChanged
        'Define variables
        Dim fb As New NWSFB07(m_strStationWeather)
        If lstAltitudes.SelectedIndex > -1 And cboLocations.SelectedIndex > -1 Then
            m_strStationWeather = m_aryWeather(cboLocations.SelectedIndex)

            Dim strAlt As String = lstAltitudes.SelectedItem.ToString.Substring(0, 2)

            lblDirection.Text = fb.getWindDir(strAlt)
            lblSpeed.Text = fb.getWindSpeed(strAlt)
            lblTemperature.Text = fb.getWindTemp(strAlt)

        End If
    End Sub
End Class



Imports System.Xml.Xsl
Public Class NWSFB07
    Implements NWSTemplate
    Private strStaWea As String 'Station Weather

    Public Sub New(ByVal strStationWeather As String)
        strStaWea = strStationWeather & " "
    End Sub
    ''' <summary>
    ''' This function will accept a two character string parameter
    ''' representing the altitude in thousands of feet and
    ''' returns the seven character altitude weather.
    ''' </summary>
    ''' <param name="strAlt"></param>
    ''' <returns>Altitude weather as a seven character string</returns>
    Public Function getAltitudeWeather(strAlt As String) As String Implements NWSTemplate.getAltitudeWeather
        ' Declare Variables
        Dim strRet As String = ""

        ' Determine where to extract characters
        strRet = strStaWea.Substring(getPos(strAlt), 7)

        Return strRet

    End Function
    ''' <summary>
    '''  This function will accept a two character string parameter 
    ''' representing the altitude in thousands of feet and 
    ''' returns an integer representing the starting
    ''' position of the Altitude Weather for that altitude.
    ''' </summary>
    ''' <param name="strAlt"></param>
    ''' <returns>integer representing the starting position
    ''' of the Altitude Weather for the called 
    ''' altitude</returns>
    ''' <remarks>
    ''' For example, 
    ''' intPos = myNWS.getPos("18") would mean that the 
    ''' user is requesting the starting position of the 
    ''' Altitude Weather at 18,000 feet.  In this case,
    ''' the function should return an xx.
    ''' </remarks>
    Public Function getPos(strAlt As String) As Integer Implements NWSTemplate.getPos
        ' Declare Variable
        Dim intPos As Integer = -1

        ' Determine what character position to start extracting the ALTITUDE weather from the STATION weather
        Select Case strAlt
            Case "03"
                intPos = 4
            Case "06"
                intPos = 9
            Case "09"
                intPos = 17
            Case "12"
                intPos = 25
            Case "18"
                intPos = 33
            Case "24"
                intPos = 41
            Case "30"
                intPos = 49
            Case "34"
                intPos = 56
            Case "39"
                intPos = 63
        End Select

        Return intPos

    End Function
    ''' <summary>
    ''' This function extracts the first three characters from the 
    ''' class level variable STATION WEATHER and returns it as a 
    ''' string.
    ''' </summary>
    ''' <returns>Three character station ID</returns>
    ''' <remarks>This function simply uses .substring to extract
    ''' the first three characters of the class level STATION WEATHER 
    ''' string.</remarks>
    Public Function getStationID() As String Implements NWSTemplate.getStationID
        Return strStaWea.Substring(0, 3)
    End Function
    ''' <summary>
    ''' This function will accept a two character string parameter
    ''' representing the altitude in thousands of feet and
    ''' returns the wind direction.
    ''' </summary>
    ''' <param name="strAlt"></param>
    ''' <returns>Wind direction as a three character string</returns>
    ''' <remarks>The wind direction is the first two bytes 
    ''' of the Altitude Weather after a "0" has been appended
    ''' to the string.  Thus, "24" would be converted into "260" 
    ''' which would be almost due west.</remarks>
    Public Function getWindDir(strAlt As String) As String Implements NWSTemplate.getWindDir
        ' Declare Variables
        Dim strAltWea As String = ""
        Dim strRet As String = ""
        Dim intDir As Integer

        ' 12000 feet contains normal data

        ' Get the Altitude Weather
        strAltWea = getAltitudeWeather(strAlt)

        ' =========================================
        ' = OUR EXCEPTIONS
        ' =========================================

        If strAltWea.Substring(0, 2) = "  " Then
            ' (6a) are first two characters blanks?
            strRet = "N/A"
        Else
            ' first two characters are NOT blanks

            If strAltWea.Substring(0, 4) = "9900" Then
                ' (6b) are first four characters "9900"?
                strRet = "Calm"
            Else
                ' first four characters are NOT "9900" 

                ' create an integer to use in a numeric comparison
                intDir = CInt(strAltWea.Substring(0, 2))

                If intDir > 36 Then
                    ' (6c) are first two characters > 36?

                    ' (6c) subtract 50 from the direction
                    intDir = intDir - 50

                    ' convert it back into a string
                    strRet = intDir.ToString()

                    ' suffix it with a "0"
                    strRet = strRet & "0"
                Else
                    ' (normal)
                End If
            End If
        End If

        ' Return the string to the calling program
        Return strRet

    End Function
    ''' <summary>
    ''' This function will accept a two character string parameter
    ''' representing the altitude in thousands of feet and 
    ''' returns the wind speed.
    ''' </summary>
    ''' <param name="strAlt"></param>
    ''' <returns>The wind speed as a two character string</returns>
    ''' <remarks>The wind speed is the second two positions
    ''' of the Altitude weather and represents the wind speed
    ''' in knots.</remarks>
    Public Function getWindSpeed(strAlt As String) As String Implements NWSTemplate.getWindSpeed
        ' Declare Variables
        Dim strAltWea As String = ""
        Dim strRet As String = ""
        Dim intSpeed As Integer

        ' 12000 feet contains normal data

        ' Get the Altitude Weather
        strAltWea = getAltitudeWeather(strAlt)

        ' =========================================
        ' = OUR EXCEPTIONS
        ' =========================================

        If strAltWea.Substring(2, 2) = "  " Then
            ' (7a) are third and fourth characters blank?
            strRet = "N/A"
        Else
            ' first two characters are NOT blanks

            If strAltWea.Substring(0, 4) = "9900" Then
                ' (7b) are first four characters "9900"?
                strRet = "Calm"
            Else
                ' first four characters are NOT "9900" 

                ' create an integer to use in a numeric comparison
                intSpeed = CInt(strAltWea.Substring(2, 2))
                If intSpeed > 36 Then
                    ' (7c) are first two characters > 36?

                    ' (7c) add 100 to the speed
                    intSpeed = intSpeed + 100

                    ' convert it back into a string
                    strRet = intSpeed.ToString()
                Else
                    ' (normal)
                End If
            End If
        End If

        ' Return the string to the calling program
        Return strRet

    End Function
    ''' <summary>
    ''' This function will accept a two character string parameter
    ''' representing the altitude in thousands of feet and
    ''' returns the wind temperature.
    ''' </summary>
    ''' <param name="strAlt"></param>
    ''' <returns>a three character string</returns>
    ''' <remarks>The wind temperature is the last two or
    ''' three characters of the Altitude weather.  For all
    ''' altitudes over 24,000 feet, assume that the value
    ''' is a negative number. </remarks>
    Public Function getWindTemp(ByVal strAlt As String) As String Implements NWSTemplate.getWindTemp
        ' declare variables
        Dim intTemp As Integer
        Dim strAltWea As String
        Dim strTemp As String
        Dim intAlt As Integer
        ' set intTemp to intPos
        intTemp = getPos(strAlt)
        ' assign strAltWea to the 7 characters based off of intTemp
        strAltWea = strStaWea.Substring(intTemp, 7)
        ' extract the 5th 6th and 7th characters from strAltWea and store them in strTemp
        strTemp = strAltWea.Substring(4, 3)
        ' is the 5th character a blank?
        If strAltWea.Substring(4, 1) = " " Then
            ' display "N/A"
            strTemp = "N/A"
            ' if not
        Else
            ' Assign the first two characters of the altitude the user selected as an integer
            intAlt = CInt(strAlt)
            ' Is intAlt greater than 24
            If intAlt > 24 Then
                ' Put a negative sign in front of it
                strTemp = "-" & strTemp
            Else
                ' Nothing is changed
            End If
        End If
        ' Return strTemp to display the temperature at altitude
        Return strTemp
    End Function
End Class


This post has been edited by macosxnerd101: 06 April 2013 - 10:29 PM
Reason for edit:: Please use a descriptive title


Is This A Good Question/Topic? 0
  • +

Replies To: startIndex cannot be larger than length of string

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,772
  • Joined: 26-March 09

Re: startIndex cannot be larger than length of string

Posted 07 April 2013 - 01:00 AM

First off, this is VB.Net code, so you're actually in the wrong forum.

That being said, having a quick glance at your code, it looks like you instantiate your NWSFB07 class with an empty string, then try to use that string.

' at this point I don't see m_strStationWeather as being set
Dim fb As New NWSFB07(m_strStationWeather)

        If lstAltitudes.SelectedIndex > -1 And cboLocations.SelectedIndex > -1 Then

            ' You set your variable here, but I don't see it being updated in the NWSFB07 class
            m_strStationWeather = m_aryWeather(cboLocations.SelectedIndex)



If you add a breakpoint to the line with your error and look at the value of variable strStaWea, you may find that it is empty.

Hopefully that gives you some pointers.
Was This Post Helpful? 0
  • +
  • -

#3 DJ2334  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-February 13

Re: startIndex cannot be larger than length of string

Posted 07 April 2013 - 01:26 AM

Yea, I just posted it in the other forum about an hour ago after I realized this was the wrong place. I've tried using just the array instead of the strStationWeather string, but it keeps messing up when it comes to substrings. I'm really lost on what to do and my professor isn't helping at all. I've read the chapter too and everything and just can't seem to figure out how to get this array to work with the combo box and list box. Every time I look something up, people show easier ways to do it and most recommend getting rid of the large array string and breaking it down, but my professor wants it done in this particular way. I'm really lost on what to do...
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: startIndex cannot be larger than length of string

Posted 07 April 2013 - 09:10 AM

You just want to look at the string when you get the error. You should go into debug mode when you get it, and when you're there, go to the immediate window and look at the value of getPos(strAlt). It will be less than 8 characters long. Startindex of 7 means that you want to start with the 8th character in the string. So if your string is smaller than 8 characters, you'll get that error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1