【发布时间】:2019-10-01 15:34:22
【问题描述】:
我已经激活了Option Explicit 和Option 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