Vb.net 有没有更好的方法来写这个。尝试某事3次或成功

Vb.net 有没有更好的方法来写这个。尝试某事3次或成功,vb.net,Vb.net,基本上我想试3次。如果失败,我再试一次。如果行得通,我就继续 如果在我退出3次后仍失败 For i = 1 To 3 Do Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols") If json = "" Then Exit Do

基本上我想试3次。如果失败,我再试一次。如果行得通,我就继续

如果在我退出3次后仍失败

    For i = 1 To 3
        Do
            Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
            If json = "" Then
                Exit Do
            End If

            jtok1 = JObject.Parse(json)
            If jtok1.Item("success").ToString = "False" Then
                Exit Do
            End If
        Loop While False
        Exit For
    Next
我不想使用goto。我就是这样做的

我能想到的另一种方法是将success作为一个布尔变量,并据此采取行动

   Dim jtok1 = New JObject
    Dim success As Boolean = False

    For i = 1 To 3
        success = True
        Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
        If json = "" Then
            success = False
        End If

        If success Then
            jtok1 = JObject.Parse(json)
            If jtok1.Item("success").ToString = "False" Then
                success = False
            End If
        End If

        If success Then
            Exit For
        End If
    Next

    If success = False Then
        Return
    End If

更多的专业程序员怎么做?

颠倒逻辑。使用
而不是
=
似乎可以得到更简洁、更清晰的代码

Dim success as Boolean = False
For i = 1 To 3
    Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
    ' If we get something proceed to parsing it else start again the loop
    If json <> "" Then ' or better if Not String.IsNullOrEmpty(json) Then
        jtok1 = JObject.Parse(json)
        ' if the parsing is not false, mission accomplished, else re-loop
        If jtok1.Item("success").ToString <> "False" Then
            success = True
            Exit For
        End If
    End If
Next

' here you can test for success or not....
Dim success as Boolean=False
对于i=1到3
Dim json=CookieAwareWebClient.downloadString1(“https://api.bitforex.com/api/v1/market/symbols")
'如果我们得到某个东西,则继续解析它,否则再次启动循环
如果json为“Then”,如果不是String.IsNullOrEmpty(json),则更好
jtok1=JObject.Parse(json)
'如果解析不为false,则任务完成,否则重新循环
如果jtok1.Item(“success”).ToString为“False”,则
成功=正确
退出
如果结束
如果结束
下一个
“在这里,你可以测试成功与否。。。。

颠倒逻辑。使用
而不是
=
似乎可以得到更简洁、更清晰的代码

Dim success as Boolean = False
For i = 1 To 3
    Dim json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
    ' If we get something proceed to parsing it else start again the loop
    If json <> "" Then ' or better if Not String.IsNullOrEmpty(json) Then
        jtok1 = JObject.Parse(json)
        ' if the parsing is not false, mission accomplished, else re-loop
        If jtok1.Item("success").ToString <> "False" Then
            success = True
            Exit For
        End If
    End If
Next

' here you can test for success or not....
Dim success as Boolean=False
对于i=1到3
Dim json=CookieAwareWebClient.downloadString1(“https://api.bitforex.com/api/v1/market/symbols")
'如果我们得到某个东西,则继续解析它,否则再次启动循环
如果json为“Then”,如果不是String.IsNullOrEmpty(json),则更好
jtok1=JObject.Parse(json)
'如果解析不为false,则任务完成,否则重新循环
如果jtok1.Item(“success”).ToString为“False”,则
成功=正确
退出
如果结束
如果结束
下一个
“在这里,你可以测试成功与否。。。。

我发现最好使用函数,而不是围绕每个需要它的代码块编写重试逻辑。验证了重试函数的有效性后,就不必担心该逻辑的正确实现。您编写代码的意图也变得显而易见

''' <summary>
''' Executes a delegate function until success or maxAttempts reached
''' </summary>
''' <param name="codeToRetry">a boolean function delegate containing the code to execute.  Return true if code completed successfully</param>
''' <param name="maxAttempts">maximum number of times <paramref name="codeToRetry"/> is executed </param>
''' <returns></returns>
Public Shared Function RetryCode(codeToRetry As Func(Of Boolean), maxAttempts As Int32) As Boolean
    Dim attempts As Int32 = 0
    Dim ret As Boolean = False
    Do While (Not ret) AndAlso (attempts < maxAttempts)
        attempts += 1
        ret = codeToRetry()
    Loop
    Return ret
End Function
“”
''执行委托函数,直到达到成功或最大尝试次数
''' 
''包含要执行的代码的布尔函数委托。如果代码成功完成,则返回true
''执行的最大次数
''' 
公共共享函数RetryCode(codeToRetry为Func(属于布尔型),maxtures为Int32)为布尔型
当Int32=0时变暗尝试
Dim ret作为布尔值=False
执行While(非ret)和also(尝试次数<最大尝试次数)
尝试次数+=1
ret=codeToRetry()
环
回程网
端函数
对原始代码的重写如下所示。我相信我成功地获得了正确的检索逻辑,但请验证它是否符合您的需要

Sub DemoRetryCode()
    Dim jtok1 As New JObject
    Dim json As String
    Dim success As Boolean = RetryCode(Function()
                                        Dim downloadSuccess As Boolean
                                        json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
                                        If Not String.IsNullOrWhiteSpace(json) Then
                                            jtok1 = JObject.Parse(json)
                                            downloadSuccess= jtok1.Item("success").ToString <> "False"
                                        End If
                                        Return downloadSuccess
                                      End Function, 3)
End Sub
子演示代码()
Dim jtok1作为新作业对象
将json设置为字符串
Dim success作为Boolean=RetryCode(函数()
Dim downloadSuccess作为布尔值
json=CookieAwareWebClient.downloadString1(“https://api.bitforex.com/api/v1/market/symbols")
如果不是String.IsNullOrWhiteSpace(json),那么
jtok1=JObject.Parse(json)
downloadSuccess=jtok1.Item(“success”).ToString“False”
如果结束
返回下载成功
结束功能,3)
端接头

编译器将发出闭包代码来包装局部变量
json
jtok1
以及委托,使其看起来像是向委托函数传递/返回了局部变量。

与其围绕每个需要它的代码块编写重试逻辑,不如使用函数。验证了重试函数的有效性后,就不必担心该逻辑的正确实现。您编写代码的意图也变得显而易见

''' <summary>
''' Executes a delegate function until success or maxAttempts reached
''' </summary>
''' <param name="codeToRetry">a boolean function delegate containing the code to execute.  Return true if code completed successfully</param>
''' <param name="maxAttempts">maximum number of times <paramref name="codeToRetry"/> is executed </param>
''' <returns></returns>
Public Shared Function RetryCode(codeToRetry As Func(Of Boolean), maxAttempts As Int32) As Boolean
    Dim attempts As Int32 = 0
    Dim ret As Boolean = False
    Do While (Not ret) AndAlso (attempts < maxAttempts)
        attempts += 1
        ret = codeToRetry()
    Loop
    Return ret
End Function
“”
''执行委托函数,直到达到成功或最大尝试次数
''' 
''包含要执行的代码的布尔函数委托。如果代码成功完成,则返回true
''执行的最大次数
''' 
公共共享函数RetryCode(codeToRetry为Func(属于布尔型),maxtures为Int32)为布尔型
当Int32=0时变暗尝试
Dim ret作为布尔值=False
执行While(非ret)和also(尝试次数<最大尝试次数)
尝试次数+=1
ret=codeToRetry()
环
回程网
端函数
对原始代码的重写如下所示。我相信我成功地获得了正确的检索逻辑,但请验证它是否符合您的需要

Sub DemoRetryCode()
    Dim jtok1 As New JObject
    Dim json As String
    Dim success As Boolean = RetryCode(Function()
                                        Dim downloadSuccess As Boolean
                                        json = CookieAwareWebClient.downloadString1("https://api.bitforex.com/api/v1/market/symbols")
                                        If Not String.IsNullOrWhiteSpace(json) Then
                                            jtok1 = JObject.Parse(json)
                                            downloadSuccess= jtok1.Item("success").ToString <> "False"
                                        End If
                                        Return downloadSuccess
                                      End Function, 3)
End Sub
子演示代码()
Dim jtok1作为新作业对象
将json设置为字符串
Dim success作为Boolean=RetryCode(函数()
Dim downloadSuccess作为布尔值
json=CookieAwareWebClient.downloadString1(“https://api.bitforex.com/api/v1/market/symbols")
如果不是String.IsNullOrWhiteSpace(json),那么
jtok1=JObject.Parse(json)
downloadSuccess=jtok1.Item(“success”).ToString“False”
如果结束
返回下载成功
结束功能,3)
端接头

编译器将发出闭包代码来包装局部变量
json
jtok1
以及委托,使其看起来像是向委托函数传递/返回了局部变量。

只有在成功为真时才退出。令人惊叹的。是的,请离开。有时,我会用false替换C中的“括号”,只有在成功为真时才退出。阿维索姆