Click here to Skip to main content
15,312,400 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I currently am creating a new class and i am struggling to get a subclass hidden and protected from being directly initialised.

When i use
Private TheClass as MyClass1

i can also select its subclass (clsClientInfo) - i want to be able to remove the clsClientInfo and only have it part of the base class, can anyone help me and poit me in the right direction please.

i have read loads on inherit, protect, mybase etc but cannot seem to get my head round this bit in particular.

Public Class MyClass1
    'Gain Properties from clsClientInfo
    Public CustomerInfo As clsClientInfo

    'SUB CLASS: clsClientInfo'

    Class clsClientInfo
        Private m_AC_ACCOUNT_NUMBER As Long
        Private m_AC_ACCOUNT_NAME As String
        Private m_AC_ACCOUNT_DOB As Date
        Private m_AC_ACCOUNT_EMAIL As Date
        Public Property AccountNum() As Long
                Return m_AC_ACCOUNT_NUMBER
            End Get
            Set(value As Long)
                m_AC_ACCOUNT_NUMBER = value
            End Set
        End Property
        Public Property FullName() As String
                Return m_AC_ACCOUNT_NAME
            End Get
            Set(value As String)
                m_AC_ACCOUNT_NAME = value
            End Set
        End Property
        Public Property DateOfBirth() As Date
                Return m_AC_ACCOUNT_DOB
            End Get
            Set(value As Date)
                m_AC_ACCOUNT_DOB = value
            End Set
        End Property
        Public Property EmailAddress() As Date
                Return m_AC_ACCOUNT_DOB
            End Get
            Set(value As Date)
                m_AC_ACCOUNT_DOB = value
            End Set
        End Property
    End Class
End Class

Really Appreciate it,

many Thanks
Richard MacCutchan 16-Oct-14 12:33pm
Why are you using the subclass like this, what purpose do you expect it to serve?
Dev O'Connor 16-Oct-14 12:43pm
i am trying to do the following:

the class i have will have over 100 properties and i am trying to group them into subgroups by way of creating a subclass.

So the main class Creates a public var as the subclass which contains the properties.

this is a large development project and need to find a way to catagorise my properties into manageble groups.
Dev O'Connor 16-Oct-14 12:44pm
But again as it stands, when i go to initiate the main class and following it with a . my subclasses show up and i dont want them to be able to instantiate them directly i only want them visible from the main class.
Richard MacCutchan 16-Oct-14 12:50pm
You should make the class and the object of that call private. In your example you have made CustomerInfo Public.
Dev O'Connor 16-Oct-14 12:54pm
I want the Var 'CustomerInfo' to be available, what i dont want it to allow s to instantiate the subclass directly, such as:

Dim MyCls as MyClass1.clsClientInfo

i only want them to be able to use
Dim MyCls as MyClass1

Then they can use for example
MyCls.CustomerInfo.AccountNum = 123456789
Dev O'Connor 16-Oct-14 12:55pm
Just to add when i make the var Private i can then no longer access:
Richard MacCutchan 16-Oct-14 12:59pm
And nor can anyone else, which is what you imply you want. However, it may be better to follow Sergey's advice and use a public class with a private default constructor.
Dev O'Connor 16-Oct-14 13:03pm
I want the class to be available from the Main Class
As in MyCls.CustomerInfo - i dont want them to call the sub class directly as i did above.

I have asked sergey as i do not know how to create a private default constructor, i have researched this but just simply dont understand it. Would you be able to show me by way of copying my code above and adapating it to have a private constructor please as i am losing the plot here.

Many Thanks
Richard MacCutchan 16-Oct-14 13:24pm
I'm not sure how to do this in VB. I don't see a question posted to Sergey's answer, you need to use the "Have a Question or Comment" link below his solution entry.
Dev O'Connor 16-Oct-14 17:15pm
I clicked on it but it posted it as a solution

is it something along the lines of:

Private Sub New()
End Sub

1 solution

Any class, not a "sub-class". There are two aspects of this.

First of all, this class can be abstract, which cannot be instantiated by definition.

But there are cases when you want to have instances of the class, but protect the user of the class from direct instantiation of the class through initialization of a variable or a member by constructing an instance of this class. This is done by creating a class without accessible constructors. How? The only tricky thing is to eliminate default constructor. For this purpose, define private constructor without parameters. It doesn't need any significant body/implementation, it can be empty. If you do so, any initialization statement of such class will cause compilation error.

The purpose of these techniques is to use this class as a base class for other classes which can be instantiated. In second technique, this is the way to allow creation of the instances of the class only in some special way (using factory methods), not directly. The variation of this technique would be using different access modifiers, for example, to allow creation of an instance only for derived class, then protected access modifier should be used; and so on:[^].

Dev O'Connor 16-Oct-14 17:39pm
Hi Sergey

I have tried repeated times to get this going to no avail i have tried Private Sub New() with no code and several other procedures

please could you use the code above and demonstrate an example so i can work from it as i do not understand the logic, you cant make a class private as then its not visible outside the class and its driving me nuts at the minutes.

Really appreciate any help you can give.
Sergey Alexandrovich Kryukov 16-Oct-14 18:36pm
Which way, private constructor or abstract class? What do you want to achieve?
Sergey Alexandrovich Kryukov 16-Oct-14 18:42pm
The sample of the private constructor technique is the first code sample here.
Here you will see the code sample for an abstract class: abstract classes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900