friend constructor

  • Thread starter Thread starter Franz
  • Start date Start date
F

Franz

Class Foo has a private constructor. How can I give the right to Class Bar
to use that private constructor?
For example, the constructor of System.Windows.Forms.TreeNodeCollection is
private. How can the TreeView create an instance of TreeNodeCollection?
 
Hi Franz,

You can give it access to a static method that will return a new instance of Foo
 
Morten Wennevik said:
Hi Franz,

You can give it access to a static method that will return a new instance of Foo

I have thought about it. But I just want Bar to be the only class which can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the same
assembly.
 
I have thought about it. But I just want Bar to be the only class which
can
create an instance of Foo.
internal constructor is good. But it opens to all the class within the same
assembly.

You can make Foo a private class of Bar, and have it implement a public
interface.

n!
 
You mean this?

public class Foo {
private class Bar : IBar {
void bar() { ........ }
}
}
interface IBar {
void bar();
}
 
public class Foo {
private class Bar : IBar {
void bar() { ........ }
}
}
interface IBar {
void bar();
}

Yep, of course you only need the public interface if you want it visible
outside of the containing class.

n!
 
I used to worry about that sort of thing too, until one day I realised
that if I couldn't trust my own code (i.e. the code in the same assembly)
then I was doomed anyway.....
Simon Smith
simon dot s at ghytred dot com
http://www.ghytred.com/NewsLook - Usenet for Outlook
 

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

Back
Top