|
-
Nov 23rd, 2005, 06:40 PM
#1
Thread Starter
Lively Member
[RESOLVED] Stream Reader
What I am doing is using a stream reader to read data from a file each line of the file contains 3 parts:
ToolCode,Description,Price
Obviously I am using the "," as the delimiter character
I would like to split up the three parts into three separate arrays. I have a textbox set up where the user inputs a toolcode (must match exactly) and i would like it to display the description and the price in separate textboxes. I know where my problem resides I just can't find anything that tells me how to define the variable properly.
This is the code for the Load Event:
VB Code:
'Declare variables and constants
Dim strFileName As String
Dim strLine As String
Dim chrDelimiter As Char
'Declare delimiter character
chrDelimiter = ","
strFileName = CurDir() & "\tools.txt"
Dim srdReadFile As System.IO.StreamReader = New System.IO.StreamReader(strFileName)
Do Until srdReadFile.peek = -1
ReDim Preserve strToolCode(intNumTools) = strFields(0) 'I realize this is illegal but how do I define this so that I can display it later?
ReDim Preserve strDescription(intNumTools)
ReDim Preserve decPrices(intNumTools)
strLine = srdReadFile.ReadLine
strFields = strLine.Split(chrDelimiter)
intNumTools = intNumTools + 1
Loop
'Close file
FileClose()
This is the search button code:
VB Code:
'Declare variables
Dim intCounter As Integer
Dim intResults As Integer
Dim strFindToolCode As String
Dim blnFound As Boolean
Dim intPartIndex As Integer
Dim intNumRecords As Integer
strFindToolCode = CStr(txtToolCode.Text)
blnFound = False
For intCounter = 0 To intNumTools - 1
If UCase(strFindToolCode) = UCase(strToolCode(intCounter)) Then
blnFound = True
intPartIndex = intCounter
Exit For
End If
Next
If blnFound Then
txtDesc.Text = strToolCode(intPartIndex)
Else
MsgBox("Part not found", MsgBoxStyle.Exclamation, "Harry's Hardware")
Exit Sub
End If
My 3 textboxes are txtToolCode, txtDesc, and txtPrice
-
Nov 23rd, 2005, 06:53 PM
#2
Re: Stream Reader
I'd ditch the arrays frankly. I'd be inclined to use ADO.NET to read the file directly into a DataTable. Even using a StreamReader, you can still set up a DataTable manually and populate it. The next best alternative would be to use a HashTable keyed on the tool code. You could use two HashTables, one for description and one for price, or you could use a single HashTable where the value was an array, a collection, or a structure of your own definition.
-
Nov 23rd, 2005, 10:27 PM
#3
Re: Stream Reader
Well you are splitting your string after you try to assign it... just do it beforehand, and you can use arraylist, which is easier since you are working with strings, and with arraylist, you can just use the .Add method to add an item to it, or use JM's methods he had mentioned..
VB Code:
ReDim Preserve strToolCode(intNumTools) = strFields(0) 'errors because you havent split yet
ReDim Preserve strDescription(intNumTools)
ReDim Preserve decPrices(intNumTools)
strLine = srdReadFile.ReadLine
strFields = strLine.Split(chrDelimiter) '<----- do this line first.. move it to the top
-
Nov 23rd, 2005, 11:12 PM
#4
Member
Re: Stream Reader
You can use RegularExpression in system.text
<vbcode>
imports system.text.regularExpression
dim temp as new regex(",") ' this line to define the delimiter character
dim tempArray as string() = temp.Split(<content>)
'Then you ca get the content from the array you have defined
tempArray.GetValue(<index>)
</vbcode>
Try this code.
Take care and good luck
-
Nov 23rd, 2005, 11:30 PM
#5
Re: Stream Reader
There's no point using Regex.Split over String.Split unless you want to use a multi-character string as the delimiter instead of a single character. String.Split will be more efficient so unless you need the additional functionality of Regex.Split, i.e. split on a string or a pattern, then it is less desirable.
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
|