【问题标题】:Option strict on disallows implicit conversion from string to double and double to string选项严格禁止从字符串到双精度和双精度到字符串的隐式转换
【发布时间】:2019-10-01 15:34:22
【问题描述】:

我已经激活了Option ExplicitOption Strict,在我看来我遇到了一些错误:

Option Strict On 不允许从 'String' 到 'Double' 的隐式转换

Option Strict On 不允许从 'Double' 到 'String' 的隐式转换。

为什么会出现这些错误?

如果我删除Option Strict,我就没有更多错误了。有些错误可能无法修复,我该怎么办?如果出现这些错误,为什么要使用Option Strict

TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1
TxtCheckList.Text = TxtBoxIntDrawsCount.Text - 1

代码:

Private Sub BttImport_Click(sender As Object, e As EventArgs) Handles BttImport.Click
    TxtBoxIntDraws.Clear()
    TxtBoxIntDraws.Text = System.IO.File.ReadAllText(My.Application.Info.DirectoryPath + ("\itemInfo.txt"))
    Dim sr As New IO.StreamReader(My.Application.Info.DirectoryPath + ("\itemInfo.txt"))
    Dim strLines() As String = Strings.Split(sr.ReadToEnd, Environment.NewLine)
    TxtBoxIntDrawsCount.Text = strLines.Length
    sr.Close()
    TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1
    TxtCheckList.Text = TxtBoxIntDrawsCount.Text - 1
    TxtBoxIntDraws.Text = String.Join(Environment.NewLine, TxtBoxIntDraws.Lines.Select(Function(l) String.Join(",", l.Split(",").Select(Function(s) Integer.Parse(s)))))
End Sub

【问题讨论】:

  • 您应该先将 TxtBoxIntDrawsCount.Text 解析为一个整数,然后再将其取为 1。因为如果将文本放入这个文本框,就会导致错误
  • 如果您将鼠标悬停在问题行上,它会提供纠正建议吗?
  • 您需要进行一些错误检查,并且如上面@Cal-cium 所述,从字符串转换为整数。查看Convert.ToInt32() 进行转换。但是您应该首先查看TxtBoxIntDrawsCount.Text 中的文本字符串或使用Try Catch 块来确保该字符串仅包含适合您所需范围的数字。
  • ...如TxtCheckDraws.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()
  • @daShier 更糟糕的是:TxtBoxIntDrawsCount.Text = strLines.Length 只排了两行。

标签: vb.net


【解决方案1】:

查看各个组件

TxtCheckDraws.Text = TxtBoxIntDrawsCount.Text - 1

TxtBoxIntDrawsCount 是一个文本框。 Text 属性是一个字符串。您尝试从字符串中减去 1。这个不对。首先,将文本转换为整数。以下将产生一个整数。

Integer.Parse(TxtBoxIntDrawsCount.Text) - 1

我们有另一个 TextBox TxtCheckDraws 和另一个 Text 属性,它也是一个字符串。我们不能将整数 Integer.Parse(TxtBoxIntDrawsCount.Text) - 1 分配给字符串。所以我们应该先把它转回字符串。

TxtCheckDraws.Text = (Integer.Parse(TxtBoxIntDrawsCount.Text) - 1).ToString()

只要TxtBoxIntDrawsCount 确实有一个整数,它就可以工作。例如,如果它有字符串“four”而不是字符串“4”,那么您将遇到问题。您将需要添加一些验证。有很多方法可以做到这一点,它们超出了问题的范围。

【讨论】:

  • 还请查看@OliverJacot-Descombes 的回答,因为他提供了一种添加我提到的验证的好方法。
【解决方案2】:

Option Explicit On 强制你声明所有变量。 Option Strict On 强制您明确指定转换。根据Option Strict Statement

当文件中出现 Option Strict On 或 Option Strict 时,以下情况会导致编译时错误:

  • 隐式缩小转换
  • 后期绑定
  • 导致 Object 类型的隐式类型

优点是,它生成的代码明确说明了程序员的想法。您仍然可以执行隐式扩展转换。例如从IntegerDouble 的隐式转换仍然有效。

将代码更改为

TxtCheckDraws.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()
TxtCheckList.Text = (CInt(TxtBoxIntDrawsCount.Text) - 1).ToString()

这表明您想将文本框的内容解释为Integer,然后想从中减去1,最后想将结果转换回String

使用Integer.TryParse 也是一个好主意,以防用户输入无效号码

Dim number As Integer

If Integer.TryParse(TxtBoxIntDrawsCount.Text, number) Then
    TxtCheckDraws.Text = (number - 1).ToString()
Else
    ' Tell the user to enter a correct number
End If

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 2018-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-20
    • 2011-05-11
    • 1970-01-01
    相关资源
    最近更新 更多