对象中的属性未更新

时间:2016-07-09 04:02:02

标签: vb.net oop

当我使用以下代码创建我的对象时,我在with部分中指定的值没有反映在我的类中,我试图在第二个片段中的Sub New()部分中使用这些值。我怎样才能做到这一点?

Dim RoughPnl As New RoughPnl(trvPartList, pnlBasePanel, chkRoughUnderFinal.Checked) With
                 {.Material = cboMaterial.Text, .Label = cboLabel.Text, .Qty = cboQty.Text,
                  .BoardThickness = cboThickness.Text,
                  .BoardWidth = cboWidth.Text,
                  .BoardLength = cboLength.Text,
                  .BoardColor = picFinalColor.Tag,
                  .BoardImage = picFinalColor.Image,
                  .DimLinesON = chkFinalDimLines.Checked,
                  .LabelsON = chkLabelsON.Checked,
                  .SizeON = chkSizeON.Checked}

第二个代码段

Public NotInheritable Class RoughPnl
    Inherits MyPanel

    Sub New(objTree As TreeView, basePnl As Panel, rpnl As boolean)
        MyBase.New(objTree, basePnl)

        'Initial values
        m_myKey = myKey
        m_refNum = refNum
        m_material = material
        m_label = label
        m_qty = qty
        m_pnlID = "Rough"
        Me.Name = m_pnlID & "key" & m_material & "-" & m_myKey
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

根据@GlorinOakenfoot的建议,代码如下:

Dim obj As New SomeType With {.SomeProperty = someValue}

只是一个简写:

Dim obj As New SomeType

obj.SomeProperty = someValue

很明显,为什么你不能在构造函数中使用someValue。如果你想在构造函数中使用一个值,那么就像任何其他方法一样,你必须通过参数传递该值。