【问题标题】:Option Strict ON made some errorsOption Strict ON 犯了一些错误
【发布时间】:2020-11-09 17:18:12
【问题描述】:

我必须启用选项脚本,才能正确使用 OpenXML 包。完成后,我遇到了几个关于从一种类型到另一种类型的隐式转换的错误。

Label6.Text = FormatNumber(CInt(Form3.Label12.Text), "0.00") 'O.S. On disallows implicit conversions from String to Integer'

hrrspk.Add(Form3.ListBox2.Items(i)) 'O.S. On disallows implicit conversions from Double to Integer'

tsdom.Add(((hrrspk(ts) - CInt(Label6.Text)) * (CInt(Label3.Text) - CInt(Label4.Text)) / (CInt(Label6.Text))) + CInt(Label4.Text)) 'O.S. On disallows implicit conversions from Double to Integer'

hrrnativexcel = CreateObject("EXCEL.APPLICATION") 'O.S. On disallows implicit conversions from Object to Application'

hrrnativexcel.Cells(1, 1).value = "Time [s]" 'O.S. On disallows late binding'

For Each o As String In Form3.ListBox1.Items.Cast(Of Object).Zip(Form3.ListBox2.Items.Cast(Of Object), Function(x1, x2) x1 & "," & x2) 'O.S. On prohibits operands of type Object for operator &.'

如何更改此行以解决此错误?谢谢大家会回答我。

【问题讨论】:

标签: vb.net option-strict


【解决方案1】:

变化:

Label6.Text = FormatNumber(CInt(Form3.Label12.Text), "0.00") - O.S

收件人:

Label6.Text = CDbl(Form3.Label12.Text).ToString("0.00")

对于 Double to Integer 错误。

变化:

hrrspk.Add(Form3.ListBox2.Items(i))

收件人:

hrrspk.Add(CInt(Form3.ListBox2.Items(i)))

变化:

If Me.TextBox1.Text.Split(".")(1).Length < 3 Then

收件人:

If Me.TextBox1.Text.Split(".".ToCharArray())(1).Length < 3 Then

对于 Object 和 & 一,更改:

Function(x1, x2) x1 & "," & x2

收件人:

Function(x1, x2) CStr(x1) & "," & CStr(x2) ' or use x1.ToString() and x2.ToString()

【讨论】:

  • 他只是评论“O S”,意思是“选项严格”。它实际上不是我认为的代码的一部分。
  • @AndrewMortimer 哈哈!你可能是对的... =)
  • 是的,@AndrewMortimer 就是这么写的。抱歉写错了,现在我编辑了帖子。
  • 我正在纠正我能够纠正的行。您能帮我纠正一下从 String 到 Char 以及从 Double 到 Integer 转换的错误吗?
  • 这是字符串到字符错误的行:'If Me.TextBox1.Text.Split(".")(1).Length
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
相关资源
最近更新 更多