By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,484 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

MyBase.New Question on Inherited UserControls

P: n/a
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_TextBo x1(
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/...orVirtual.html

Can someone explain what is actually wrong here please?

Thanks

Blu.
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* BluDog <ne**@nospam.bludog.net> scripsit:
With a link to more information:

http://www.gotdotnet.com/team/fxcop/...orVirtual.html

Can someone explain what is actually wrong here please?


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #2

P: n/a
On 18 Aug 2004 12:40:55 +0200, hi***************@gmx.at (Herfried K.
Wagner [MVP]) wrote:
* BluDog <ne**@nospam.bludog.net> scripsit:
With a link to more information:

http://www.gotdotnet.com/team/fxcop/...orVirtual.html

Can someone explain what is actually wrong here please?


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?
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.