Class Inheritance

G

Guest

I've been working on a project to help myself better understand how inherited
classes work. I think I've learned more about classes by doing this than any
other effort I've made.

I've tried to create a simple problem by creating a class named TestThis
that inherits System.Windows.Forms.Form and then adding one shared procedure
to this. I set the form to inherit TestThis instead and it gets everything
plus the one procedure that I've given it. So far so good. My problem is
trying to have TestThis dynamically set a form property through inheritance.
It's easy to have my form set this property but for education purposes I'm
doing it the hard way because I want the class to be able to do this. As you
can see below Me causes problems as well as MyBase, MyClass etc. I'm trying
to address Form1 this way without actually calling its name. how do I do
this? Let me show you some code:

Public Class TestThis
Inherits System.Windows.Forms.Form

Public Shared Sub Fade(ByVal Value As Integer)
If Value < 0 Then
Value = 0
ElseIf Value > 100 Then
Value = 100
Else
Value = Value / 100
End If
Me.Opacity = Value ' <- ME causes problems.
End Sub

End Class

' Just create a new form. nothing special. change the inheritance and add
the event handlers.
Public Class Form1
Inherits FadeTest

'+ " Windows Form Designed Generated Code " ' nothing changed here

Private Sub Form1_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
Me.Fade(100)
'Me.Opacity = 1
End Sub

Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate
Me.Fade(50)
'Me.Opacity = 0.5
End Sub
End Class
 
G

Guest

Ok,

I've discovered a few things.

Through trial and error I thought I had to have the derived class procedure
declared as public to be seen. this isn't true apparently. It is friend by
default.
Again as I had tried in the past I wasn't able to get the procedure to show
up in my intellisense list unless I had it shared. I just found out that I
don't have to have it shared. In fact that would have an effect that I
wouldn't want.

Strangely enough now that I took "Public Shared" from the declaration it
seems to work fine. I'm still scratching my head on this one because it
didn't before. Although my code needs to be refined now that it's possible
to debug it during runtime. And I did make a few mistakes ;)
 
B

Branco Medeiros

AWesner wrote:
I've tried to create a simple problem by creating a class named TestThis
that inherits System.Windows.Forms.Form and then adding one shared procedure
to this.
Public Class TestThis
Inherits System.Windows.Forms.Form

Public Shared Sub Fade(ByVal Value As Integer)
Me.Opacity = Value ' <- ME causes problems.
End Sub

End Class
<snip>

Notice that *shared* methods are 'unbound' procedures that just use the
class where they're declared as a namespace. In other words, they're
not associated to a living instance of a class. Thus, there's no 'Me'
inside a shared method, because it's not in the scope of any instance.

Therefore, to access the Opacity of a form from inside the Fade sub,
you must pass the form as parameter:

Public Sub Shared Fade(Target As Form, Value As Integer)
'...
Target.Opacity = Value
End Sub

On the other hand, if you want 'Fade' to be bound to instances of your
class, you must drop the 'Shared' specifier:

Public Sub Fade(ByVal Value As Integer)
'...
Me.Opacity = Value 'No Problem with Me...
End Sub

Regards,

Branco.
 
G

Guest

Noted. Thank you.


I believe my first mistake was trying to call fade within the event handlers
like this

Form1.Fade()

instead of

Me.Fade()

This seemed to fix the issue for me after I realized what I had done.
Although I don't fully understand why Form1 and Me are different when called
within the same class. I had previously thought these were the same
instantiation?
 
J

Jim Wooley

Noted. Thank you.
I believe my first mistake was trying to call fade within the event
handlers like this

Form1.Fade()

instead of

Me.Fade()

This seemed to fix the issue for me after I realized what I had done.
Although I don't fully understand why Form1 and Me are different when
called within the same class. I had previously thought these were the
same instantiation?

In this case, Form1 is the TYPE and Me is the INSTANCE. The Form1 TYPE is
a thing that has a potential for a shape, but doesn't exist yet. Me would
need to exist. The challenge here is that you are trying to call the Me.Fade
method on an instance of Me that doesn't exist yet. Just remove the Shared
instruction from the method call and you should be fine.

As an additional point, you can consider making the Fade method Protected
rather than Friend. A method that is Protected can only be seen by instances
that inherit from the class that contains the declaration. A method with
a scope of Friend can be seen everywhere within your project. Since you may
not need to call Fade outside of your given form instance (the form should
manage itself), protected might be sufficient.

Jim Wooley
http://devauthority.com/blogs/jwooley
 

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