|
-
Dec 17th, 2008, 04:13 PM
#1
Thread Starter
New Member
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
-
Dec 17th, 2008, 04:25 PM
#2
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
-
Dec 18th, 2008, 11:13 AM
#3
Thread Starter
New Member
Re: substring confusion
 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
-
Dec 18th, 2008, 01:10 PM
#4
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
-
Dec 18th, 2008, 02:58 PM
#5
Addicted Member
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. 
-
Dec 18th, 2008, 04:00 PM
#6
Thread Starter
New Member
Re: substring confusion
 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
-
Dec 18th, 2008, 04:19 PM
#7
Re: substring confusion
see my signature for info about resolved threads
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|