【问题标题】:try catch statement to turn string into double in vb尝试catch语句在vb中将字符串转换为双精度
【发布时间】:2017-07-10 18:52:27
【问题描述】:

我有以下函数应该检查给定条目是否高于 0.0

Dim inputstr As String = .Item("conc")

Try
    Dim concentration As Double = CDbl(inputstr)
Catch ex As Exception
    Dim concentration As Double = -1.0
Finally
    Dim concentration As Double = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

但是,我不断收到“未声明浓度”。由于其保护级别,它可能无法访问。

有什么想法吗?谢谢

【问题讨论】:

  • 它不在 try catch 范围之外。在外面声明它然后尝试在 try/catch 中设置它
  • ... 你不需要 try-catch。尝试使用double.TryParse
  • 这似乎是XY problem。您要达到的最终目标是什么?
  • 所有导致缩进的东西都会创建一个新级别的(块)范围。

标签: vb.net


【解决方案1】:

变量范围

变量Concentration 仅存在于 Try 块中。因此,无论何时离开此块,该变量都不再存在。

要解决这个问题,您必须在 Try 之前声明 Concentration

终于阻止

你会遇到的另一个问题是Concentration总是-1,因为你在Finally块里这么说,这里不需要这个块。

Dim inputstr As String = .Item("conc")
Dim concentration As Double

Try
    concentration = CDbl(inputstr)
Catch ex As Exception
    concentration = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

阅读一下Variable scope in VB .Net

还有一点关于Try/Catch blocks的阅读

不要使用 Try/Catch 来做到这一点

但是,作为stated by Fabio,您可以使用Double.TryParse() 来执行此操作,它更易于阅读,更重要的是it is a performance increase

所以,最后,最好的编码实践是:

Dim inputstr As String = .Item("conc")
Dim concentration As Double

If Not Double.TryParse(inputstr, concentration) Then
    concentration = -1.0
End If

If concentration > 0.0 Then
    err = 1
End If

【讨论】:

  • 感谢您的澄清。改了。
【解决方案2】:

它不在 try catch 范围之外。在外面声明它然后尝试在 try/catch 中设置它。但问题是它总是以-1.0 结尾,因为Finally

Dim inputstr As String = .Item("conc")
Dim concentration As Double = 0.0
Try
    concentration = CDbl(inputstr)
Catch ex As Exception
    concentration = -1.0
End Try

If concentration > 0.0 Then
    err = 1
End If

最终你可以使用Double.TryParse

Dim inputstr As String = .Item("conc")
Dim concentration As Double = 0.0
If Double.TryParse(inputstr, concentration) AndAlso concentration > 0.0 then
    'Do what you intended
End If

【讨论】:

    【解决方案3】:

    虽然你的主要问题是 concentration 不在范围内
    Finally 将始终执行,无论是否抛出异常 - 所以 concentration 将始终设置为 -1.0
    - 你仍然不需要使用Try .. Catch 方法。 Double.TryParse 将在一行中处理。

    Dim concentration As Double = 0.0
    Double.TryParse(.Item("conc"), concentration)
    
    err = If(concentration > 0.0, 1, 0)
    

    Double.TryParse(inputstr, concentration) - 将concentration 设置为转换后的双精度值或默认值,如果字符串格式不正确,则0.0 用于double 类型。

    【讨论】:

      【解决方案4】:

      用VB.Net怎么样,用Double.TryParse或者Double.Parse

      这会使您的代码类似于

      dim concentration as Double = -1
      'dont really know where this one is coming from though
      err = 0
      
      if Double.TryParse(inputstr, concentration) then
          if concentration > 0.0 then
              err = 1
          end if
      end if
      

      【讨论】:

        【解决方案5】:

        事实上,您的代码有 2 个问题

        一个concentration需要在try之前声明
        第二次删除 finally 否则它将总是为-1.0

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-11
          • 1970-01-01
          相关资源
          最近更新 更多