473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Substring generates error

I've found a problem in my program. I'm reading a file (fixed length) and
I'm using a structure to return the values...all ok. But the problem arises
when the length of the line I'm reading isn't as long as it's supposed to
be. Then my substring() function throws an error. Below is my code.

Public Structure strucRecord
Public strLine As String
Public ReadOnly Property AssignCity() As String
Get
AssignCity = strLine.Substring(335, 15).Trim
End Get
End Property
End Structure

Dim rec As New strucRecord
Dim sr As StreamReader = New StreamReader(Me.txtFile.Text)
rec.strLine = sr.ReadLine

* While looping through this file, there are a few lines in file that have a
length of 300? I haven't figured that one out. So, when I go to access
"rec.AssginCity" it throws an error. Anyhow, I'm wondering if I need to put
an "IF" statement in the "GET"...checking on length...or is there a better
way??

TIA
-bruce

Nov 21 '05 #1
3 992
Its always better to check for the error before hand.

HTH
rawCoder

"Bruce D" <br*************@hotmail.com> wrote in message
news:11*************@corp.supernews.com...
I've found a problem in my program. I'm reading a file (fixed length) and
I'm using a structure to return the values...all ok. But the problem arises when the length of the line I'm reading isn't as long as it's supposed to
be. Then my substring() function throws an error. Below is my code.

Public Structure strucRecord
Public strLine As String
Public ReadOnly Property AssignCity() As String
Get
AssignCity = strLine.Substring(335, 15).Trim
End Get
End Property
End Structure

Dim rec As New strucRecord
Dim sr As StreamReader = New StreamReader(Me.txtFile.Text)
rec.strLine = sr.ReadLine

* While looping through this file, there are a few lines in file that have a length of 300? I haven't figured that one out. So, when I go to access
"rec.AssginCity" it throws an error. Anyhow, I'm wondering if I need to put an "IF" statement in the "GET"...checking on length...or is there a better
way??

TIA
-bruce

Nov 21 '05 #2
BruceD,

Use an "If" to check if the instruction can be done.
Its always better to check for the error before hand.

:-)

Cor
Nov 21 '05 #3
Another alternative is to pad the string when you read it in so that it
always has the correct length. Of course your property might return an
empty string in that case.

rec.strLine = sr.ReadLine.PadRight(maxlength," "c)

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Don Freeman | last post by:
Seems like whatever value I use for the first int field (starting position) the substring procedure negates it and triggers a String index out of range error. I've tried all sorts of work...
2
by: Michele Fondry via .NET 247 | last post by:
hello. I have a webform form app in vb.Net that uses a SQL query. I am trying to use the substring function, but get the following error: Description: An unhandled exception occurred during the...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
2
by: David Filion | last post by:
Hi, I have a question about substring(), when I run the following query: prepaid=# select substring('15148300', 0, 5); substring ----------- 1514 (1 row)
15
by: Duncan Allen | last post by:
Hi, Using C# I'm trying to use the substring method of a string variable but it just generates an "error: 'variable.Substring' does not exist " exception - how do I fix this ? code example: ...
1
by: rh1200la | last post by:
Hey all. I'm doing a very basic screen scrape of a page that displays a banner ad. It is basically an <Atag and an <IMGtag. I need to grab the url of each and store them. Right now I can...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
1
by: =?KOI8-R?Q?=F3=D4=C1=CE=C9=D3=CC=C1=D7_=F1=C7=CC=C | last post by:
Hello all! I have a string named text. I need to extract a substring from it starting by variable 's' and ending by 'e'. text generates the following error: TypeError: slice indices must be...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.