By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,238 Members | 1,485 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,238 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Can't you just mark the class as mustinherit?

i.e.

Public MustInherit MyClass()
....
End Class

Thanks,

Seth Rowe
Holger Boskugel wrote:
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
Nov 14 '06 #2

P: n/a
On Nov 14, 6:17 am, "Holger Boskugel" <news.p...@vbwebprofi.dewrote:
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

Nov 14 '06 #3

P: n/a
Hello Seth,
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
Nov 14 '06 #4

P: n/a
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
Nov 14 '06 #5

P: n/a
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.

Nov 15 '06 #6

P: n/a
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/_new...nstructors.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
Nov 15 '06 #7

P: n/a
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
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

Nov 15 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.