Home All Groups Group Topic Archive Search About

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

Author
14 Nov 2006 12:17 PM
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

Author
14 Nov 2006 1:58 PM
rowe_newsgroups
Can't you just mark the class as mustinherit?

i.e.

Public MustInherit MyClass()
....
End Class

Thanks,

Seth Rowe


Holger Boskugel wrote:
Show quoteHide quote
> 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
Author
14 Nov 2006 3:45 PM
Holger Boskugel
Hello Seth,

Show quoteHide quote
> > 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.

> 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
Author
14 Nov 2006 2:06 PM
Chris Dunaway
On Nov 14, 6:17 am, "Holger Boskugel" <news.p***@vbwebprofi.de> wrote:

> 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
Author
14 Nov 2006 3:48 PM
Holger Boskugel
Hello Chris,

> > 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

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
Author
15 Nov 2006 2:44 PM
Chris Dunaway
Holger Boskugel wrote:
> > 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.
>

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.
Author
15 Nov 2006 3:42 PM
Holger Boskugel
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.

> 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
Author
15 Nov 2006 4:16 PM
Tim Patrick
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.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> 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
>