Arraylist in C #

I created a class called employee to store employee information. the class is 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 created an array called arrSector. it takes the name of the sectors with which the employee is associated. now I also want to take the sector identifier along with the sector name.

my question is how to implement the sector identifier as well as the sector name in one arraylist variable.
I want to keep the value of the sector identifier the same as the name of the sector together. any help is appreciated.

+3
source share
7 answers

Create an object to store 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;
}
+14

: ArrayList, , , .NET 2 . List<T>, , , .

, , , Hashtable Dictionary<TKey, TValue>. Hashtables - , () (). , , GUID .

, , Sector, .

, , Hashtable/Dictionary, - . , (, , ), - .

+5

, ArrayList, ArrayList, , SectorId, .

:

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

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

, . , Sector , , .

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; }  
}
0
class Sector
{
    int id;
    string name;
}


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

:

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

    public string Name { get; set; }
}

List<Sector> :

private List<Sector> sectors;
0

2 ( ), , (sh) :

  • Create a class (internal, public, your call) to save these values.
  • Create a structure, look here
  • Use KeyValuePair, it will contain both information and lame.

And all the other answers are also good, especially when you advise using shared lists.

-1
source

Source: https://habr.com/ru/post/1721999/