MyBase.New Question on Inherited UserControls

B

BluDog

Hi

I have a UserControl called MyUserControl that has the following
constructor and InitializeComponent (automatically generated by VS):

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub

I then inherit from the control with MyInheritedUserControl and add a
text box to the control, again the constructor and InitializeComponent
is automatically generated by the VS:

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(16, 32)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = "TextBox1"
'
'MyInheritedControl
'
Me.Controls.Add(Me.TextBox1)
Me.Name = "MyInheritedControl"
Me.ResumeLayout(False)

End Sub

My company is implementing FxCop
(http://www.gotdotnet.com/team/fxcop/), a fantastic utility, but it
flags the following warning on the constructor in
MyInheritedUserControl:

"'MyInheritedControl..ctor()' contains a call chain that results in a
call to a virtual method defined by the class. Review the following
call stack for unintended consequences:
MyInheritedControl..ctor()MyInheritedControl.
InitializeComponent()MyInheritedControl.set_TextBox1(
TextBox WithEventsValue)"

"Virtual methods defined on the class should not be called from
constructors. If a derived class has overridden the method, the
derived class version will be called (before the derived class
constructor is called)."

With a link to more information:

http://www.gotdotnet.com/team/fxcop/docs/rules/UsageRules/CtorVirtual.html

Can someone explain what is actually wrong here please?

Thanks

Blu.
 
B

BluDog

I found the description + sample really self-explaining. So I suggest
to take a closer look at it and play around with the sample provided in
the document referenced above.

I agree that it makes sense, but why does the code that VS adds
automatically break their own rules?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top