Click to See Complete Forum and Search --> : OOP problem with vb.
Boumxyz2
September 9th, 2004, 11:10 AM
I'm working on a form that inherits from system.windows.forms.form
and I actually want to Overload the show sub.
In other word.. I want to force programmers that uses my class to call the showdialog and cannot call the show one. I want for example make sure that it doesn't compile when the programmer try to use show. For example :
in C++ if you want to disable the Default constructor you just put the default constructor under private
I would like to do that. But instead, VB resolve to Higher class which is not what I would like to see as correct behavior in an OOP.
is there anyway to do what I want ?
Make sure show for my class is unreachable compilation time.
soumya_bhatta
September 10th, 2004, 08:25 PM
Shadows should give you this effect:
Public Class FormShadow
Inherits System.Windows.Forms.Form
Public Sub New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
Public Shadows Sub Show(ByVal myInt As Integer)
MsgBox("Not from original wondows form, value:" & _
myInt.ToString())
End Sub
Private Sub InitializeComponent()
'
'FormShadow
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "FormShadow"
End Sub
End Class
Now, create another class inheriting fom this class and on a button click on that, notice the commented Show() method call, that will not compile:
Public Class FormShadowInstance
Inherits FormShadow
Friend WithEvents Button2 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(90, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 0
Me.Button2.Text = "Button2"
'
'FormShadowInstance
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button2)
Me.Name = "FormShadowInstance"
Me.Controls.SetChildIndex(Me.Button2, 0)
Me.ResumeLayout(False)
End Sub
Public Sub New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Show(30)
'Show() 'This will not compile
End Sub
End Class
Boumxyz2
September 13th, 2004, 07:39 AM
Actually what you did is a new definition of show with a parameter. Your example works but it's not exactly what I want to do.
What I want to do is this
Private shadows sub Show()
End sub
And this doesn't work. Doesn't force the compiler to not compile if you do myform.show(). It actually resolves to higher class.
Boumxyz2
September 14th, 2004, 10:22 AM
Here's the shadows Definition from MSDN
The Shadows keyword indicates that a declared programming element shadows, or hides, an identically named element, or set of overloaded elements, in a base class. You can shadow any kind of declared element with any other kind.
A shadowed element is normally unavailable from within the derived class that shadows it. However, the following considerations apply:
If the shadowing element is not accessible from the code referring to it, for example if it is Private, the reference is resolved to the shadowed element.
If you shadow an element, you can still access the shadowed element through an object declared with the type of the base class.
So this means I can't DISABLE the show Sub from my class, it will be resolved to the show sub of the form class.
soumya_bhatta
September 14th, 2004, 08:54 PM
Thanks for letting us know.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.