列表:如何添加常量类

本文关键字:添加 常量 何添加 列表 | 更新日期: 2023-09-27 18:33:24

我正在尝试将速度添加到列表中,但我没有意识到这样做,当我希望它们作为常量时,列表中的变量会发生变化。有人有什么想法吗?多谢。

我的代码

    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;
        }
    }

列表:如何添加常量类

你必须

在你的类Velocity中实现ICloneable接口:

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;
        }
    }