Results 1 to 7 of 7

Thread: substring confusion

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    14

    substring confusion

    I'm trying to select all events which happen in a particular month using the following code (VB2008Express):
    Public Fd(100,10) as String
    for i=1 to 75
    dim str as String=Fd(i,8).substring(1,2) (Fd(i,8) is a string date: 12/12/08)
    using debug.print, this returns "2/" - not good. When I run this, I get a "ArgumentOutOf RangeException unhandled - Start index cannot be larger than length of string Parameter:start index"

    When I run .Substring(0,2) I get "12", which is good, but (0,2) is not per the Help instructions for Substring. This also gives the "ArgumentOutOfRangeException - Index and length must refer to a location within the string Parameter:length"

    I don't understand either exception. The start index does not seem to be larger than the string, and even if the index doesn't refer to a location within the string I get the correct return. I don't know what I need to fix and I certainly don't know what to use as a Catch phrase to correct whatever is wrong. Please someone set me straight , but don't make me look too inept.

    joewillie

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: substring confusion

    Code:
            Dim someString As String = "012345"
            Debug.WriteLine(someString.Substring(0, 2)) 'strings are zero based
            Debug.WriteLine(someString.Substring(1, 2)) 'strings are zero based
            Debug.WriteLine(someString.Substring(0, someString.Length - 1)) 'therefore number of chars. is length -1 
            'debug output
            '01
            '12
            '1234
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    14

    Re: substring confusion

    Quote Originally Posted by dbasnett
    Code:
            Dim someString As String = "012345"
            Debug.WriteLine(someString.Substring(0, 2)) 'strings are zero based
            Debug.WriteLine(someString.Substring(1, 2)) 'strings are zero based
            Debug.WriteLine(someString.Substring(0, someString.Length - 1)) 'therefore number of chars. is length -1 
            'debug output
            '01
            '12
            '1234
    dbasnett - thanks for your quick response to a beginner. I understand your method, and can get the result I need using the (0,2) parameter. The big problem is the "ArgumentOutOfRangeException" which doesn't allow the program to execute. No matter which of the parameters I use, I can't run the program. Is there some Try-Catch statement I can use to run the program - some Catch statement which tells VB to ignore the perceived exception? Thanks again - joewillie

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: substring confusion

    Code:
            'this array Fd(100, 10) is 101 rows by 11 columns
            Dim Fd(100, 10) As String
            Dim aStr As String
            Debug.WriteLine(Fd.GetUpperBound(0))
            Debug.WriteLine(Fd.GetUpperBound(1))
            For i = 0 To Fd.GetUpperBound(0)
                For ii = 0 To Fd.GetUpperBound(1)
                    Fd(i, ii) = "12/12/08"
                Next
            Next
            For i = 0 To Fd.GetUpperBound(0)
                'substring start + number of characters can't exceed .Length
                Debug.WriteLine(0 + Fd(i, 8).Length) 'show length of string
                aStr = Fd(i, 8).Substring(0, 8)
            Next
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    Addicted Member
    Join Date
    Nov 2006
    Location
    Minnesota
    Posts
    235

    Re: substring confusion

    What it sounds like is that some of your data in FD maybe an empty string. You have to make sure that you have a value that you can get a substring from first.
    If someone has helped you, please make sure to rate them.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    14

    Re: substring confusion

    Quote Originally Posted by demausdauth
    What it sounds like is that some of your data in FD maybe an empty string. You have to make sure that you have a value that you can get a substring from first.
    Thank you, thank you, thank you - no more exceptions when I check the array for blanks, of which there are many. I would have spent a lot of time before I realized that some of the strings were blanks. Thanks again - it's good to know VB was right and good to know there are folks like you out there willing to bail out the anateurs.
    joewillie

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: substring confusion

    see my signature for info about resolved threads
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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