Home > DeveloperSection > Forums > Adding data to an array of struct
lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    March-02-2015 11:39 PM

 .NET C#  .NET 
Ratings:


 1 Reply(s)
 483  View(s)
Rate this:

I'm trying to add/remove data from an array of a defined struct.


struct process
{                   
    public int Proc_Id;
    public int Proc_BurstTime;
    public int Proc_Priority;
    public override string ToString()
    {
        return "ID: " + Proc_Id.ToString() + " Time: " + Proc_BurstTime.ToString() + " Prior: " + Proc_Priority.ToString();
    }
};
 
readonly process[] ProcessList = new process[]
{
    new process{ Proc_Id = 1, Proc_BurstTime = 3000, Proc_Priority = 1},
    new process{ Proc_Id = 2, Proc_BurstTime = 5000, Proc_Priority = 2},
    new process{ Proc_Id = 3, Proc_BurstTime = 1000, Proc_Priority = 3},
    new process{ Proc_Id = 4, Proc_BurstTime = 10000, Proc_Priority = 4}
};

Basically, what I'm exactly trying to do is populate this array of structs into a ListBox control. I have been able to initially populate it by using DataSource, I've also been able to modify the contents and "re-datasource" it to update the ListBox. However, I cannot ADD or REMOVE processes. Does anyone know what I can do?



Brad Pitt
Brad Pitt

Total Post:61

Points:429
Supported
Posted on    March-03-2015 2:12 AM

You'll need to use a collection.

A List<T> would do just fine.

var processes = new List<process>();
processes.Add(new process
{
    Proc_Id = 1,
    Proc_BurstTime = 3000,
    Proc_Priority = 1
});

Modifying your code a bit would result in this below.

List<process> ProcessList = new List<process>()
{
    new process {Proc_Id = 1, Proc_BurstTime = 3000, Proc_Priority = 1},
    new process {Proc_Id = 2, Proc_BurstTime = 5000, Proc_Priority = 2},
    new process {Proc_Id = 3, Proc_BurstTime = 1000, Proc_Priority = 3},
    new process {Proc_Id = 4, Proc_BurstTime = 10000, Proc_Priority = 4}
};

This collection type also has the Add method you are referring to in your question.


Don't want to miss updates? Please click the below button!

Follow MindStick