Hiding Default Constructor in VB Express 2005 (Public Not Creatable in VB6)

  • Thread starter Thread starter Holger Boskugel
  • Start date Start date
H

Holger Boskugel

Hello,

I'm looking for a solution to hide the default constructor of a class.
In VB6 there was a class type of "Public Not Creatable". I have
tried to write the default ctor as friend or private but the compiler
adds a default ctor. Actual I use the way to set the default ctor
as protected and inside I raise an error if this ctor will be called
from an inheriting class.


Regards

Holger
 
Can't you just mark the class as mustinherit?

i.e.

Public MustInherit MyClass()
....
End Class

Thanks,

Seth Rowe
 
I'm looking for a solution to hide the default constructor of a class.
In VB6 there was a class type of "Public Not Creatable". I have
tried to write the default ctor as friend or private but the compiler
adds a default ctor. Actual I use the way to set the default ctor
as protected and inside I raise an error if this ctor will be called
from an inheriting class.



Private Sub New()
End Sub
 
Hello Seth,
Can't you just mark the class as mustinherit?

i.e.

Public MustInherit MyClass()
...
End Class

no, I can't in case that I need the class as a Public and will be instanced
through functions in my class library


Regards

Holger
 
Hello Chris,
Private Sub New()
End Sub

This was what I checked out, but if I reference the library in an
other project, class becomes a default public ctor, that is what
I don't want.


Regards

Holger
 
Holger said:
This was what I checked out, but if I reference the library in an
other project, class becomes a default public ctor, that is what
I don't want.

I'm not sure what you mean here. If you provide a Private Sub New,
then the class cannot be instantiated directly from another class. The
compiler only supplies a default constructor if you don't explicitly
declare one. As for inheritance, constructors are not inherited so any
derived classes will have to have a private constructor as well if you
want to prevent them from being instantiated directly.
 
Hello Chris,
I'm not sure what you mean here. If you provide a Private Sub New,
then the class cannot be instantiated directly from another class. The
compiler only supplies a default constructor if you don't explicitly
declare one. As for inheritance, constructors are not inherited so any
derived classes will have to have a private constructor as well if you
want to prevent them from being instantiated directly.

It's problem of missviewing in the VB.NET Express 2005 IDE. The IDE
adds allways a Public Default Constructor to my classes in class view,
which not exists. I have now tried all kinds of Construtors, the test can
be download from this URL :

http://www.vbwebprofi.de/public/_news_/VB.NET/Classes/Constructors.zip

The result of my test is that

Protected Friend Sub New()

or

Protected Friend Sub New(ByVal in_Parameter As Object)

and

Friend Sub New()

or

Friend Sub New(ByVal in_Parameter As Object)

fit the former Public Not Creatable in VB6. The both first implementations
give programmers the option to inherit my class and call my own constructor
with MyBase.New() or MyBase.New(MyParameter).


Regards

Holger
 
If you add a constructor to your class that has arguments, but don't add
a default (argument-less) constructor, that should hide the default constructor.

Public Class SomeClass
Public Sub New(ByVal startupInfo As Object)
' ----- Add relevant code here.
End Sub
End Class

Users of SomeClass will now be required to pass an argument to the constructor;
just calling "New SomeClass" will not work.
 
Back
Top