Help constructing a class !

B

Big George

Hello,

I develop in ASP.NET with VB.NET code.

I need some help constructing a class: Worker. I'm designing the
properties of this class.
The class is filled reading tables in database.

Properties of Class Worker:
- ID_Card
- Names of Person
- ID_Current_Company
- Name of Current Company
- Is Worker working at the company? (Yes, No)
- Projects that worker worked in
- Project 1 (ID and name)
- Project 2 (ID and name)
- Project 3 (ID and name)
- ...

My problem is with property Projects_Worked_In. A worker could have
been in many projects. I don't know how many projects a worker has
worked at. It could be only 1 project or 5 projects.

How would you desing this property Projects_Worked_In of class Worker?

I was thinking to:
- Define 50 properties like Project1, Project2, Project3,... Project50,
all of them initialized in NULL
- Define a property Number_Of_Projects and according to that I will
know if I have to display Project1, Project2, Project3, etc.

Problem is what happen if Number_Of_Projects > 50

Thank you for your help !
 
M

Mark Fitzpatrick

You use a collection instead. You could actually do this in several ways.
You could create a NameValueCollection (located in the
System.Collections.Specialized namespace). This way you could put an id and
name into the collection. In this case, most people would probably put the
ID number as the name, and the name of the project as the value. You create
the namevaluecollection just as you would any other field. Then, you can add
projects to it as needed.

for example:
// initialize the collection
private NameValueCollection col = new NameValueCollection

then you can add to it such as:
col.Add(Id1.ToString(),"ProjectName1");
That's assuming that Id1 is a number.

Now, what if you wanted to make that a property. You woud do it like so

public NameValueCollection Projects
{
get
{
return col;
}
set
{
col = value;
}
}


You cuold then access it like so:
Worker.Projects.Add("1","Project1");
 
R

Robert Haken [MVP]

Properties of Class Worker:
- ID_Card
- Names of Person
- ID_Current_Company
- Name of Current Company
- Is Worker working at the company? (Yes, No)
- Projects that worker worked in
- Project 1 (ID and name)
- Project 2 (ID and name)
- Project 3 (ID and name)
- ...

My problem is with property Projects_Worked_In. A worker could have
been in many projects. I don't know how many projects a worker has
worked at. It could be only 1 project or 5 projects.

How would you desing this property Projects_Worked_In of class Worker?

I was thinking to:
- Define 50 properties like Project1, Project2, Project3,... Project50,
all of them initialized in NULL
- Define a property Number_Of_Projects and according to that I will
know if I have to display Project1, Project2, Project3, etc.

Problem is what happen if Number_Of_Projects > 50

Forget these ways to hell...

1) You have to make second class - Project (props ID and Name).
2) Add property Worker.Projects of type IList(Of Project), or other
collection-type.

....than, you can have any number of project that the worker worked on and
coding like this:
Worker.Project.Add(FirstProject)
Worker.Project.Add(SecondProject)
Worker.Project.Remove(FirstProject)
Worker.Project.Count

Robert Haken [MVP ASP/ASP.NET]
HAVIT, s.r.o., www.havit.cz
http://knowledge-base.havit.cz
 
B

Big George

Maybe you are stil on line:

How do you declare and instance these classes:

Private oWorker as Worker(myID_Card)

' Then how do you instance or call class "Project" ?
' Is enough to do:
oWorker.Project.Add(FirstProject)

But .NET doesn't allow this

Sorry about my ignorance
 
S

Sean Chambers

It depends on what type the "Project" field is.

Let's just say for the sake of argument that it's some type of
collection, Depending on the requirements of the class, you usually
instantiate it when you make a new instance of your Worker class:

public class Worker {

private List<Project> _projects;
public List<Project> Projects {
get { return _projects; }
set { _projects = value; }
}

public Worker() {
_projects = new List<Project>();
}
}

Then, whereever else in your code you can then add like so:

private oWorker = new Worker();
oWorker.Projects.Add(FirstProject);
oWorker.Projects.Add(SecondProject);
int projectCount = oWorker.Projects.Count;

you get the idea.

hope that helps!

Sean
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top