清单:如何添加常量类

时间:2014-12-06 12:04:02

标签: c#

我正在尝试将速度添加到列表中,但我没有意识到这样做,当我希望它们作为常量时,列表中的变量将会改变。有人有主意吗?非常感谢。

我的代码

    void CalculateVelocity(List<velocity> ListOfVelocity, particle newprojectile, Environment newEnvironment)
    {
        //load in stats
        newEnvironment.gravity = -9.8;
        newprojectile.TimeOfFlight = Convert.ToDouble(txtbox_TimeOfFlight.Text);
        newprojectile.InitialVelocity.Magnitude = Convert.ToDouble(txtbox_InitialVelocity.Text);
        newprojectile.InitialVelocity.AngleOfTravel = Convert.ToDouble(txtb_AngleOLaunch.Text);
        double TimeInterval;
        double FlightTime =0;
        double InitialHVelocity;
        velocity newVelocity;
        newVelocity = new velocity();
        newVelocity.Magnitude = Convert.ToDouble(txtbox_InitialVelocity.Text);
        newVelocity.AngleOfTravel = Convert.ToDouble(txtb_AngleOLaunch.Text);
        newVelocity.AngleOfTravel = newprojectile.InitialVelocity.AngleOfTravel;
        velocity.CalculateVComponent(newVelocity);
        velocity.CalculateHComponent(newVelocity);
        InitialHVelocity = newVelocity.HorizontalVelocity;
        ListOfVelocity.Add(newVelocity);
        if (newprojectile.TimeOfFlight > 60)
        {
            TimeInterval = newprojectile.TimeOfFlight / 60;
        }
        else
        {
            TimeInterval = 1;
        }
        FlightTime =FlightTime+ TimeInterval;
        while (!(newprojectile.TimeOfFlight < FlightTime))
        {
            velocity.CalculateVComponent(newVelocity, FlightTime, newEnvironment, newVelocity.VerticleVelocity);
            ListOfVelocity.Add(newVelocity);
            FlightTime = FlightTime + TimeInterval;
        }
    }

1 个答案:

答案 0 :(得分:0)

您必须在班级ICloneable中实施Velocity界面:

class velocity() : ICloneable
{
    #region ICloneable Members

    public object Clone()
    {
        newVelocity = new Velocity();

        newVelocity .Magnitude = this.Magnitude;
        // ...

        return newVelocity ;
    }

    #endregion
}

然后克隆代码中的对象:

    void CalculateVelocity(List<velocity> ListOfVelocity, particle newprojectile, Environment newEnvironment)
    {
        //load in stats
        newEnvironment.gravity = -9.8;
        newprojectile.TimeOfFlight = Convert.ToDouble(txtbox_TimeOfFlight.Text);
        newprojectile.InitialVelocity.Magnitude = Convert.ToDouble(txtbox_InitialVelocity.Text);
        newprojectile.InitialVelocity.AngleOfTravel = Convert.ToDouble(txtb_AngleOLaunch.Text);
        double TimeInterval;
        double FlightTime =0;
        double InitialHVelocity;
        velocity newVelocity;
        newVelocity = new velocity();
        newVelocity.Magnitude = Convert.ToDouble(txtbox_InitialVelocity.Text);
        newVelocity.AngleOfTravel = Convert.ToDouble(txtb_AngleOLaunch.Text);
        newVelocity.AngleOfTravel = newprojectile.InitialVelocity.AngleOfTravel;
        velocity.CalculateVComponent(newVelocity);
        velocity.CalculateHComponent(newVelocity);
        InitialHVelocity = newVelocity.HorizontalVelocity;
        ListOfVelocity.Add(newVelocity);
        if (newprojectile.TimeOfFlight > 60)
        {
            TimeInterval = newprojectile.TimeOfFlight / 60;
        }
        else
        {
            TimeInterval = 1;
        }
        FlightTime =FlightTime+ TimeInterval;
        while (!(newprojectile.TimeOfFlight < FlightTime))
        {
            newVelocity = (velocity)newVelocity.clone(); // HERE!
            velocity.CalculateVComponent(newVelocity, FlightTime, newEnvironment, newVelocity.VerticleVelocity);
            ListOfVelocity.Add(newVelocity);
            FlightTime = FlightTime + TimeInterval;
        }
    }