开发者

C# reassigning array values(down one step ex 5-4) that are within an object during a event

Just learning C# (along with object and event programing) and the teacher didn't really show us how to get things done.

class Postion
{
    private int[] x_coordinate = new int[100];
    private int[] y_coordinate = new int[100];
    private double[] speed = new double[100];
    private int[] direction = new int[100];
    const int MAX_SPEED = 50;
    int counter = 0;


    public Postion()
    {
        x_coordinate[counter] = 0;
        y_coordinate[counter] = 0;
        speed[counter] = 0;
        direction[counter] = 0;
    }


    //get set methods
    public int X
    {
        get
        {
            return x_coordinate[counter];
        }
        set
        {
            x_coordinate[counter] = value;
        }
    }

There is one more Class between them

The values are frist assigned by a button click.

 Airplane newplane = new Airplane();
  private void BtnCreate_Click(object sender, EventArgs e)
    {   
        bool box = txtName.Text != "";
        if (box == true)
            newplane.Name = txtName.Text;
        else { }

        box = txtx.Text != "";
        if (box == true)
            newplane.PlanePostion.X = int.Parse(txtx.Text);
        else { }

Etc.

I can call on the array values for display for the list box.

 private void lsbplanes_SelectedIndexChanged(object sender, EventArgs e)
    {
        placeholder = newplane.PlanePostion.Counter;
        newplane.PlanePostion.Counter = lsbplanes.SelectedIndex;
        if (newplane.PlanePostion.Counter < 0)
            newplane.PlanePostion.Counter = 0;
        else { }

        lblxshow.Text = Convert.ToString(newplane.Getx());

but when using a destroy button to remove an item in the list box I need to have it so the box updates with the new values when the user selects the item in the listbox.

This is what I have to try and do it so far, it sets all the ones above to 0s but does remove the the deleted one fine

private void BtnKill_Click(object sender, EventArgs e)
    {
        if (lsbplanes.SelectedIndex == -1)
        {
            MessageBox.Show("Please select an item first.", "开发者_StackOverflowNo item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        else
        {
            placeholder = lsbplanes.SelectedIndex;
            newplane.PlanePostion.Counter = lsbplanes.Items.Count;

            while (newplane.PlanePostion.Counter > placeholder)
            {
                placex = newplane.PlanePostion.X;
                placey = newplane.PlanePostion.Y;
                placespeed = newplane.Getspeed();
                placedic = newplane.Getdirection();

                newplane.PlanePostion.Counter--;

                newplane.PlanePostion.X = placex;
                newplane.PlanePostion.Y = placey;
                newplane.PlanePostion.Speed = placespeed;
                newplane.PlanePostion.Direction = placedic;

            }
            lsbplanes.Items.RemoveAt(lsbplanes.SelectedIndex);


            newplane.PlanePostion.Counter = lsbplanes.Items.Count;
        } 

anyone can help me on this?


I was torn in this question, answer exactly what your problem is, or suggest that you redesign it.

@Marc is right you should be using some sort of List<Position> on your Plane object (or a ReadOnlyObservableCollection<Position>).

@Marc is also right, that the problem you are having is that you are trying to push the values down from the end of the list and overwriting them. In these cases it is better to start from the deletion point and pull them down.

So if you have {1,2,3,4,5,6,7,8,9,10} and you delete from item 5, you would have {1,2,3,4,10,10,10,10,10,10}. The code below will let you end up with {1,2,3,4,6,7,8,9,0}

        placeholder = lsbplanes.SelectedIndex;
        int idx = placeholder;

        while (idx < lsbplanes.Items.Count)
        {
            newplane.PlanePosition.Counter = idx+1;
            placex = newplane.PlanePostion.X;
            placey = newplane.PlanePostion.Y;
            placespeed = newplane.Getspeed();
            placedic = newplane.Getdirection();

            newplane.PlanePostion.Counter = idx;

            newplane.PlanePostion.X = placex;
            newplane.PlanePostion.Y = placey;
            newplane.PlanePostion.Speed = placespeed;
            newplane.PlanePostion.Direction = placedic;

            idx++;

        }

        // Need to zero out elements at the end
        newplant.PlanePosition.Counter = lsbplanes.Items.Count;
        /* Zeroing code goes here */

        newplane.PlanePosition.Counter = placeholder;
        lsbplanes.Items.RemoveAt(lsbplanes.SelectedIndex);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜