Hi,
How can I achieve that a childs constructor is only callable from it's parent?
Must I declare the class Private within A?
Thanks in Advance
Public MustInherit Class A
Protected Sub New()
End Sub
Public Shared Function CreateInstance( ) As A
Return New B
End Function
End Class
Public Class B : Inherits A
'Should only be callable from the CreateInstance method in A
Public Sub New()
Mybase.New()
End Sub
End Class 10 1249
John, How can I achieve that a childs constructor is only callable from it's
parent?
I normally put A & B into a Class Library (DLL), then make B.New Friend, as
I normally have a "family" of derived classes. In that C, D, E, F, and G
also inherit from A, and A.CreateInstanc e is able to create any one of them,
based on a parameter. The class library represents this family.
Of course other classes within the class library will be able to create B
objects.
Public Class B : Inherits A
'Should only be callable from the CreateInstance method in A Friend Sub New() Mybase.New() End Sub End Class
Must I declare the class Private within A?
Would be the other option, which would ensure that no one could create B
objects! I just noticed that the System.IO.Strea m.Null property is
implemented in terms of a nested class.
Hope this helps
Jay
"John" <ju***********@ yahoo.se> wrote in message
news:5f******** *************** ***@posting.goo gle.com... Hi,
How can I achieve that a childs constructor is only callable from it's
parent? Must I declare the class Private within A?
Thanks in Advance
Public MustInherit Class A
Protected Sub New() End Sub
Public Shared Function CreateInstance( ) As A Return New B End Function
End Class
Public Class B : Inherits A
'Should only be callable from the CreateInstance method in A Public Sub New() Mybase.New() End Sub End Class
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message news:<Oz******* *******@TK2MSFT NGP10.phx.gbl>. .. John, How can I achieve that a childs constructor is only callable from it's parent? I normally put A & B into a Class Library (DLL), then make B.New Friend, as I normally have a "family" of derived classes. In that C, D, E, F, and G also inherit from A, and A.CreateInstanc e is able to create any one of them, based on a parameter. The class library represents this family.
Of course other classes within the class library will be able to create B objects.
Public Class B : Inherits A
'Should only be callable from the CreateInstance method in A Friend Sub New() Mybase.New() End Sub End Class
Must I declare the class Private within A? Would be the other option, which would ensure that no one could create B objects! I just noticed that the System.IO.Strea m.Null property is implemented in terms of a nested class.
Hope this helps Jay
Thanks for the reply Jay. Making b.New friend works but in my case, in
order to avoid misunderstandin gs, it would be better if no other class
in the class library, besides the parent A, could call b.new, c.new
etc. If I declare all children of A within A I would achieve this but
the problem then is the size of the class. Is there any way to declare
B as private within A but supply the implementation in another file?
Best Regards
John
"John" <ju***********@ yahoo.se> schrieb Thanks for the reply Jay. Making b.New friend works but in my case, in order to avoid misunderstandin gs, it would be better if no other class in the class library, besides the parent A, could call b.new, c.new etc. If I declare all children of A within A I would achieve this
How? Neither making the constructor in the derived class private, nor
friend, protected, or public would achieve this.
but the problem then is the size of the class. Is there any way to declare B as private within A but supply the implementation in another file?
The only way (IMO) is
- declare a public Interface within A
- declare the derived classes private within A ("Private Class..."). The
constructor is declared public.
- implement the public Interface in the derived classes
- Have the public function within A return objects implementing the public
Interface ("..As IMyInterface... ...Return New B").
Only a comment to Jay's suggestion: It might not be appliable in all
situations which means it is not a general solution for this design pattern.
Let's have classes A, B and C. Requirements:
- Only A can create B objects
- Only B can create C objects
On one side A and B must be in the same assembly, on the other side B and C
must be in the same assembly. IMO there are other criteria defining which
classes to place in the same library.
--
Armin http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
John, the problem then is the size of the class. Is there any way to declare B as private within A but supply the implementation in another file?
Not in the current version of VB.NET, Within Whidbey (VS.NET 2004, available
late this year) we gain "partial types" that allow you to split a class
between files.
However I think having nested classes spread across multiple files may lead
to more confusion then understanding.
Thanks for the reply Jay. Making b.New friend works but in my case, in order to avoid misunderstandin gs, it would be better if no other class
IMHO this is one of those items that the pain of getting it to do
specifically what you want probably is not worth the gain, hence I simply
put the "family" in a class library, and document the library that A has the
Factory method. The "pain" being either a really large A class due to all
the nesting, or Friend Sub New in derived classes, IMHO Friend Sub New is
the less painful...
Hope this helps
Jay
"John" <ju***********@ yahoo.se> wrote in message
news:5f******** *************** ***@posting.goo gle.com... "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:<Oz******* *******@TK2MSFT NGP10.phx.gbl>. .. John, How can I achieve that a childs constructor is only callable from it's parent? I normally put A & B into a Class Library (DLL), then make B.New Friend,
as I normally have a "family" of derived classes. In that C, D, E, F, and G also inherit from A, and A.CreateInstanc e is able to create any one of
them, based on a parameter. The class library represents this family.
Of course other classes within the class library will be able to create
B objects.
Public Class B : Inherits A
'Should only be callable from the CreateInstance method in A Friend Sub New() Mybase.New() End Sub End Class
Must I declare the class Private within A? Would be the other option, which would ensure that no one could create B objects! I just noticed that the System.IO.Strea m.Null property is implemented in terms of a nested class.
Hope this helps Jay
Thanks for the reply Jay. Making b.New friend works but in my case, in order to avoid misunderstandin gs, it would be better if no other class in the class library, besides the parent A, could call b.new, c.new etc. If I declare all children of A within A I would achieve this but the problem then is the size of the class. Is there any way to declare B as private within A but supply the implementation in another file?
Best Regards John
"Armin Zingler" <az*******@free net.de> schrieb Only a comment to Jay's suggestion: It might not be appliable in all situations which means it is not a general solution for this design pattern. Let's have classes A, B and C. Requirements: - Only A can create B objects - Only B can create C objects On one side A and B must be in the same assembly, on the other side B and C must be in the same assembly.
I should clarify: Due to both requirements it is not possible.
--
Armin
Armin, How? Neither making the constructor in the derived class private, nor friend, protected, or public would achieve this.
Public Class A
Private Class B : Inherits A
Public Sub New()
End Sub
End Class
Public Shared Function CreateInstance( ) As A
Return New B
End Function
End Class
The only way (IMO) is - declare a public Interface within A - declare the derived classes private within A ("Private Class..."). The constructor is declared public. - implement the public Interface in the derived classes - Have the public function within A return objects implementing the public Interface ("..As IMyInterface... ...Return New B").
Are you sure?
On the last item you need a public shared function, because A is an
Interface you cannot have shared functions. John's use of an abstract base
class (MustInherit Class) allows the Shared Function.
You are correct having the nested classes ensure that only A will be able to
create them, however you then run into a "source management issue"
Only a comment to Jay's suggestion: It might not be appliable in all situations which means it is not a general solution for this design
pattern. Let's have classes A, B and C. Requirements: - Only A can create B objects - Only B can create C objects
You're changing the pattern! ;-) The pattern I was describing is 2 levels
(base & derived from base). You are describing N levels (base, derived from
base, derived from derived from base).
I was suggesting that: - Only A can create B objects - Only A can create C objects - Only A can create D objects - Only A can create E objects
Does fit nicely in a single assembly, especially when only that assembly can
create A objects! And referenced assemblies do not care about B, C, D, and
E. In other words A's sub new is only Friend not Protected.
Think of the case where you have an field in a database, and that field has
"behavior" in your code. A is the polymorphic class that represents the
field in the database, B, C, D, & E are the specific "values" that field can
have, each with their own specific behaviors. A.CreateInstanc e accepts the
database field and returns an instance of B, C, D or E. The assembly itself
represents the field as a whole.
Of course I agree, one size does not fit all.
IMHO what John "needs" is the C++ version of the Friend keyword, however
VB.NET does not support it. I don't believe the CLR supports it. Basically
in C++ Class B can state explicitly who is its Friends, then only those
specifically listed classes can use the method in B.
Hope this helps
Jay
"Armin Zingler" <az*******@free net.de> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. .. "John" <ju***********@ yahoo.se> schrieb Thanks for the reply Jay. Making b.New friend works but in my case, in order to avoid misunderstandin gs, it would be better if no other class in the class library, besides the parent A, could call b.new, c.new etc. If I declare all children of A within A I would achieve this How? Neither making the constructor in the derived class private, nor friend, protected, or public would achieve this.
but the problem then is the size of the class. Is there any way to declare B as private within A but supply the implementation in another file?
The only way (IMO) is - declare a public Interface within A - declare the derived classes private within A ("Private Class..."). The constructor is declared public. - implement the public Interface in the derived classes - Have the public function within A return objects implementing the public Interface ("..As IMyInterface... ...Return New B").
Only a comment to Jay's suggestion: It might not be appliable in all situations which means it is not a general solution for this design
pattern. Let's have classes A, B and C. Requirements: - Only A can create B objects - Only B can create C objects On one side A and B must be in the same assembly, on the other side B and
C must be in the same assembly. IMO there are other criteria defining which classes to place in the same library.
-- Armin
http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb Armin,
How? Neither making the constructor in the derived class private, nor friend, protected, or public would achieve this. Public Class A Private Class B : Inherits A
Public Sub New() End Sub
End Class
Public Shared Function CreateInstance( ) As A Return New B End Function
End Class
Nowhere you can access the additional members of B so I don't see when it
makes sense.... Ah, ok, if B does not have additional members. *g* AFAIR I
haven't written a derived class without additional (public) members til now.
Ok, in rare cases this might be required. ;-) The only way (IMO) is - declare a public Interface within A - declare the derived classes private within A ("Private Class..."). The constructor is declared public. - implement the public Interface in the derived classes - Have the public function within A return objects implementing the public Interface ("..As IMyInterface... ...Return New B"). Are you sure?
On the last item you need a public shared function, because A is an Interface you cannot have shared functions. John's use of an abstract base class (MustInherit Class) allows the Shared Function.
No, A is not an interface. A is the class containing the interface
declaration:
class A
public interface IMyInterface
end interface
public shared function func as imyinterface
return new B
end function
private class B
inherits A
implements imyinterface
end class
end class
(interface members left out)
You are correct having the nested classes ensure that only A will be able to create them, however you then run into a "source management issue"
Why? Only a comment to Jay's suggestion: It might not be appliable in all situations which means it is not a general solution for this design pattern. Let's have classes A, B and C. Requirements: - Only A can create B objects - Only B can create C objects You're changing the pattern! ;-) The pattern I was describing is 2 levels (base & derived from base). You are describing N levels (base, derived from base, derived from derived from base).
Right. I only wanted to show the limits of your suggestion ("..not appliable
in *all* situations"). Usually it soon comes to the situation I described,
so your model is not only limited to the "2 levels", it also requires
rearrangement and a different approach if only 1 level will be added.
[...]
Of course I agree, one size does not fit all.
I should have read _everything_ first, so I think we agree. :-)
--
Armin
Armin, Nowhere you can access the additional members of B so I don't see when it makes sense.... Ah, ok, if B does not have additional members. *g* AFAIR
I haven't written a derived class without additional (public) members til
now. Ok, in rare cases this might be required. ;-)
What we didn't show you is that A has overridable methods, and B is (only)
overriding those methods. You are correct having the nested classes ensure that only A will be able to create them, however you then run into a "source management issue"
Why?
I don't like a single source file from getting TOO large, I realize I can
use regions & outlining... I would prefer multiple source files. Also I'm
not so sure "partial classes" is a good idea (outside of designers) however
I have not played with "partial classes" in Whidbey yet. The way I see it is
if a class is getting so big that it needs multiple source files, your class
is probably too big & should be refactored (http://www.refactoring.com). As
I'm sure you have noticed I am a big proponent of OO along with small "light
weight" objects with small "light weight" methods... Basically I try to
"right size" everything ;-)
Hope this helps
Jay
"Armin Zingler" <az*******@free net.de> wrote in message
news:Ok******** ******@TK2MSFTN GP10.phx.gbl... "Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb Armin,
How? Neither making the constructor in the derived class private, nor friend, protected, or public would achieve this. Public Class A Private Class B : Inherits A
Public Sub New() End Sub
End Class
Public Shared Function CreateInstance( ) As A Return New B End Function
End Class
Nowhere you can access the additional members of B so I don't see when it makes sense.... Ah, ok, if B does not have additional members. *g* AFAIR
I haven't written a derived class without additional (public) members til
now. Ok, in rare cases this might be required. ;-)
The only way (IMO) is - declare a public Interface within A - declare the derived classes private within A ("Private Class..."). The constructor is declared public. - implement the public Interface in the derived classes - Have the public function within A return objects implementing the public Interface ("..As IMyInterface... ...Return New B"). Are you sure?
On the last item you need a public shared function, because A is an Interface you cannot have shared functions. John's use of an abstract base class (MustInherit Class) allows the Shared Function.
No, A is not an interface. A is the class containing the interface declaration:
class A public interface IMyInterface end interface
public shared function func as imyinterface return new B end function
private class B inherits A implements imyinterface end class end class
(interface members left out)
You are correct having the nested classes ensure that only A will be able to create them, however you then run into a "source management issue"
Why?
Only a comment to Jay's suggestion: It might not be appliable in all situations which means it is not a general solution for this design pattern. Let's have classes A, B and C. Requirements: - Only A can create B objects - Only B can create C objects You're changing the pattern! ;-) The pattern I was describing is 2 levels (base & derived from base). You are describing N levels (base, derived from base, derived from derived from base).
Right. I only wanted to show the limits of your suggestion ("..not
appliable in *all* situations"). Usually it soon comes to the situation I described, so your model is not only limited to the "2 levels", it also requires rearrangement and a different approach if only 1 level will be added.
[...]
Of course I agree, one size does not fit all.
I should have read _everything_ first, so I think we agree. :-)
-- Armin
"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> schrieb You are correct having the nested classes ensure that only A will be able to create them, however you then run into a "source management issue"
Why?
I don't like a single source file from getting TOO large, I realize I can use regions & outlining... I would prefer multiple source files. Also I'm not so sure "partial classes" is a good idea (outside of designers) however I have not played with "partial classes" in Whidbey yet. The way I see it is if a class is getting so big that it needs multiple source files, your class is probably too big & should be refactored (http://www.refactoring.com). As I'm sure you have noticed I am a big proponent of OO along with small "light weight" objects with small "light weight" methods... Basically I try to "right size" everything ;-)
Ok, I see the point now. Probably my lack of experience because I have never
had to write so many larger nested classes til now. Though, I would prefer a
large class over your modifier approach due to the reasons I have already
given - but I think we both know that often the right decision will turn
out in future. ;-)
--
Armin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Axel Straschil |
last post by:
Hello!
Im working with new (object) classes and normaly call init of ther
motherclass with callin super(...), workes fine.
No, I've got a case with multiple inherance and want to ask if this is
the right and common case to call init:
class Mother(object):
def __init__(self, param_mother): print 'Mother'
|
by: Peter Hamilton |
last post by:
I am trying to implement inheritance but I am having a difficult time with some concepts.
What I am trying to do is have a Child object inherit from the Parent, and when you set a property value at the Parent level, I want all of the children to see this change. Likewise, when I set a Parent's property value in the child object, both the parent and all of the children see the change.
Maybe what I am trying to do is not "inheritance" after...
|
by: Ben Blank |
last post by:
I'm writing a family of classes which all inherit most of their methods and
code (including constructors) from a single base class. When attempting to
instance one of the derived classes using parameters, I get CS1501 (no method
with X arguments). Here's a simplified example which mimics the
circumstances:
namespace InheritError {
// Random base class.
public class A {
protected int i;
|
by: Andrew Ducker |
last post by:
Let's say I have a root class called RootBusinessService
and I then want to have 25 business service classes based off of it.
And each class has a property that's shared between all instances of it
- called 'State'. I therefore want all of them to have a static
property State.
However, I can't declare this static property in RootBusinessService -
because if I do, the subclasses will all point to that single property.
|
by: VR |
last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some
WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages,
my MasterPage does not have a form tag itself so, cannot be called a WebForm
itself, the child pages will implement forms).
I created a Master.aspx page and removed all HTML from it, added some code
to the .aspx.vb file to add controls to my page. Then I created a Child.aspx
and changed the...
| |
by: Mark |
last post by:
why doesn't .NET support multiple inheritance?
I think it's so silly!
Cheers,
Mark
|
by: Julius Fuchs |
last post by:
Hi,
if I create a Person, a Vehicle is created; that's fine.
The problem is that if I create a Child the constuctor of Person is called
before and a Vehicle is created then a Bike is created and the variable is
overwritten. How can I prevent this?
class Person
{
|
by: mmcloughlin |
last post by:
I'm learning about objects and am trying to figure out how basic
inheritance works. I've got into the habit of explicitly setting the
prototype object with an object literal as it seems to make the
creation of a class easier to read/understand.
Anyway it seems to break the inheritance chain in the following code
and I don't know why.
window.onload = function() {
|
by: Ook |
last post by:
The following code compiles and runs. In my overloaded operator<<, I
call Parent.stuff. I would expect it to call Child.stuff, since Child
is the child class, but it does not. What am I missing, and how can I
get it to call Child.stuff. Eventually I'll have different child
classes, and I need it to call stuff() from the associated child
class, not Parent.stuff.
#include <iostream>
#include <ostream>
#include <string>
|
by: johnsonlau |
last post by:
When a class derives from a class,
You can use a pointer to the parent class to delete the instance of
child
only when a virtual destructor declared in the parent.
class Parent
{
virtual ~Parent(); // virtual destructor
}
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |