【问题标题】:Data Mapping with function [closed]具有功能的数据映射[关闭]
【发布时间】:2015-02-20 07:50:09
【问题描述】:

有人可以帮我使用这个函数吗?这个函数的目的应该是从数据库中获取值并更改名称,如

存款,提款,手动余额更正,现金更正,HighRoller存款更正,HighRoller提款更正,玩家到玩家发送,玩家到玩家接收,现金兑现存款,现金兑现提款,小费存款,小费提款。数据库中的原始表是

Id Name                    IsCredit
1  Deposit                      0 
2. Withdrawal                   0 
3. Manual Balance Correction    0
4. Cash Correction              0
5. HighRoller Correction        0 or 1
6. Player To Player             0 or 1
7. Cash In Cash Out             0 or 1
15. Tip                         0 or 1

Function GetTransactionTypeName(ByVal key As Integer, ByVal isCredit As Boolean) As String

Dim keys As Integer() = {1,2,3,4,5,6,7,15}

Dim names As String() = {"Deposit", "Withdrawal", "Manual Balance Correction", "Cash Correction", "High Roller {0} Correction", _
"Player To Player {0}", "Cash In Cash Out {0}", "Tip {0}"}

Dim indx As Integer

For indx = 0 To Ubound(keys)
If keys(indx) = key Then
Exit For
End If
Next

If indx >= Ubound(names) Then
Return key
End If

If key=7 Or key=5 Or Key=6 or Key=15 Then

If  isCredit Then
If key=7 Or key=5 Or key=15
Return [String].Format(names(indx) ," Deposit")
End If
If key=6
Return [String].Format(names(indx) ," Recieve")
End If
Else
If key=7 Or key=5 or key=15
Return [String].Format(names(indx) ," Withdrawal")
End If
If key=6
Return [String].Format(names(indx) ," Send")
End If
End If
End If

Return names(indx)

End Function

【问题讨论】:

  • 请在您的问题中添加更多详细信息:代码到底应该做什么?它目前在做什么?您认为代码的哪一部分可能是问题所在?请修正代码的格式。

标签: sql-server vba reporting-services


【解决方案1】:

您为什么不简单地使用SELECT CASE 语句?

Function GetTransactionTypeName(ByVal key As Integer, ByVal isCredit As Boolean) As String

    Select Case key
        Case 1
            Return "Deposit"
        Case 2
            Return "Withdraval"
        ...
        Case 15
            If isCredit Then
                "Tip In"
            Else
                "Tip Out"
            End If
        Case Else
            Return "(Unknown)"
    End Select

End Function

它更容易阅读和维护,而且由于您只有 8 个不同的键值,我敢打赌,完整的代码会比您的原始代码更短。

【讨论】:

  • 但我也有 IsCredit 值,所以我想要一些组合,如 (1,0) 、 (2,0) 、 (3,0) 、 (4,0)、 (5, 0), (5,1), (6,0), (6,1) , (7,0) , (7,1) , (15,0) (15,1)
  • 然后只需在 Case 语句下方添加一个 If 语句。我已经用案例 15 的示例更新了我的答案。
猜你喜欢
  • 2018-12-16
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2013-10-19
  • 1970-01-01
相关资源
最近更新 更多