CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2008
    Posts
    1

    reading csv into array

    Hello Folks,
    I want to read information in a text file and then display it in a message box. I have both an employees class with a constructor and my form with the streamreader and arrays. I'm stuck at pulling the information out of the arrays. Any ideas? Here is my code

    "amespace Employee_Homework
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    string[] strTemp;

    StreamReader sr;
    sr = File.OpenText("file.txt");
    Employees newEmployee;
    while (sr.Peek() != -1)
    {
    strTemp = sr.ReadLine().Split(',');
    newEmployee = new Employees(Convert.ToString(strTemp[0]), Convert.ToString(strTemp[1]), DateTime.Parse(strTemp[2]), Decimal.Parse(strTemp[3]));
    MessageBox.Show(newEmployee.ToString());

    }
    }
    }
    }"


    and my class:


    class Employees
    {
    string e_name = "";
    string e_position = "";
    DateTime hire_date = Convert.ToDateTime("dd/mm/yyyy");
    decimal e_salary = 0;


    public string EmpName
    {
    get { return e_name;}
    set { e_name = value; }
    }

    public string EnmpPosition
    {
    get { return e_position; }
    set { e_position = value; }
    }

    public DateTime DateHired
    {
    get { return DateTime.Parse(e_position); }
    set { hire_date = value; }
    }
    public decimal EmpSalary
    {
    get { return e_salary; }
    set { e_salary = value; }
    }

    public Employees(string newName, string newPosition, DateTime newDate, decimal newSalary)
    {
    e_name = newName;
    e_position = newPosition;
    hire_date = newDate;
    e_salary = newSalary;
    }
    }
    }

  2. #2
    Join Date
    Jun 2006
    Posts
    3

    Re: reading csv into array

    Not entirely sure what your question is, but I'd imagine the following line will cause you runtime problems...

    DateTime hire_date = Convert.ToDateTime("dd/mm/yyyy");

    as it'll have a problem parsing that string as a valid date (it's a date format, not a date). Since your Employees (plural - why?) class takes a datetime in the constructor, why bother with the declaration assignments at all? In this case with such a simple class you could use the member assignments in the constructor.

    Also it seems to me you want to override string ToString() display whatever string representation of the Employees class you want - something like this:

    public override string ToString()
    {
    return e_name + " " + e_position; // etc etc etc
    }

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Lightbulb Re: reading csv into array

    replace this line:
    string[] strTemp;

    with
    Code:
    ArrayList strTemp  = new ArrayList();
    Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]

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

Featured