错误:Option Strict On禁止从“对象”到“十进制”的隐式转换

时间:2013-06-21 20:19:37

标签: vb.net

我想要检查单元格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

1 个答案:

答案 0 :(得分:3)

Range(" J34")生成Object类型的对象引用,而不是Range。你必须施展以保持Option Strict On happy:

Dim sel As Range = CType(Globals.dsbPositionBoard.Range("J34"), Range)
decAnnualMid = Convert.ToDecimal(sel.Value)