Non-creatable classes question

G

Guest

Hi

How do I make a class non-creatable out side the DLL it is in but still
allow it to be used a data type? I.e. In the example below I don’t want
Class2 to be New’ed outside this DLL.

Public Class Class1
Private moMyProperty As New Class2

Public ReadOnly Porperty MyProperty() As Class2
Get
Return moMyProperty
End Get
End Property
End Class

Public Class Class2
…
End Class

Many thanks.

Julia.
 
O

Oenone

Julia said:
How do I make a class non-creatable out side the DLL it is in but
still allow it to be used a data type?

You can do this by giving the constructor of your class Friend scope. This
will allow other classes within the same project to create instances of the
class (because they can gain access to the class's constructor), but classes
in other projects won't be able to create them (as the constructor will be
inaccessible to them).

For example:


'Friend constructor -- makes the class Public Not Creatable
Friend Sub New()
MyBase.New()
End Sub


Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top