goomba454 goomba454 - 1 year ago 45 Question

Property in object not updating

When I use the following code to create my object, the values I assign in the with portion does no reflect in my class, and I am trying to use that values in the Sub New() portion which is in the 2nd snippet. How can I make that work?

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}

2nd Snippet

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

Answer Source

As suggested by @GlorinOakenfoot, code like this:

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

Is just a shorthand for this:

Dim obj As New SomeType

obj.SomeProperty = someValue

It should be obvious why you can't use someValue in the constructor. If you want to use a value in the constructor then, like for any other method, you must pas that value via a parameter.