有没有更好的方法来编写下面的VB6代码段?

有没有更好的方法来编写下面的VB6代码段?,vb6,styles,short-circuiting,select-case,Vb6,Styles,Short Circuiting,Select Case,我在$COMPANY工作,我正在帮助维护$LEGACY_应用程序。它是用VisualBasic6编写的 由于VB6无法在if语句中执行短路求值(这将大大简化这一过程),我面临着一个令人不快的复杂嵌套if语句。我也试过了,但没用。必须是在VB6之后添加的功能 某个地方的天才指出,如果你有耐心的话,你可以欺骗一个select case语句,使其像短路if语句一样工作,所以我尝试了一下,下面是我的想法: Select Case (True) ' pretend this is an if-else s

我在$COMPANY工作,我正在帮助维护$LEGACY_应用程序。它是用VisualBasic6编写的

由于VB6无法在if语句中执行短路求值(这将大大简化这一过程),我面临着一个令人不快的复杂嵌套if语句。我也试过了,但没用。必须是在VB6之后添加的功能

某个地方的天才指出,如果你有耐心的话,你可以欺骗一个select case语句,使其像短路if语句一样工作,所以我尝试了一下,下面是我的想法:

Select Case (True) ' pretend this is an if-else statement
    Case (item Is Nothing): Exit Sub ' we got a non-element
    Case ((item Is Not Nothing) And (lastSelected Is Nothing)): Set lastSelected = item ' we got our first good element
    Case (item = lastSelected): Exit Sub ' we already had what we got
    Case (Not item = lastSelected): Set lastSelected = item ' we got something new
End Select
这肯定有点不寻常,我不得不利用我的神奇白板(顺便说一句,它几乎是计算机之外最有用的编程资源)来确保我正确地映射了所有语句

这里发生了什么:我有一个昂贵的操作,我想避免重复,如果可能的话。lastSelected是对最近传递给此计算的值的持久引用。item是刚从GUI接收到的参数。如果以前从未调用过该程序,lastSelected将以Nothing开始。物品也可以什么都不是。此外,如果lastSelected和item都是相同的内容,请跳过计算

如果我在C++中写这个,我会写:

if (item == NULL || (lastSelected != NULL && item->operator==(*lastSelected))) return;
else lastSelected = item;
然而,我不是

问题 我怎样才能重写它,使它看起来更好,更有意义?回答“是,原因如下:X,Y,Z”或“否,原因如下:X,Y,Z”的答案将获得追加投票

编辑

修复C++语句匹配VB6(它们应该是等价的)< /P> < P>是< /P> 我从你的案情陈述中翻译过来的。我个人觉得它更容易阅读

If Item Is Nothing Then
  Exit Sub ' we got a non-element
ElseIf LastSelected Is Nothing Then
  Set LastSelected = Item ' we got our first good element
ElseIf Item = LastSelectedItem Then
  Exit Sub ' we already had what we got
Else
  Set LastSelected = Item ' we got something new
End If
你要求解释。我尽量不给太多(通过重新使用您自己的代码注释)

但无论如何,它在这里:-)

  • 首先,如果没有项目,请退出。简单
  • 否则,如果
    LastSelected为Nothing
    ,则我们知道,因为第一个if条件失败,
    项存在,并且可以安全地将该值标记为上次选择。正如你所说,我们得到了第一个好元素。潜艇继续前进
  • 但是,如果我们有
    上次选择的
    的现有值,那么它们要么相等,要么不相等。如果他们是平等的,就退出吧
  • 如果它们不相等,则更新
    LastSelected
    。正如你所说,我们有了新的东西
  • 是的

    我从你的案情陈述中翻译过来的。我个人觉得它更容易阅读

    If Item Is Nothing Then
      Exit Sub ' we got a non-element
    ElseIf LastSelected Is Nothing Then
      Set LastSelected = Item ' we got our first good element
    ElseIf Item = LastSelectedItem Then
      Exit Sub ' we already had what we got
    Else
      Set LastSelected = Item ' we got something new
    End If
    
    你要求解释。我尽量不给太多(通过重新使用您自己的代码注释)

    但无论如何,它在这里:-)

    • 首先,如果没有项目,请退出。简单
    • 否则,如果
      LastSelected为Nothing
      ,则我们知道,因为第一个if条件失败,
      项存在,并且可以安全地将该值标记为上次选择。正如你所说,我们得到了第一个好元素。潜艇继续前进
    • 但是,如果我们有
      上次选择的
      的现有值,那么它们要么相等,要么不相等。如果他们是平等的,就退出吧
    • 如果它们不相等,则更新
      LastSelected
      。正如你所说,我们有了新的东西

    这段文字更短,可读性更高100倍

    EDIT Wug将MarkJ原始答案中的代码编辑为:

    If (item Is Nothing)
        Then Exit Sub ' we got a non-element
    ElseIf (lastSelected Is Nothing) Then
        Set lastSelected = item ' we got our first go 
    ElseIf (item = lastSelected) Then
        Exit Sub ' we already had what we got
    End If
    Set lastSelected = item ' we got something new 
    
    下面是MarkJ的编辑回应。一个嵌套if,但仅一个集合。对我来说似乎更整洁

    If (item Is Nothing) Then 
      Exit Sub ' we got a non-element 
    ElseIf Not (lastSelected Is Nothing) Then ' not our first go
      If (item = lastSelected) Then 
        Exit Sub ' we already had what we got 
      End If 
    End If
    Set lastSelected = item ' we got something new
    ' does stuff here? @Wug is that true?
    
    • 要比较VB6中的引用相等性,请最后选择“使用项”。因为item=lastSelected可能会计算对象中的默认属性,并进行比较 既然简洁似乎是一个目标,考虑一下这个。如果在条件X为真时退出Sub,则以后无需再次检查X。这是假的!除非在两次评估之间更改其值(例如,X是检查系统时钟的函数)。您正在检查项目是否上次选中,然后检查是否未选中。若项目为Nothing,则不必检查项目是否为Nothing,也不必检查项目是否为True
    • VB6不会对电源短路
    • 不要担心VB6不是其他语言,放松点
    这段文字更短,可读性更高100倍

    EDIT Wug将MarkJ原始答案中的代码编辑为:

    If (item Is Nothing)
        Then Exit Sub ' we got a non-element
    ElseIf (lastSelected Is Nothing) Then
        Set lastSelected = item ' we got our first go 
    ElseIf (item = lastSelected) Then
        Exit Sub ' we already had what we got
    End If
    Set lastSelected = item ' we got something new 
    
    下面是MarkJ的编辑回应。一个嵌套if,但仅一个集合。对我来说似乎更整洁

    If (item Is Nothing) Then 
      Exit Sub ' we got a non-element 
    ElseIf Not (lastSelected Is Nothing) Then ' not our first go
      If (item = lastSelected) Then 
        Exit Sub ' we already had what we got 
      End If 
    End If
    Set lastSelected = item ' we got something new
    ' does stuff here? @Wug is that true?
    
    • 要比较VB6中的引用相等性,请最后选择“使用项”。因为item=lastSelected可能会计算对象中的默认属性,并进行比较 既然简洁似乎是一个目标,考虑一下这个。如果在条件X为真时退出Sub,则以后无需再次检查X。这是假的!除非在两次评估之间更改其值(例如,X是检查系统时钟的函数)。您正在检查项目是否上次选中,然后检查是否未选中。若项目为Nothing,则不必检查项目是否为Nothing,也不必检查项目是否为True
    • VB6不会对电源短路
    • 不要担心VB6不是其他语言,放松点 是的

      我会让这更简单:

      If item Is Nothing Then
        Exit Sub ' we got a non-element
      Else
        Set lastSelected = item ' we got something to assign
      End If
      
      除非分配lastItem有副作用(它可以是具有无效分配代码的属性),否则代码逻辑基本相同

      如果不需要退出sub(代码段位于sub或其他内容的末尾),则下一步更简单:

      If Not (item Is Nothing) Then Set lastSelected = item
      
      顺便说一句,你的
      selectcase(True)
      对VB程序员来说看起来真的很奇怪:)

      是的

      我会让这更简单:

      If item Is Nothing Then
        Exit Sub ' we got a non-element
      Else
        Set lastSelected = item ' we got something to assign
      End If
      
      除非分配lastItem有副作用(它可以是具有无效分配代码的属性),否则代码逻辑基本相同

      如果不需要退出sub(代码段位于sub或其他内容的末尾),那么下一步就是偶数simp