【问题标题】:Using two With statements使用两个 With 语句
【发布时间】:2013-01-23 19:35:43
【问题描述】:

首先,我在 onselect 期间让 combobox1 填充 combobox2。我开始了很长的路,见下文。

    procedure TFGetZoneDept.ComboBox1Select(Sender: TObject);
begin
  Combobox2.Clear;
  with Combobox1 do
  begin
      if text = '3' then
      begin
        with combobox2 do
        begin
          Add('Zone 3 depts');
          Add('Zone 3 depts');
          Add('Zone 3 depts');
          Add('Zone 3 depts');
          Add('Zone 3 depts');
          Add('Zone 3 depts');
        end;   {with combobox2}
      end;  {If }
      if text = '4' then
      begin
        with ComboBox2 do
        begin
          add('Zone 4 depts');
          add('Zone 4 depts');
          add('Zone 4 depts');
          add('Zone 4 depts');
          add('Zone 4 depts)';
        end;{combobox2 with}
      end;{IF}
      if text ='1' then
      begin
        with ComboBox2 do
        begin
          add('Zone 1 depts');
          add('Zone 1 depts');
          add('Zone 1 depts');
          add('Zone 1 depts');
          add('Zone 1 depts');
          add('Zone 1 depts');
        end; {combobox2 with}
      end; {IF}
      if text ='2' then
      begin
        with ComboBox2 do
        begin
          add('Zone 2 depts');
          add('Zone 2 depts');
          add('Zone 2 depts');
          add('Zone 2 depts');
          add('Zone 2 depts');
          add('Zone 2 depts');
        end; {Combobox2 with}
      end; {IF}
      if text ='BoneYard' then
      begin
        with ComboBox2 do
        begin
          add('BoneYard depts');
          add('BoneYard depts');
          add('BoneYard depts');
          add('BoneYard depts');
          add('BoneYard depts');
          add('BoneYard depts');
        end; {combobox2 with}
      end; {IF}
      if text = 'Misc' then
      begin
        with ComboBox2 do
          begin
            add('Misc Depts');
            add('Misc Depts');
            add('Misc Depts');
            add('Misc Depts');
            add('Misc Depts');
            add('Misc Depts');
          end; {combobox2 with}
      end; {IF}
  end;{combobox1 with}
  Combobox2.Enabled := true;
end;

我注意到你不能在里面使用一个 with 和另一个 with.. 或者我做错了。其次,我开始认为必须有更好的方法 :D 所以任何一个答案都可以。如何解决这个问题或以更好的方式做到这一点。

【问题讨论】:

  • 啊啊啊啊!它燃烧!它buuurrrnssss!
  • 这就是我讨厌with 声明的原因。

标签: delphi with-statement


【解决方案1】:

嵌套with 语句是完全可能的。这通常不是一个好主意,因为with 语句的坏处复合了,但是编译器在解释代码时没有问题。解析标识符时,编译器会简单地从内部语句到外部语句,直到找到具有它正在寻找的方法或属性的对象。

编译器找到的可能与您期望的不同。


您可以通过使用一些变量和循环来避免重复代码以及消除对with 语句的需要,从而使您的代码更加简洁。

procedure TFGetZoneDept.ComboBox1Select(Sender: TObject);
var
  text1, text2: string;
  i: Integer;
begin
  Combobox2.Clear;
  text1 := Combobox1.Text;
  if text1 = '3' then
    text2 := 'Zone 3 depots'
  else if text1 = '4' then
    text2 := 'Zone 4 depts'
  else if text ='1' then
    text2 := 'Zone 1 depts'
  else if text ='2' then
    text2 := 'Zone 2 depts'
  else if text ='BoneYard' then
    text2 := 'BoneYard depts'
  else if text = 'Misc' then
    text2 := 'Misc Depts';
  for i := 1 to 6 do
    Combobox2.Items.add(text2);
  Combobox2.Enabled := true;
end;

当您避免重复代码时,您也可以避免错误。除非您打算让所有案例都有六个选项除了区域 4,它只有五个。

【讨论】:

  • 啊,在您保证可以之后.. 我回去看到我在做 Combobox2 而不是 Combobox2.items 谢谢.. 你也认为这种情况可以吗?这就是这个函数正在做的所有事情,填充组合框 2?
  • 选项可能是 20 到 5
  • 然后为循环的上界引入另一个变量,和text2同时设置。
【解决方案2】:

如果您使用嵌套的 With 语句,请打自己的脸以节省时间。

人们对 GOTO 和 WITH 语句有强烈的争论。 WITH 语句更糟糕,因为它们可以嵌套,每次使用都会加重它们的邪恶。

With 的第一条规则,没有人谈论它(或使用它。)

【讨论】:

  • 没人谈论它吗?我们一直在谈论它。
  • 蹩脚的“搏击俱乐部”参考......搏击俱乐部的第一条规则是你不要谈论搏击俱乐部。
  • 格伦,相信我,你在编写代码时节省使用 with 语句的任何时间,在你调试和维护它时会用掉 10 倍。
  • @GlenMorse 你不会花时间打字。你花时间调试。更重要的是,如果您避免像 Q 中的代码那样重复自己,那么这就是减少打字和调试的方法。
  • 什么成本更高 - 创建代码或维护代码?如果您必须公开发布这样的代码,因为您无法弄清楚它在做什么……所有这些都是为了节省一些击键,那么您的优先级就会倒退。 (而且你应该有类来处理人口而不是这么多对控件的引用......这将是节省击键的更好方法!)
猜你喜欢
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 2020-02-29
相关资源
最近更新 更多