[英]VB.NET error message - “Option Strict On disallows implicit conversions from 'Object' to 'String'”
[英]Error: Option Strict On disallows implicit conversions from 'Object' to 'Decimal'
我想檢查單元格J34(excel)的值。 如果該值為null,則顯示消息,如果未運行,則運行其余代碼。
但是,當我編譯時,我在上面得到錯誤:
decAnnualMid = Convert.ToDecimal(Globals.dsbPositionBoard.Range(“ J34”)。Value)
這是我的其余代碼
Option Strict On
Option Explicit On
Private Sub chk10thPercentile_CheckedChanged(sender As Object, e As EventArgs) Handles chk10thPercentile.CheckedChanged
'This event runs when the 10th checkbox is checked
'The event clears the cells where the value will be pasted
'enters the value from cell J34 into calculationShet H45 and clears
'the checkboxes for 25th, 50th, 75th, 90th and Other Amount
'checkboxes.
Dim decAnnualMid As Decimal
If chk25thPercentile.Checked = True And Globals.dsbPositionBoard.Range("J34").Value Is DBNull.Value Then
'Display is user has not selected a position in the dashboard.
MsgBox("The Market Data section of the Position Dashboard does not have a value for the 10th Percentile.", MsgBoxStyle.Critical, "Input Error")
Me.Close()
Else
'convert the value of K34 to decimal and
decAnnualMid = Convert.ToDecimal(Globals.dsbPositionBoard.Range("J34").Value)
'convert annual salary to hourly
decHourlyMid = decAnnualMid / 52 / 40
'display in the Mid label box
lblAnnualMid.Text = decAnnualMid.ToString("C")
lblHourlyMid.Text = decHourlyMid.ToString("C")
'Uncheck all other boxes
chk25thPercentile.Checked = False
chk50thPercentile.Checked = False
chk75thPercentile.Checked = False
chk90thPercentile.Checked = False
chkOtherAmount.Checked = False
End If
End Sub
Range(“ J34”)生成對象類型而不是范圍的對象引用。 您必須進行強制轉換以使Option Strict On保持滿意:
Dim sel As Range = CType(Globals.dsbPositionBoard.Range("J34"), Range)
decAnnualMid = Convert.ToDecimal(sel.Value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.