0

I'm using the Timer control and I want set the interval equal to the Proc_BurstTime of an object in a ListBox. I can't figure out how I can go about this.

The thing is, I have multiple things different objects in the ListBox; what I want to do is have the program go through the Proc_BurstTime of each object in the list.

So for example, I have a ProgressBar. There are 5 things in the ListBox with these Proc_BurstTime values (in ms): 5000 4000 1000 7000 2000 The program should go to the first object in the ListBox, set the progress bar to "load" for X seconds (Proc_BurstTime). The first one will be 5000 ms (5 seconds), the progress bar will go from 0 to 100 in 5 seconds, once completed, it will go to the next one, 4000 ms; so on so forth.

My issue is this:

timer1.Interval = initial_ProcessList.Items[0].Proc_BurstTime;

This is the error I get
*'object' does not contain a definition for 'Proc_BurstTime' and no extension method 'Proc_BurstTime' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)*

This is my structure and initialization:

    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 = 5000, Proc_Priority = 1},
        new process{ Proc_Id = 2, Proc_BurstTime = 4000, Proc_Priority = 2},
        new process{ Proc_Id = 3, Proc_BurstTime = 1000, Proc_Priority = 3},
        new process{ Proc_Id = 4, Proc_BurstTime = 7000, Proc_Priority = 4},
        new process{ Proc_Id = 5, Proc_BurstTime = 2000, Proc_Priority = 5}
    };

And this is how I am adding things to the ListBox:

private void Form1_Load(object sender, EventArgs e)
    {
        foreach (var p in ProcessList)
        {
            initial_ProcessList.Items.Add(p);
        }
    }
Evan
  • 2,405
  • 3
  • 20
  • 24
  • You might want to note that the [ListBox.ObjectCollection.Item Property](http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.item.aspx) returns an object. As an aside you might want to check out [Why are mutable structs evil?](http://stackoverflow.com/q/441309/119477) – Conrad Frix Sep 20 '13 at 21:22

1 Answers1

2

You'd have to cast it to the appropriate type:

timer1.Interval = ((process)initial_ProcessList.Items[0]).Proc_BurstTime;

Or if you have to access multiple properties, you'd probably want to do this:

process proc = (process)initial_ProcessList.Items[0];
timer1.Interval = proc.Proc_BurstTime;

Note that if you're looping through the processes in the ListBox, you can conveniently declare the iterator's type in the head of your foreach-loop:

foreach (process proc in initial_ProcessList.Items)
{
    timer1.Interval = proc.Proc_BurstTime;
    ...
}
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331