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.
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; }
: ArrayList, , , .NET 2 . List<T>, , , .
ArrayList
List<T>
, , , Hashtable Dictionary<TKey, TValue>. Hashtables - , () (). , , GUID .
Hashtable
Dictionary<TKey, TValue>
, , Sector, .
Sector
, , Hashtable/Dictionary, - . , (, , ), - .
, 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"});
, . , 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; } }
class Sector { int id; string name; } class Employee { ... List<Sector> sectors; }
public class Sector { public int Id { get; set; } public string Name { get; set; } }
List<Sector> :
List<Sector>
private List<Sector> sectors;
2 ( ), , (sh) :
And all the other answers are also good, especially when you advise using shared lists.
Source: https://habr.com/ru/post/1721999/More articles:Why does Perl complain about “Using uninitialized value” in my CGI script? - variablesHow to get multiple items from using PHP? - phpCustom exception handling in Python - pythonIs the default ASHX handler mapping missing in IIS 7.5 (Windows 7)? - asp.netRedirect Detection in IActionFilter.OnAction Done Reliably - asp.net-mvcHow to get a ToolStripMenuItem shortcut to work when a WinForms control is placed in VB6 - vb6Easy Data Access Layer - .netASP.net MVC - как сохранить модель по различным представлениям - model-view-controllerHow to safely stop the running thread at the request of the user? - c ++PHP development start - phpAll Articles