如何在没有重复代码(VBA)的情况下编写or语句

时间:2020-01-08 23:43:19

标签: vba

我知道在大多数其他语言中,您可以编写表达式gb来测试变量的有效性和要执行的测试。 VBA不允许您这样做。因此,我竭尽全力想出一种方法来编写以下代码,而无需进行以下操作:1)使用错误捕获作为条件分支的手段,以及2)不复制代码。

class Test extends Component {
  constructor(props) {
    super(props);
    this.state = {
      data: "",
      dummy: [
        {
          _id: "5e12905eb10fe53808d1ca55",
          name: "WHY NAME EXISTS -_-",
          stage: "Confirmed",
          feedback: {
            _id: "5e12905eb10fe53808d1ca56",
            rating: 1,
            review: "bad bad only bad."
          },

          itemDetails: [
            {
              _id: "5e12905eb10fe53808d1ca5a",
              nameXquantity: "Lehsun Adrak x100",
              individualTotal: 155
            },
            {
              _id: "5e12905eb10fe53808d1ca59",
              nameXquantity: "Lehsun x50",
              individualTotal: 25
            },
            {
              _id: "5e12905eb10fe53808d1ca58",
              nameXquantity: "Lehsun Adrak Dhaniya Shimla mirch x Infinity",
              individualTotal: 9969
            }
          ],

          __v: 0
        }
      ]
    };
  }


  render() {
    return (
      <SafeAreaView>
        <ScrollView>
          <FlatList
            data={this.state.dummy}
            renderItem={({ item }) => (
              <View>
                <Text>{item.name}</Text>
                <FlatList
                  data={item.itemDetails}
                  renderItem={({ item }) => <Text>{item.nameXquantity}</Text>}
                  keyExtractor={item => item._id}
                />
              </View>
            )}
            keyExtractor={item => item._id}
          />
        </ScrollView>
      </SafeAreaView>
    );
  }
}

export default Test;


现在,无论vSize是否为0(如果vData从未被删除),该代码都将在UBound行上崩溃。我能看到的唯一另一种方法是执行一条额外的elseif语句来检查UBound,但这将复制将向量大小加倍的代码。

如果您认为这是重复项:VBA Short-Circuit `And` Alternatives。这讨论了AND语句的替代方案(不是or)。嵌套的if(也称为AND语句)不会像OR那样重复代码。

2 个答案:

答案 0 :(得分:2)

如果我理解正确,则需要检查数组是否已分配。

一个这样的选择就是做到这一点(尽管看起来很奇怪):

import sqlite3
def DB():    
    List = {"Name":"Omar", "Age":"33"}

    columns = ', '.join("" + str(x).replace('/', '_') + "" for x in List.keys())     
    values = ', '.join("'" + str(x).replace('/', '_') + "'" for x in List.values())

    sql_qry = "INSERT INTO %s ( %s ) values (?,?) ; ( %s )" % ('Table Name', columns, values)


    conn = sqlite3.connect("DBname.db")
    curr = conn.cursor()
#     curr.execute("""create table if not exists TestTable(
#                         Name text, 
#                         Age text
#                         )""")

#     print columns
#     print values
#     print sql



    #     sql = 'INSERT INTO yell (Name , Age) values (%s, %s)'
    curr.execute(sql_qry)    
DB()

取自this thread的答案-有关更多建议,请参见。

答案 1 :(得分:0)

使用SnowGroomer的答案,我正在发布完整的解决方案:

这是一个名为Vector

的类
Private data() As Variant
Private size As Long

Property Get vSize() As Long
    vSize = size
End Property

Property Let vData(ByVal index As Long, elem As Variant)
    If index < 0 Then Exit Property
    If index < size Then
        data(index) = elem
    Else
        Me.add elem, index
    End If
End Property

Property Get vData(ByVal index As Long) As Variant
    If index < 0 Or (Not Not data) = 0 Then
        vData = Nothing
        Exit Property
    End If
    vData = data(index)
End Property

Public Sub add(elem As Variant, Optional index As Long = -1)
    If index > -2 Then
        If index = -1 Then
            If size = 0 Or (Not Not data) = 0 Then
                ReDim data(0)
                data(size) = elem
                size = size + 1
                Exit Sub
            Else 'size <> 0
                ReDim Preserve data(0 To size * 2 + 1)
                data(size) = elem
                size = size + 1
            End If
        Else 'index <> -1
            If index >= size Then
                ReDim Preserve data(0 To index)
                data(index) = elem
                size = index + 1
            Else 'index < vSize
                data(index) = elem
            End If 'index >= vSize
        End If 'index = -1
    End If 'index > -2
End Sub 'add