InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index [Resolved]

Posted by Babyeney under C# on 8/12/2011 | Points: 10 | Views : 9482 | Status : [Member] | Replies : 2
Hi. I'm trying to do shortest seek time first algorithm. I have this current code, and when I run it, I get this error:

InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index


Does anyone have a solution for my problem? Thank you!

private void btnSimulate_Click(object sender, EventArgs e)
{
int[] mylist = new int[lstbxQueue.Items.Count];
int indexOfNearest = 0;
while (lstbxQueue.Items.Count != 0)
{

for (int i = 0; i < lstbxQueue.Items.Count; i++)
{
int subtrahend = Convert.ToInt32(txtHeader.Text) - Convert.ToInt32(lstbxQueue.Items[i]);
int minuend = Convert.ToInt32(txtHeader.Text) - Convert.ToInt32(lstbxQueue.Items[indexOfNearest]);

if (subtrahend < 0)
{
subtrahend = subtrahend * -1;
}
if (minuend < 0)
{
minuend = minuend * -1;
}
if (subtrahend < minuend)
{
indexOfNearest = i;
}
}

int difference = Convert.ToInt32(txtHeader.Text) - Convert.ToInt32(lstbxQueue.Items[indexOfNearest]);
if (difference < 0)
{
difference = difference * -1;
}

lstboxSimulate.Items.Add(lstbxQueue.Items[indexOfNearest]);
lstbxQueue.Items.RemoveAt(indexOfNearest);
}

for (int x = 0; x < lstbxQueue.Items.Count; x++)
{
mylist[x] = lstboxSimulate.Items.Add(lstbxQueue.Items[indexOfNearest]);

}
foreach (int a in mylist)
{
lstboxSimulate.Items.Add(a);
}
}




Responses

Posted by: Nisar87 on: 8/13/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
if you point out the specific line, then this will be easier to identify the issue.
but usually this error occur when you try to remove item from empty listbox.
so before trying to delete make sure that listbox has atleast one item
[code]
if (lstbxQueue.SelectedIndex != -1)
{
lstbxQueue.Items.RemoveAt(indexOfNearest);
}
[/code]


_______________________________________
Nisar Ahmad
http://www.dotnetobject.com/

Babyeney, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Babyeney on: 8/13/2011 [Member] Starter | Points: 25

Up
0
Down
Thank you so much! It actually worked! :-)
You helped me a lot. Thank you thank you!

Babyeney, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response