473,796 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheritance and child creation

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
Nov 20 '05
10 1250
Armin,
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. ;-) Actually that is one of the cool things about Refactoring!

You can start out with one design, then change to the another design later,
in a controlled and "safe" manner.

I just wish there were more automated tools for Refactoring in VB.NET! Or
possible even a Refactoring book geared toward VB.NET similar to James
Cooper's VB6 & VB.NET book on Design Patterns.

Hope this helps
Jay

"Armin Zingler" <az*******@free net.de> wrote in message
news:Og******** ******@TK2MSFTN GP10.phx.gbl... "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

Nov 20 '05 #11

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

Similar topics

14
6410
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'
4
1645
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...
45
6368
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;
6
1863
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.
6
2102
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...
47
3653
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
3
1595
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 {
6
1488
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() {
7
2180
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>
1
14237
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 }
0
10452
Oralloy
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...
0
10003
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 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...
0
9050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 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...
0
6785
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();...
0
5440
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...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
bsmnconsultancy
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...

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.