If I declare a class within another class like:
Class ParentClass
...
Class ChildClass
...
End Class
End Class
How do I reference a property in the ParentClass in the ChildClass? If the
ChildClass is inherited from another class (like CollectionBase), do that
affect the reference?
Also, when an instance of ParentClass is created, is an instance of
ChildClass also created?
Thanks,
Craig Buchanan 2 7387
Craig, How do I reference a property in the ParentClass in the ChildClass?
You would need to give ChildClass a field of type ParentClass. When I need
this I normally define the parent to be part of the constructor.
Class ParentClass ... Class ChildClass ...
Private Readonly m_parent As ParentClass
Public Sub New(ByVal parent As ParentClass)
m_parent = parent
End Sub
End Class End Class
If the ChildClass is inherited from another class (like CollectionBase), do that affect the reference?
No
Also, when an instance of ParentClass is created, is an instance of ChildClass also created?
No, you need to explicitly create any ChildClass instances you need. If you
need a ChildClass created when you create a ParentClass I would do so in the
ParentClass constructor. You can create any number of ParentClass instances
for any number of ChildClass instances, including zero in both cases. In
other words you can have a ChildClass without a ParentClass, and you can
have a ParentClass without a ChildClass.
Class ParentClass ...
' continuing the above sample
Private ReadOnly m_child As ChildClass
Public Sub New()
m_child = New ChildClass(me)
End Sub
End Class
In the above sample, each parent has a reference to its lone child, that
child has a reference to its parent. Once all other references to both the
parent and child are gone, both objects will be garbage collected. In other
words you can the can references to each other and the GC will still remove
them from memory when no one else refers to them...
Hope this helps
Jay
"Craig Buchanan" <so*****@somewhere.com> wrote in message
news:uT**************@TK2MSFTNGP11.phx.gbl... If I declare a class within another class like:
Class ParentClass ... Class ChildClass ... End Class End Class
How do I reference a property in the ParentClass in the ChildClass? If
the ChildClass is inherited from another class (like CollectionBase), do that affect the reference?
Also, when an instance of ParentClass is created, is an instance of ChildClass also created?
Thanks,
Craig Buchanan
Hi Craig,
In addition to what Jay has explained.
At the very least, nested classes give you a way to distinguish between
classes with the same name.
Class CarFactorySupplier
Class Widget
End Class
End Class
Class PlumbingBitsSupplier
Class Widget
End Class
End Class
In this sense the enclosing classes act in the same way as Namespaces to
provide a means of differentiating between the two (or more) sorts of Widgets.
As Jay, says, it's not necessary for the Parent class to actually use the
Child class within itself. The child would be nested to give it context, but
exist solely to provide additional services to users of the Parent class.
Regards,
Fergus This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Neil Zanella |
last post by:
Hello,
Suppose I have some method:
Foo::foo() {
static int x;
int y;
/* ... */
}
|
by: DanielBradley |
last post by:
Hello all,
I have recently been porting code from Linux to cygwin and came across
a problem with static const class members (discussed below). I am
seeking to determine whether I am programming...
|
by: hamil |
last post by:
I have a form with one button, Button1, and a Textbox, Textbox1
I have a class, class1 as follows.
Public Class Class1
Public DeForm As Object
Sub doit()
DeForm.Textbox1.text = "It works"...
|
by: Brett |
last post by:
In a class, I have several Private subs. I declare an instance of the class
such as:
Dim MySelf as new Class1
within a private sub. The motive is to provide access to other subs within
the...
|
by: Gman |
last post by:
Hi,
I have created a usercontrol, a grid control essentially. Within it I
have a class: clsGridRecord. I have coded the events such that when a
user clicks on the grid, say, the events occur on...
|
by: WXS |
last post by:
Vote for this idea if you like it here:
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9...
|
by: roland.bali |
last post by:
Hi,
Here is the basic setup, my base class is Shoe which has a child class
called Sandal. I would like to create objects by calling Sandal.Load.
But without overloading Load in Sandal and...
|
by: andy |
last post by:
A question about about passing a class by reference:
Say you have a class called car, and within that you have two objects
called car01 and car02.
Within the class I have an int variable...
|
by: Mayur H Chauhan |
last post by:
All,
For my knowledge, if I declare Class as follow, then it thows
compilation error.
Protected Class Book
End Class
Even same for...
|
by: hurricane_number_one |
last post by:
I'm trying to have a class, which uses threads be able to raise events
to the form that created it. I've seen solutions around the net for
this, but I didn't really like their implementation. ...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |