Stream data in and calculate

Hello everyone. I'll try to get to the point as quick as possible. I have a form that has a textbox, an OpenFileDialog control and two command buttons. One command button, the "Next" button, takes you to the next form but only after you use the "Browse" to find the text file. The browse button, after finding the text file will use a StreamReader to get the data from the file. The text file contains text and numbers but the numbers will have to be converted before making the calculation (ROI). I was thinking that streaming the data into the "List" would be the best way to go and then I could access the data according to it's index number. Does this make sense so far If so, am I correct in my thinking With the assumption that I am correct how would I use StreamReader to read all data in the text file until it reaches the end of the text file, put it into an array and make it so that I can access the data in the List (collection ).

The help I am asking for will hopefully have some examples but not completely give away the answer because I need to learn as much as possible as fast as possible. My biggest problem is that I end up confusing myself as I work along on the project (not school related). Thank you for any help! :)

-Kimo


Answer this question

Stream data in and calculate

  • Alepodrakoulas

    If you want to read the file line by line you would use the StreamReader.ReadLine() method. You know when to stop as the StreamReader.EndOfStream property is true.

  • BenSisson

    LOL...because I didn't think of that. Thank you for your help! My inexperience with application development (the coding side) has made this more complicated than was necessary.

  • MikeMinsk

    That one is not as simple to answer. The application I am writing is a stripped down model of the actual that will be written after the stripped version is proven to work. In the stripped version the text file's data looks like this.

    Company
    Code
    Profit Invested

    DHBO 153.69 25.89
    FTHY 34.78 24.98
    OPUI 45.90 12.67

    I was planning on streaming them into the List as strings and after accessing the data by their index in the List I could then convert the decimals to actual decimals for calculation purposes and display company codes with the ROI percentage in a datagrid or something like that.

    The full application will have to stream in text, dates, URLs, decimals and percentages from 3 Excel files and 1 tab delimited text file. A macro in an Access database then finishes the ROI reports but this application is supposed to replace that way of creating the ROI reports by running the calculations through a SQL database, then taking that data and creating a formatted Excel spreadsheet and writing the data into it and finally saving the report with the clients 4 letter code, report type and the date range of the report.

    I hope that wasn't "too" much information! :) Thank you for you help!

    -Kimo

  • Delusion7

    The code is ok but why don't you parse the lines into company/profit/invested directly You could use a DataTable to store the result.

  • LAE2

    Exactly what list or collection to use depends a bit what the data looks like. Can you describe or give a short sample on how the data looks like and what you need to calculate.

    An ArrayList, StringCollection or a generic List<> might all suite you. Depending on the "key"of the data a Hasttable or Dictionary<> might be better.

    ArrayList list = new ArrayList();
    //StringCollection list = new StringCollection();
    //List<string> list = new List<string>(); //.NET 2.0 only
    //Hashtable dict = new Hashtable();
    //Dictionary<int, string> dict = new Dictionary<int, string>(); //.NET 2.0 only
    while(false == stream.EndOfStream)
    {
    line = stream.ReadLine();
    list.Add(line);
    //split line into key and value
    //dict.Add(key, value);
    }

    If you have some more details on what you want to achieve I should be able to be more precise.



  • dct-val

    Andreas,

    Thank you for your time. The methods you have provided I know and understand. What I am specifically having problems with is streaming the data into a "List" (Collection ) so that I may access the indexes within the "List" (Collection ) and run the calculations from the data that way. For the most part I understand the syntax for a "List" but I am having problems figuring out how to stream the data from the text file directly into the "List" if that is possible. Thank you once again. :)

    -Kimo

  • Lampkin

    To help clearify a bit more after using the OpenFileDialog to get the text file the data is read into the list immediately. The "Next" button opens the next form, if the calculations work correctly. If so then the next form displays the ROI results from the calculations either in a "Rich Text Box" or a Datagrid, etc. Do I have the right idea

    private void cmdYList_Click(object sender, EventArgs e)

    {

    if (ofdList.ShowDialog() == DialogResult.OK)

    {

    StreamReader textIn = new StreamReader(ofdList.FileName);

    string ylFileName = System.IO.Path.GetFileName(ofdList.FileName);

    txtList.Text = ylFileName;

    List<string> list = new List<string>(); //.NET 2.0 only

    while (false == textIn.EndOfStream)

    {

    list.Add(textIn.ReadLine());

    }

    textIn.Close();

    }

    }


  • Stream data in and calculate