Results 1 to 3 of 3

Thread: Loading an array from file into variable??

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2003
    Posts
    830

    Loading an array from file into variable??

    I'm a newbie.

    I can load a text file into a listbox no problem, but how do I load a list of names into a variable? I don't think I know enought to even ask the question right.

    Any examples of loading arrays from a file?

  2. #2
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    Well, well. A fellow Newbie. Hello!

    I have a project I did for a college VB.Net class I'm taking. It reads from, saves to, and deletes lines from a text file. It uses an array to store all the data, and all that good stuff.

    However, it's too big to attach(336kb). Do you have somewhere I could send it?

    The only thing is, you'll have to Unzip it into your C:\ drive for the all file stuff to work right.
    Take my love
    Take my land
    Take me where I cannot stand
    I don't care, I'm still free
    You can't take the sky from me...

  3. #3
    Addicted Member The Phoenix's Avatar
    Join Date
    Aug 2003
    Location
    With my wife
    Posts
    142
    Ah nevermind, I'll just post the relevant code.

    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. Imports System.Convert
    4. '''''
    5. 'Create the structure for the record
    6. Public Structure sEventRec
    7.     Public EventName As String
    8.     Public EventDate As Date
    9.     Public EventPrice As Double
    10.     Public EventDesc As String
    11. End Structure
    12. Public Class frmDisplay
    13.     Inherits System.Windows.Forms.Form
    14.     '''''
    15.     'Not really sure about this one...
    16.     Private EventRec() As sEventRec
    17.  
    18.  
    19.     Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
    20.  
    21.         '''''''''''''''''''''''''''''''''''''''''''''''''''''
    22.         '' First check to make sure the Combo Box is empty
    23.         '''''''''''''''''''''''''''''''''''''''''''''''''''''
    24.         If cboEvents.Items.Count = Nothing Then
    25.             '''''
    26.             'Create the streamreader, delimiter, lines, and fields
    27.             Dim psrdDisplay As System.IO.StreamReader
    28.             Dim pchrDelimiter() As Char = {ToChar(",")}
    29.             Dim pstrline As String
    30.             Dim pstrFields() As String
    31.             Dim pintcount As Integer
    32.             '''''
    33.             'Use a preset fielname and read the line
    34.             psrdDisplay = New System.IO.StreamReader("C:\Project\File.txt")
    35.             pstrline = psrdDisplay.ReadLine()
    36.             '''''
    37.             'Stope reading when we hit a blank line
    38.             Do Until pstrline = Nothing
    39.                 '''''
    40.                 'Otherwise spilt the line into fields
    41.                 ReDim Preserve EventRec(pintcount)
    42.                 pstrFields = pstrline.Split(pchrDelimiter)
    43.                 EventRec(pintcount).EventName = (pstrFields(0).ToString)
    44.                 cboEvents.Items.Add(EventRec(pintcount).EventName)
    45.                 EventRec(pintcount).EventDate = ToDateTime(pstrFields(1))
    46.                 EventRec(pintcount).EventPrice = ToDouble(pstrFields(2))
    47.                 EventRec(pintcount).EventDesc = (pstrFields(3).ToString)
    48.                 '''''
    49.                 'Increase the counter and read the next line
    50.                 pintcount += 1
    51.                 pstrline = psrdDisplay.ReadLine()
    52.             Loop
    53.             '''''
    54.             'Display a message bos when file is done opening and close
    55.             'the streamreader
    56.             MsgBox("Done Opening File", MsgBoxStyle.OKOnly, "Event Tracker")
    57.             psrdDisplay.Close()
    58.         Else
    59.             '''''
    60.             'If the Combo Box is populated, don't re-open the file
    61.             MsgBox("The File is already open", MsgBoxStyle.OKOnly, "Event Tracker Help")
    62.         End If
    63.     End Sub
    64.  
    65.     Private Sub cboEvents_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboEvents.SelectedIndexChanged
    66.  
    67.         ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    68.         '' When a selection is made, find that record and display
    69.         '' all the info about it
    70.         ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    71.         Dim pintcount As Integer
    72.         Dim pstrRec As sEventRec
    73.         '''''
    74.         'First clear all labels
    75.         lblName.Text = ""
    76.         lblDate.Text = ""
    77.         lblDescription.Text = ""
    78.         lblPrice.Text = ""
    79.         For pintcount = 0 To EventRec.GetUpperBound(0)
    80.             '''''
    81.             'Look for a match in the array
    82.             If cboEvents.Text = EventRec(pintcount).EventName Then
    83.                 '''''
    84.                 'When match is found display cooresponding info
    85.                 pstrRec = EventRec(pintcount)
    86.                 lblName.Text = pstrRec.EventName
    87.                 lblDate.Text = (pstrRec.EventDate).ToShortDateString
    88.                 lblPrice.Text = FormatCurrency(pstrRec.EventPrice)
    89.                 lblDescription.Text = pstrRec.EventDesc
    90.             End If
    91.         Next
    92.     End Sub

    Hope this helps!
    Take my love
    Take my land
    Take me where I cannot stand
    I don't care, I'm still free
    You can't take the sky from me...

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