Asp classic 使用函数设置变量';s结果

Asp classic 使用函数设置变量';s结果,asp-classic,vbscript,Asp Classic,Vbscript,我使用 Call GameTimer(FormatDate(objLiveCommentary("DateFirstStarted"), "WithTime"), FormatDate(objLiveCommentary("DateSecondStarted"), "WithTime"), "Soccer") 它将结果打印为23,35,64,90。我想将此结果存储为 CurrentGameTime = 因为我会将Current

我使用

Call GameTimer(FormatDate(objLiveCommentary("DateFirstStarted"), "WithTime"), 
               FormatDate(objLiveCommentary("DateSecondStarted"), "WithTime"), 
               "Soccer")
它将结果打印为23,35,64,90。我想将此结果存储为

CurrentGameTime = 
因为我会将CurrentGameTime保存到我的数据库中。我怎么做

Function GameTimer (FirstStarted, SecondStarted, GameType)

If GameType =   "Soccer"    Then
    DateFirstStarted    = DateDiff("n", FirstStarted, FormatDate(NOW(), "WithTime"))
    DateSecondStarted   = DateDiff("n", SecondStarted, FormatDate(NOW(), "WithTime"))

    If DateFirstStarted <= 45 Then
    Response.Write DateFirstStarted
    ElseIf DateFirstStarted <= 45 Then
    DateFirstStarted
    ElseIf DateSecondStarted <= 45 Then
    Response.Write DateSecondStarted + 45
    ElseIf DateFirstStarted <= 45 Then
    DateFirstStarted
    Else 
    Response.Write "90"
    End If  
End If

End Function
功能游戏计时器(首次启动、第二次启动、游戏类型)
如果GameType=“Soccer”,则
DateFirstStart=DateDiff(“n”,FirstStart,FormatDate(现在(),“WithTime”))
DateSecondStarted=DateDiff(“n”,SecondStarted,FormatDate(现在(),“WithTime”))

如果DateFirstStart要返回函数结果,需要设置函数返回值:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    ...
    GameTimer = 90
End Function
您的GameTimer函数应该如下所示:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    Dim result
    result = 90

    If GameType = "Soccer" Then
        DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
        DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))

        If DateFirstStarted <= 45 Then
            result = DateFirstStarted
        End If

        If DateSecondStarted <= 45 Then
            result = DateSecondStarted + 45
        End If
    End If

    GameTimer = result
End Function
下一步是用数组替换FirstStarted和SecondStarted参数,以允许游戏使用三分之一和四分之一


时段开始时间数组

Function GameTimer(Periods, PeriodInMinutes)
    Dim result
    Dim currenttime
    Dim i

    result = 0 '-- preset to zero --'

    For i = 1 To (UBound(Periods))
        currenttime = DateDiff("n", Periods(i), FormatDateTime(Now(), 0))

        If currenttime > 0 Then
            If (currenttime <= PeriodInMinutes * i) Then
                result = currenttime + (PeriodInMinutes * (i - 1))
            Else
                result = PeriodInMinutes * i
            End If
        End If
    Next

    GameTimer = result
End Function
编辑


重构以更正在两个时段之间返回完整时间的缺陷。

要返回函数结果,需要设置函数返回值:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    ...
    GameTimer = 90
End Function
您的GameTimer函数应该如下所示:

Function GameTimer(FirstStarted, SecondStarted, GameType)
    Dim result
    result = 90

    If GameType = "Soccer" Then
        DateFirstStarted = DateDiff("n", FirstStarted, FormatDateTime(Now(), 0))
        DateSecondStarted = DateDiff("n", SecondStarted, FormatDateTime(Now(), 0))

        If DateFirstStarted <= 45 Then
            result = DateFirstStarted
        End If

        If DateSecondStarted <= 45 Then
            result = DateSecondStarted + 45
        End If
    End If

    GameTimer = result
End Function
下一步是用数组替换FirstStarted和SecondStarted参数,以允许游戏使用三分之一和四分之一


时段开始时间数组

Function GameTimer(Periods, PeriodInMinutes)
    Dim result
    Dim currenttime
    Dim i

    result = 0 '-- preset to zero --'

    For i = 1 To (UBound(Periods))
        currenttime = DateDiff("n", Periods(i), FormatDateTime(Now(), 0))

        If currenttime > 0 Then
            If (currenttime <= PeriodInMinutes * i) Then
                result = currenttime + (PeriodInMinutes * (i - 1))
            Else
                result = PeriodInMinutes * i
            End If
        End If
    Next

    GameTimer = result
End Function
编辑


重构以纠正在两个时段之间返回完整时间的缺陷。

这很有意义。你能再解释一下用数组替换FirstStarted和SecondStarted吗?还有,CurrentGameTime=result或CurrentGameTime=GameTimer?这是brilliant Filburt。我喜欢你计划和组织的方式。最后一个问题。如果我没有弄错的话,它会在两个时段之间打印“result=PeriodInMinutes*UBound(Periods)”--预设为最大游戏时间--”。因此,在曲棍球比赛的第一阶段和第二阶段之间,函数将打印60。你能想出一个办法来修复这个缺陷吗?@zurna:你说得对,这是一个bug。我通过重新组织Ifs并区分周期时间和跨周期时间来解决这个问题。这很有意义。你能再解释一下用数组替换FirstStarted和SecondStarted吗?还有,CurrentGameTime=result或CurrentGameTime=GameTimer?这是brilliant Filburt。我喜欢你计划和组织的方式。最后一个问题。如果我没有弄错的话,它会在两个时段之间打印“result=PeriodInMinutes*UBound(Periods)”--预设为最大游戏时间--”。因此,在曲棍球比赛的第一阶段和第二阶段之间,函数将打印60。你能想出一个办法来修复这个缺陷吗?@zurna:你说得对,这是一个bug。我确实通过重新组织Ifs并区分时段时间和时段间时间来修复此问题。我添加了用法并进行了重构,以使用时段开始时间数组。更正了在时段之间返回完整时间的错误。我很荣幸收到您对我的解决方案的评论。如果你还没有听说;Robert C.Martin(Bob叔叔)的“Clean Code”给了我很多关于如何重构和改进代码的见解。我添加了用法并进行了重构,以使用一个周期开始时间数组。更正了周期之间完全返回的错误。您对我的解决方案的评论使我感到荣幸。如果你还没有听说;罗伯特·C·马丁(鲍勃叔叔)的《干净的代码》给了我很多关于如何重构和改进代码的见解。