如何创建动态标签更改?

我有很多模型来设置信息标签,但这看起来很丑。如何创建简短的代码?

case "2er Coupé":
  auto BMW_2erCoupé = new auto(30.700f,1.850f,5,4.461f,420);
  price_result_lbl.Text = BMW_2erCoupé.getSetPrice.ToString(euro);
  weight_result_lbl.Text = BMW_2erCoupé.getSetWeight.ToString(kg);
  seats_result_lbl.Text = BMW_2erCoupé.getSetSeats.ToString();
  length_result_lbl.Text = BMW_2erCoupé.getSetLength.ToString(lenght);
  power_result_lbl.Text = BMW_2erCoupé.getSetPower.ToString(ps);
  modellView_img.Image = Properties.Resources._2erCoupé;
  break;
case "330e Limousine":
  auto BMW_330eLimousine = new auto(51.550f,2.300f,4.709f,292);
  price_result_lbl.Text = BMW_330eLimousine.getSetPrice.ToString(euro);
  weight_result_lbl.Text = BMW_330eLimousine.getSetWeight.ToString(kg);
  seats_result_lbl.Text = BMW_330eLimousine.getSetSeats.ToString();
  length_result_lbl.Text = BMW_330eLimousine.getSetLength.ToString(lenght);
  power_result_lbl.Text = BMW_330eLimousine.getSetPower.ToString(ps);
  modellView_img.Image = Properties.Resources._330eLimousine;
  break;
jf5437 回答:如何创建动态标签更改?

您可以使用所需的类型和相关名称来进行这样的重构,而不是在切换前的方法中使用带有lambda的valueX进行重构:

Action<float,float,Image> initialize
  = (value1,value2,value3,value4,value5,image) =>
  {
    var instance = new auto(value1,value5);
    price_result_lbl.Text = instance.getSetPrice.ToString(euro);
    weight_result_lbl.Text = instance.getSetWeight.ToString(kg);
    seats_result_lbl.Text = instance.getSetSeats.ToString();
    length_result_lbl.Text = instance.getSetLength.ToString(lenght);
    power_result_lbl.Text = instance.getSetPower.ToString(ps);
    modellView_img.Image = Image;
  };

您可以这样使用:

case "2er Coupé":
  initialize(30.700f,1.850f,5,4.461f,420,Properties.Resources._2erCoupé);
  break;

case "330e Limousine":
  initialize(51.550f,2.300f,4.709f,292,Properties.Resources._330eLimousine);
  break;

如果要获取自动实例:

Func<float,Image,auto> initialize
  = (value1,value5);
    price_result_lbl.Text = instance.getSetPrice.ToString(euro);
    weight_result_lbl.Text = instance.getSetWeight.ToString(kg);
    seats_result_lbl.Text = instance.getSetSeats.ToString();
    length_result_lbl.Text = instance.getSetLength.ToString(lenght);
    power_result_lbl.Text = instance.getSetPower.ToString(ps);
    modellView_img.Image = Image;
    return instance;
  };

您也可以创建本地方法或类方法,而不是lambda:

void or auto InitializeAutoAndControls(float value1,float value2,float value3,float value4,float value5,Image image)
{
  ...
}
本文链接:https://www.f2er.com/3151193.html

大家都在问