2014-12-06 72 views
0

我想添加速度列表,但我沒有意識到這樣做時,列表中的變量會改變,當我希望他們作爲常量。有人有主意嗎?非常感謝。列表:如何添加常量類

我的代碼

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

好問題,但請提供一個[*最小*代碼示例](http://stackoverflow.com/help/mcve)再現問題。 – Heinzi 2014-12-06 12:06:51

回答

0

你要實現你的VelocityICloneable接口:

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

謝謝你,你是最棒的! – ObnoxiousFrog 2014-12-06 15:39:20

+0

哦!你是第一個告訴我的! :d – Gnucki 2014-12-06 17:20:06