473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Class Inheritance

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
Apr 12 '06 #1
5 1077
> Inherits FadeTest

Is really: Inherits TestThis
Oops.

Apr 12 '06 #2
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 ;)

Apr 12 '06 #3

AWesner wrote:
<snip>
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. <snip>
Public Class TestThis
Inherits System.Windows.Forms.Form

Public Shared Sub Fade(ByVal Value As Integer) <snip> 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.

Apr 12 '06 #4
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?
Apr 12 '06 #5
> 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
Apr 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Ricky Romaya | last post by:
Hi, Are there any ways to get multiple inheritace in PHP4? For example, I have 3 parent class, class A, B, and C. I want class X to inherit all those 3 classes. Consider merging those 3 classes...
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
22
by: Marcin Vorbrodt | last post by:
Taken out of C++ In a Nutshell book... Example 7-18: Using an abstract classes as interface specification. struct Runnable { virtual void run() = 0; }; struct Hashable { virtual size_t...
9
by: mead | last post by:
What kind of classes is qualified as "concrete classes"? When should a member function in a class defined as "pure virtual" and when as "virtual"? Thanks!
4
by: KInd | last post by:
Hello All, When is nested class more preferable that Inheritance ? I think with proper inheritance and friend class concept we can get the same flexibility as nested classes Any comments .. Best...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
11
by: Darren.Ratcliffe | last post by:
Hi guys Posted what was probably a rather confusing post about this the other day, so I am going to have another go and see if I can make more sense. This sis purely a I've got a base class...
3
by: hurcan solter | last post by:
Consider the code fragment; class A { public: A(){} A(int prm):mprm(prm){} int mprm; }; class B:public A {
3
by: Jam | last post by:
Hello All, Your comment is needed on following subject,i define the Class and Inheritance as following,what do you think? Class: Class is a set of objects which shares common states and...
3
by: Ravi | last post by:
Is this the correct way to think of "base class"? The "base class" is a class from which other classes are derived. The "base class" will never be derived from another class.
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.