Results 1 to 5 of 5

Thread: [RESOLVED] Stream Reader

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2005
    Posts
    90

    Resolved [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:
    1. 'Declare variables and constants
    2.         Dim strFileName As String
    3.         Dim strLine As String
    4.         Dim chrDelimiter As Char
    5.  
    6.         'Declare delimiter character
    7.         chrDelimiter = ","
    8.  
    9.         strFileName = CurDir() & "\tools.txt"
    10.         Dim srdReadFile As System.IO.StreamReader = New System.IO.StreamReader(strFileName)
    11.  
    12.         Do Until srdReadFile.peek = -1
    13.             ReDim Preserve strToolCode(intNumTools) = strFields(0) 'I realize this is illegal but how do I define this so that I can display it later?
    14.             ReDim Preserve strDescription(intNumTools)
    15.             ReDim Preserve decPrices(intNumTools)
    16.             strLine = srdReadFile.ReadLine
    17.             strFields = strLine.Split(chrDelimiter)
    18.             intNumTools = intNumTools + 1
    19.         Loop
    20.  
    21.         'Close file
    22.         FileClose()

    This is the search button code:
    VB Code:
    1. 'Declare variables
    2.         Dim intCounter As Integer
    3.         Dim intResults As Integer
    4.         Dim strFindToolCode As String
    5.         Dim blnFound As Boolean
    6.         Dim intPartIndex As Integer
    7.         Dim intNumRecords As Integer
    8.  
    9.         strFindToolCode = CStr(txtToolCode.Text)
    10.         blnFound = False
    11.  
    12.         For intCounter = 0 To intNumTools - 1
    13.             If UCase(strFindToolCode) = UCase(strToolCode(intCounter)) Then
    14.                 blnFound = True
    15.                 intPartIndex = intCounter
    16.                 Exit For
    17.             End If
    18.         Next
    19.  
    20.         If blnFound Then
    21.             txtDesc.Text = strToolCode(intPartIndex)
    22.         Else
    23.             MsgBox("Part not found", MsgBoxStyle.Exclamation, "Harry's Hardware")
    24.             Exit Sub
    25.         End If

    My 3 textboxes are txtToolCode, txtDesc, and txtPrice

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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:
    1. ReDim Preserve strToolCode(intNumTools) = strFields(0) 'errors because you havent split yet
    2.             ReDim Preserve strDescription(intNumTools)
    3.             ReDim Preserve decPrices(intNumTools)
    4.             strLine = srdReadFile.ReadLine
    5.             strFields = strLine.Split(chrDelimiter) '<----- do this line first.. move it to the top

  4. #4
    Member
    Join Date
    Aug 2005
    Posts
    49

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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