开发者

Arraylist in C#

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.


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;
}


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.


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"});


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; }  
}


class Sector
{
    int id;
    string name;
}


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


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜