Reputation: 348
I am trying to delete a value from a passed array at the selected index. Basically taking the selected index of a list box that displays the arrays and using it to delete an entry. For some reason nothing is deleted and stays the same when I run the program. I am programing in Visual Studio 2010 in C#
public double [] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
for (int i = selectedIndex; i < ary.GetUpperBound(0); i++)
{
redoneArray[i] = ary[i + 1];
}
//redoneArray[ary.GetUpperBound(0)] = 0;
return redoneArray;
}
Here is the delete button portion of code
private void btnDelete_Click(object sender, EventArgs e)
{
int selectedIndex;
if (this.lstClients.SelectedIndex <= 1)
{
return;
}
//else if ((this.lstClients.SelectedIndex - 2) < arrayIndex)
else
{
selectedIndex = this.lstClients.SelectedIndex - 2;
this.lstClients.Items.RemoveAt(this.lstClients.SelectedIndex);
lbSI.Text = (this.lstClients.SelectedIndex - 2).ToString();
redrawArray(mFirstNameArray, selectedIndex);
redrawArray(mLastNameArray, selectedIndex);
redrawArray(mAgeArray, selectedIndex);
redrawArray(mHeightArray, selectedIndex);
redrawArray(mStartWeightArray, selectedIndex);
redrawArray(mGoalWeightArray , selectedIndex);
redrawArray(mTotalWeeksArray, selectedIndex);
//arrayIndex += -1;
lstClients.Items.Clear();
loadListBox();
}
}
And here is my main code that loads it into the list
private void loadListBox()
{
string currentClient;
int lineNumber = 0;
string formattedName;
string strAvgBMI;
string strLowBMI;
string strHghBMI;
double dStartAvgBMI;
double dStartLowBMI;
double dStartHghBMI;
double dEndAvgBMI;
double dEndLowBMI;
double dEndHghBMI;
lstClients.Items.Add(" CLIENT NAME AGE HEIGHT(in) START WEIGHT START BMI GOAL WEIGHT GOAL BMI WEEKS");
lstClients.Items.Add("================= ===== =========== ============== =========== ============= ========== =========");
for (int index = 0; index < arrayIndex; index++)
{
if (mFirstNameArray[index] == null)
{
continue;
}
lineNumber++;
formattedName = mFirstNameArray[index];
formattedName += " ";
formattedName += mLastNameArray[index];
mStartBMI[index] = calcBMI(mHeightArray[index], mStartWeightArray[index]);
mEndBMI[index] = calcBMI(mHeightArray[index], mGoalWeightArray[index]);
currentClient = index.ToString() + " ";
currentClient += formattedName.PadRight(18) + " ";
currentClient += mAgeArray[index].ToString("##").PadLeft(4) + " ";
currentClient += mHeightArray[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mStartWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mStartBMI[index].ToString("##.#0").PadRight(4) + " ";
currentClient += mGoalWeightArray[index].ToString("###.0").PadRight(4) + " ";
currentClient += mEndBMI[index].ToString("###.#0").PadRight(4) + " ";
currentClient += mTotalWeeksArray[index].ToString("##").PadRight(4);
lstClients.Items.Add(currentClient);
}
dStartAvgBMI = sumAvg(mStartBMI, arrayIndex);
dStartHghBMI = maxArray(mStartBMI, arrayIndex);
dStartLowBMI = minArray(mStartBMI, arrayIndex);
dEndAvgBMI = sumAvg(mEndBMI, arrayIndex);
dEndHghBMI = maxArray(mEndBMI, arrayIndex);
dEndLowBMI = minArray(mEndBMI, arrayIndex);
strAvgBMI = "";
strHghBMI = "";
strLowBMI = "";
strAvgBMI = " Average: " + dStartAvgBMI.ToString("0#.#0") + " " + dEndAvgBMI.ToString("0#.#0");
strHghBMI = " High: " + dStartHghBMI.ToString("0#.#0") + " " + dEndHghBMI.ToString("0#.#0");
strLowBMI = " Low: " + dStartLowBMI.ToString("0#.#0") + " " + dEndLowBMI.ToString("0#.#0");
lstClients.Items.Add(strAvgBMI);
lstClients.Items.Add(strHghBMI);
lstClients.Items.Add(strLowBMI);
}
Upvotes: 1
Views: 261
Reputation: 149068
I'd strongly recommend using a List<double>
instead, since it has a more graceful and efficient strategy for adding and removing items.
There is a significant problem with your algorithm, because you haven't actually used selectedIndex
to filter out the 'deleted' item. I think it should look like this
public double[] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
int i = 0;
for (; i < selectedIndex; i++)
{
redoneArray[i] = ary[i];
}
for (; i < redoneArray.Length; i++)
{
redoneArray[i] = ary[i + 1];
}
return redoneArray;
}
Or even better:
public double[] redrawArray(double[] ary, int selectedIndex)
{
double[] redoneArray = new double[ary.GetUpperBound(0)];
Array.Copy(ary, redoneArray, selectedIndex);
Array.Copy(ary, selectedIndex + 1,
redoneArray, selectedIndex, redoneArray.Length - selectedIndex);
return redoneArray;
}
Update
The real problem, however, is that you're redrawArray
method returns a new array rather than modifying the existing array. You'd have to use assign the result array back to your variables, like this:
mFirstNameArray = redrawArray(mFirstNameArray, selectedIndex);
mLastNameArray = redrawArray(mLastNameArray, selectedIndex);
mAgeArray = redrawArray(mAgeArray, selectedIndex);
mHeightArray = redrawArray(mHeightArray, selectedIndex);
mStartWeightArray = redrawArray(mStartWeightArray, selectedIndex);
mGoalWeightArray = redrawArray(mGoalWeightArray , selectedIndex);
mTotalWeeksArray = redrawArray(mTotalWeeksArray, selectedIndex);
Upvotes: 2
Reputation: 62002
With Linq I guess you can:
public double[] RedrawArray(double[] ary, int selectedIndex)
{
return ary.Where((d, i) => i!=selectedIndex).ToArray();
}
or:
public void RedrawArray(ref double[] ary, int selectedIndex)
{
ary = ary.Where((d, i) => i!=selectedIndex).ToArray();
}
depending on which syntax is most convenient when calling the method.
Note that in either case the array passed to this method should not be modified by other threads while the method is running. In the ref
case, if a field or captured variable was passed, that variable must not be re-assigned by other threads.
Upvotes: 0
Reputation: 63105
public double[] RedrawArray(double[] ary, int selectedIndex)
{
var lst = new List<double>(ary);
lst.RemoveAt(selectedIndex);
return lst.ToArray();
}
Upvotes: 0
Reputation: 4328
If you want an array which you can delete things from, then its best to use something like List<double>
, which will save you a lot of trouble.
Then to delete at a particular index, just call .RemoveAt(index)
If you still want to use Arrays externally, you can 'cheat' by using the array.ToList()
function to get yourself a list, delete whatever it is you want, and then .toArray()
it back. Yeah its quite inefficient, but I don't think what you're currently doing is that fast as it is.
Upvotes: 1
Reputation: 65087
You're starting from the selectedIndex
.. but the new array doesn't contain any of the source array. This makes everything before the index 0
. This can be solved with Array.Copy
.
public static double[] redrawArray(double[] ary, int selectedIndex) {
double[] redoneArray = new double[ary.GetUpperBound(0)];
Array.Copy(ary, redoneArray, ary.GetUpperBound(0)); // copy the source into the destination minus one..
for (int i = selectedIndex; i < ary.GetUpperBound(0); i++) {
redoneArray[i] = ary[i + 1];
}
return redoneArray;
}
Example usage:
double[] arr = new double[] {2, 4, 6};
// remove first
arr = redrawArray(arr, 0); // {4, 6}
// remove second
arr = redrawArray(arr, 1); // {2, 6}
// etc..
Upvotes: 2