3

i have created a class named employees to hold the employee information. the class looks as follows.

class Employee
{
    private int employeeID;
    private string firstName;
    private string lastName;
    private bool eligibleOT;
    private int positionID;
    private string positionName;
    private ArrayList arrPhone;
    private ArrayList arrSector;

as you can see i have created an array named arrSector. it takes the name of the sectors which the employee is associated. now i also want to take in the sector id along with the sector name.

my question is how do i implement the sector id as well as the sector name in a single arraylist variable.
i want to store the value of sector id as wel as the sector name together. any help is appreciated.

7 Answers 7

14

Create an object to hold both pieces of information.

public class Sector
{
    public string Name { get; set; }
    public int Id { get; set; }
}

Then use a generic List instead of an ArrayList.

class Employee
{
    private int employeeID;
    private string firstName;
    private string lastName;
    private bool eligibleOT;
    private int positionID;
    private string positionName;
    private ArrayList arrPhone;
    private List<Sector> arrSector;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Definitely the way to do it. While you're at it you might as well suggest he change arrPhone to a List<int> (or List<string> depending on how the numbers are stored).
@Dan: Yeah that's true, but I didn't want to step outside the bounds of the specific problem.
5

First of all: Don't use ArrayList if you can help it, at least if you're using .NET 2 or later. You can use the generic List<T> which is specific to the type you put in it, which saves you a lot of casting.

As for your problem, you probably want a Hashtable or Dictionary<TKey, TValue>. Hashtables are collections that store associations of one value (the key) to another value (the value). In your case you'd probably have an integer or a GUID as key and a string as value.

But as others have noted, you can also create a Sector class which essentially consists of the ID and a name and put instances of that class into your list.

What you gain here when using a Hashtable/Dictionary is that you have quick lookup by ID. When you search for a specific ID in a list you would have to iterate through the list (well, if it's sorted you can use a binary search) while a hashtable requires just a single lookup usually.

Comments

3

You might want to use a Dictionary instead of an ArrayList, but if you have to use an ArrayList, I would create a class or struct that holds both the SectorId and the SectorName.

With a Dictionary:

Dictionary<int, string> dictSector = new Dictionary<int, string>();
dictSector.Add(1,"MySectorName");
dictSector.Add(2,"Foo");
dictSector.Add(3,"Bar");

With an ArrayList:

class Sector {
  public int Id {set; get;}
  public string Name {set; get;}
}

ArrayList arrSectors = new ArrayList();
arrSectors.Add(new Sector() {Id = 1, Name = "MySectorName"});
arrSectors.Add(new Sector() {Id = 2, Name = "Foo"});
arrSectors.Add(new Sector() {Id = 3, Name = "Bar"});

Comments

0

Not really sure what you mean. I think you should implement a Sector class and probably also use generic lists.

class Employee
{
    // field
    private List<Sector> sectors;
    // property to get the sectors
    public List<Sector> Sectors { get { return this.sector; }
}

// sector class
class Sector
{
  int Id { get; set; }
  string Name { get; set; }  
}

Comments

0
class Sector
{
    int id;
    string name;
}


class Employee
{
   ...
   List<Sector> sectors;
}

Comments

0

Define a new Sector class:

public class Sector
{
    public int Id { get; set; }

    public string Name { get; set; }
}

Then define the list as a List<Sector> like this:

private List<Sector> sectors;

Comments

-1

If your sector has 2 values (ID and name), guess you (sh)could:

  1. Create a class (inner, public, your call) to hold those values.
  2. Create a struct, see it here
  3. Use a KeyValuePair, it will hold both info, but this is lame.

And all other answers are good as well, specially when advising you to use generic lists.

1 Comment

Strongly disagree on KeyValuePairs being a lame solution since that would make Dictionary<int,T> a lame solution, which it by no means is

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.