CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    OOP problem with vb.

    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.
    Nicolas Bohemier

  2. #2
    Join Date
    Feb 2002
    Location
    Atlanta, Georgia, USA
    Posts
    155

    Re: OOP problem with vb.

    Shadows should give you this effect:

    Code:
    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:

    Code:
    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

  3. #3
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: OOP problem with vb.

    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.
    Last edited by Boumxyz2; September 14th, 2004 at 10:45 AM.
    Nicolas Bohemier

  4. #4
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: OOP problem with vb.

    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.
    Last edited by Boumxyz2; September 14th, 2004 at 10:45 AM.
    Nicolas Bohemier

  5. #5
    Join Date
    Feb 2002
    Location
    Atlanta, Georgia, USA
    Posts
    155

    Re: OOP problem with vb.

    Thanks for letting us know.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured