RE: Provide access to a class without access to the constructor



One way is to put A and B in one assembly and make the constructor for B
internal.

Another is with nested classes:
public class B
{
private B()
{
}

public class A
{
public A()
{
}
public B CreateB()
{
return new B();
}
}
}

But, then the class A would resolve through B:

B.A a = new A();
B b = a.CreateB();
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#


"Weeble" wrote:

Suppose I have a class A that works with instances of class B. I would
like it so that only an instance of A can generate instances of B, but
I would still like users of A to be able to handle instances of B,
including passing them as arguments to methods on A. Is there any way
to achieve this? Ideas so far:

Make B public, but make its constructor internal. This is okay, but
not great. It stops code in other assemblies from constructing
instances of B, but I'd rather prevent *everything* outside of A from
constructing instances of B.

Have a public interface, IB, and make B a private class inside of A.
This is not good because other code can still create instances of IB,
which might not behave at all like B, and pass them as arguments to
methods on A.

Give B a private constructor, and put A inside B. This works, but it's
*weird*, and becomes horrible if I want to have multiple classes like
B, because it requires A to be inside all of them, so they end up like
russian dolls.

I feel sure there must be a simple way to do this, but I can't think
of it. Any help would be much appreciated.

.



Relevant Pages

  • Re: what is the RIGHT THING to make a constructor of a subclass, using super()
    ... If you are left with that requirement as a necessary or desirable part of the design, then the usual approach is probably to write a static method in the class that you call as an argument to the call to superfrom your constructor. ... public class TestDerived extends TestBase ... public Customer(String firstName, String lastName) { ... public class SlashSeparatedCustomer extends Customer { ...
    (comp.lang.java.programmer)
  • Re: Do some computation before calling super constructor.
    ... I want to do some computation in my constructor before invoking the ... Now for some manipulations of automata, ... public class StateSet extends State { ...
    (comp.lang.java.help)
  • Re: getting used to Java - question about "style"
    ... > "Subclasses may override nonfinal methods and Java will dispatch a call to ... > before executing the derived class constructors. ... > constructor." ... public class B extends A ...
    (comp.lang.java.programmer)
  • Re: constructor
    ... constructor, x will be initialized two times. ... public class Class ... > FIeld initializers run before the base class cstructor runs, the construcor body rus after hte base class constructor runs ... > public class test ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Real simple Class question-Newbe
    ... Look up Constructor. ... >public class testParseString ... >public string InputString ...
    (microsoft.public.dotnet.languages.csharp)