Help constructing a class !

Discussion in 'ASP .Net' started by Big George, Oct 12, 2006.

  1. Big George

    Big George Guest

    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 !
     
    Big George, Oct 12, 2006
    #1
    1. Advertising

  2. Big George

    Jon Paal Guest

    pass it in as a delimited string then parse it in the class.


    "Big George" <> wrote in message news:...
    > 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 !
    >
     
    Jon Paal, Oct 12, 2006
    #2
    1. Advertising

  3. 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");


    --
    Hope this helps,
    Mark Fitzpatrick
    Former Microsoft FrontPage MVP 199?-2006

    "Big George" <> wrote in message
    news:...
    > 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 !
    >
     
    Mark Fitzpatrick, Oct 12, 2006
    #3
  4. > 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
     
    Robert Haken [MVP], Oct 12, 2006
    #4
  5. Big George

    Big George Guest

    Thank you Robert !
    >
    > 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
     
    Big George, Oct 13, 2006
    #5
  6. Big George

    Big George Guest

    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
     
    Big George, Oct 13, 2006
    #6
  7. 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

    Big George wrote:
    > 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
     
    Sean Chambers, Oct 13, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Todd Bandrowsky

    Constructing a Class Given a String

    Todd Bandrowsky, May 21, 2004, in forum: C++
    Replies:
    1
    Views:
    356
    Victor Bazarov
    May 21, 2004
  2. Last Timer
    Replies:
    7
    Views:
    349
    Mike Wahler
    Feb 9, 2005
  3. Hyoung Lee

    Constructing Class in C

    Hyoung Lee, Jul 8, 2003, in forum: C Programming
    Replies:
    7
    Views:
    451
    Alan Balmer
    Jul 9, 2003
  4. Replies:
    5
    Views:
    449
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Mar 6, 2007
  5. Hicham Mouline
    Replies:
    3
    Views:
    482
    red floyd
    Nov 18, 2009
Loading...

Share This Page