5 Replies - 1447 Views - Last Post: 29 April 2009 - 12:08 AM Rate Topic: -----

#1 ThisIsPathetic20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-April 09

C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 23 April 2009 - 11:53 PM

Hey,
First off, my names is Al, and I'm an intermediate C++ programmer trying to get a project done for my Systems Analysis Class. Basically its an employee program and i couldn't seem to find any other forums that really answered my question...

I have an Employee Class, now when i start up my program, i want to create 'x' amount of those classes (depending on how many employees are read from the file) and then read in from the file into the appropriate classes.

Here is my class:

	class Employee
	{
		//Declare Employee Information//
		string UserName, Password, FirstName, LastName, Address, Unit, City, State, Email,
			SiteLocation, BuildingCode, OfficeNumber, Notes, Department, Manager, EmploymentStatus,
			EmploymentType, BirthDate, HireDate;
		char MiddleI;
		int SSN, EIN, ZipCode, HomePhone, CellPhone, WorkPhone, Ext, Salary;
		bool MaritalStatus, SMSAlert;

		//Constructor//
		public Employee(string cUserName, string cPassword, string cFirstName, string cLastName, string cAddress,
			string cUnit, string cCity, string cState, string cEmail, string cSiteLocation, string cBuildingCode,
			string cOfficeNumber, string cNotes, string cDepartment, string cManager, string cEmploymentStatus,
			string cEmploymentType, string cBirthDate, string cHireDate, char cMiddleI, int cSSN, int cEIN, int cZipCode,
			int cHomePhone, int cCellPhone, int cWorkPhone, int cExt, int cSalary, bool cMaritalStatus, bool cSMSAlert)
		{
			UserName = cUserName;
			Password = cPassword;
			FirstName = cFirstName;
			LastName = cLastName;
			Address = cAddress;
			Unit = cUnit;
			City = cCity;
			State = cState;
			Email = cEmail;
			SiteLocation = cSiteLocation;
			BuildingCode = cBuildingCode;
			OfficeNumber = cOfficeNumber;
			Notes = cNotes;
			Department = cDepartment;
			Manager = cManager;
			EmploymentStatus = cEmploymentStatus;
			EmploymentType = cEmploymentType;
			BirthDate = cBirthDate;
			HireDate = cHireDate;
			MiddleI = cMiddleI;
			SSN = cSSN;
			EIN = cEIN;
			ZipCode = cZipCode;
			HomePhone = cHomePhone;
			CellPhone = cCellPhone;
			WorkPhone = cWorkPhone;
			Ext = cExt;
			Salary = cSalary;
			MaritalStatus = cMaritalStatus;
			SMSAlert = cSMSAlert;
		}
		//Destructor//
		~Employee()
		{
		}
	}



Now i know what i want to do is similar to C++ coding...only I'm not quite sure how to do it all...its a pretty simple question, i just cant put it together. Any help is appreciated :) Thanks

-Al

P.S. i know it is a lot of data in my class, but if you follow this link you will see the design side of my program...i already made the program (only hard coded) i am looking to make it fully coded...not hard coded.

http://s137.photobuc...=ProfileTab.png

Is This A Good Question/Topic? 0
  • +

Replies To: C# Classes, Arrays of classes, and Reading a file into those classes..

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 24 April 2009 - 03:52 AM

first, I would look into creating public properties for all of those Employee variables. you are passing 30 parameters to the constructor.

here is how you read from a file...

string text = string.Empty;

using (TextReader tr = new StreamReader(@"C:\test.txt"))
{
    while (tr.Peek() > 0)
    {
        // this is assuming you change the constructor
        // to accept no arguments
        Employee emp = new Employee();
        
        text = tr.ReadLine();

        // Code to get the employee data out of the string
    }
}

Console.Write(sb.ToString());
Console.Read();



how is the data in the file arranged? is it comma separated? is it one file per employee or all employees in one file?
Was This Post Helpful? 0
  • +
  • -

#3 ThisIsPathetic20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-April 09

Re: C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 24 April 2009 - 04:16 AM

Since my class is public i thought that all of my data was already public :blink:

hmm, I think i am going to be reading it from a text file.

the data entered as follows:

Al
Stotz
W
123-23-321
4760 Lake Trail Drive
2B
etc...

then starting again with the next employee, and so on...

so basically, im going to type up 20 dummy employees, equaling 500 Lines to the text file (30 data entries * 20 employees)

i would love to make a database for the employees for easier entry, etc... but do not know how to even begin that, nor have the patience/time at this point...but in the future, yes. :)
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 24 April 2009 - 04:36 AM

View PostThisIsPathetic20, on 24 Apr, 2009 - 06:16 AM, said:

Since my class is public i thought that all of my data was already public :blink:

hmm, I think i am going to be reading it from a text file.

the data entered as follows:

Al
Stotz
W
123-23-321
4760 Lake Trail Drive
2B
etc...

then starting again with the next employee, and so on...

so basically, im going to type up 20 dummy employees, equaling 500 Lines to the text file (30 data entries * 20 employees)

i would love to make a database for the employees for easier entry, etc... but do not know how to even begin that, nor have the patience/time at this point...but in the future, yes. :)


The public class means that it's accessible outside of it's namespace. by default, variables are private to the class.

here is a sample Employee class....
public class Employee
{
	private string m_userName;
	private string m_password;
	private string m_firstName;
	private string m_lastName;

	public string UserName
	{
		get { return m_userName; }
		set { m_userName = value; }
	}

	public string Password
	{
		get { return m_password; }
		set { m_password = value; }
	}

	public string FirstName
	{
		get { return m_firstName; }
		set { m_firstName = value; }
	}

	public string LastName
	{
		get { return m_lastName; }
		set { m_lastName = value; }
	}

	// Empty Constructor
	public Employee()
	{

	}

	// Constructor passing in arguments
	public Employee(string userName, string password, string firstName, string lastName)
	{
		m_userName = userName;
		m_password = password;
		m_firstName = firstName;
		m_lastName = lastName;
	}
}



just an FYI, to easily create a property, right click on the variable --> Refactor --> Encapsulate Field --> OK.

and now your main program...
static void Main(string[] args)
{
	string text = string.Empty;
	int lineCounter = 1;
	List<Employee> empList = new List<Employee>();

	using (TextReader tr = new StreamReader(@"C:\test.txt"))
	{
		Employee emp = new Employee();

		while (tr.Peek() > 0)
		{
			while (lineCounter <= 20)
			{
				text = Console.ReadLine();

				// Now you need to use the line counter to let you know 
				// which line it is on so you will know what the data is.
				// You should possibly look into storing all the employee's data
				// on one line each and separating the data by commas.
				// It would make it much easier to get from the file
				if (lineCounter == 1)
				{
					emp.FirstName = text;
				}
				else if (lineCounter == 2)
				{
					emp.LastName = text;
				}
				// etc....

				lineCounter++;
			}

			empList.Add(emp);
			lineCounter = 1;
		}
	}

	Console.Read();
}



as I wrote in the code, storing the data as a comma-separated file would be much easier.

This post has been edited by eclipsed4utoo: 24 April 2009 - 04:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 ThisIsPathetic20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 23-April 09

Re: C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 25 April 2009 - 12:27 AM

wow, thank you!!!!! I really appreciate your help on this! Ill test it out and let ya know how it works....its all slowly coming back to me...hahaha
Was This Post Helpful? 0
  • +
  • -

#6 InternalStatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 28-April 09

Re: C# Classes, Arrays of classes, and Reading a file into those classes..

Posted 29 April 2009 - 12:08 AM

Well, I can certainly help cleaning up the File-related part. This is what your code would typically look like:

public void SomeFunction()
{
	using(System.IO.StreamReader reader = new System.IO.FileInfo(filePath).OpenRead())
	{
		while(!reader.EndOfStream)
		{
			  // Call reader.ReadLine() appropriately
		}
	}
}



OR, you can do the following:

foreach(System.String line in System.IO.File.ReadAllLines(filePath))
{
	// Use line appropriately
}



The second is obviously cleaner code-wise, but the first is more efficient memory-wise because it loads all of the file's lines into memory immediately, whereas the first just traverses through the file. The "using" statement is useful because it calls the object's Dispose() method at the end of the block (this is like closing the stream and deleting the handle in C++). C# is full of things like this, in order to prevent memory-leaks.

If your files start getting too large to read all of that information into memory, you may be able to store locations in the file (as Int64's) instead of actual strings of data. That way, you'll have your stream open the whole time, but when you call a property of the employee class, you'll set the stream's Position property that that of the property they wish to access, and then call ReadLine().

Hope that helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1