Private constructor idiom

P

PLS

I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.

Thanks,
++PLS
 
G

Guest

Neither C# nor VB have anything equivalent to the C++ 'friend' modifier
('Friend' in VB is just equivalent to 'internal' in C#).
You can declare the constructor as internal, but as you know that will only
limit calling the constructor from anywhere within the same project.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
C++ to C++/CLI
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: convert VB or C# to C++/CLI
 
P

Peter Duniho

[...]
There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

You can use the "internal" keyword to achieve similar behavior (though
not identical).

You can also fix it by changing the class hierarchy so that the factory
is combined with the created class, but then you need one factory per
class. That may or may not be desirable.

With the "internal" keyword on the constructor, you'd just make sure
that class A and class B are in the same assembly, then class B can use
the constructor but it would still be hidden from code outsdie that
assembly.

Pete
 
M

Michael C

PLS said:
I'm converting a piece of c++ code to C#. There's one part I'm having a
problem with.

There is a class A which can be created only by calling a class factory
method on object B. Things won't work correctly if the application
creates an A in any other way.

The idiom for this in C++ is to declare the A constructor private, then
declare B a friend. Thus only object B has access to the A constructor.

What is the equivalent of this in C#?

I see that VB.NET has a Friend access modifier, but it doesn't seem to
do the same thing as it does in c#.

You could create a static method on class A that creates an instance of
itself and passes this instance to the class factory. Or just decentralise
the class factory and move all that functionality into the individual static
methods.

Michael
 
G

Guest

That's one of the most common ways of doing it in C#. For example:

public class MyClass
{
private int value;
private MyClass ( int value )
{
this.value = value;
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="value">Initialization parameters</param>
// <returns>New MyClass Object</returns>
public static MyClass Create ( int value )
{
return new MyClass(value);
}
}
 
J

Jon Skeet [C# MVP]

Peter Ritchie said:
That's one of the most common ways of doing it in C#. For example:

public class MyClass
{
private int value;
private MyClass ( int value )
{
this.value = value;
}
/// <summary>
/// Factory method
/// </summary>
/// <param name="value">Initialization parameters</param>
// <returns>New MyClass Object</returns>
public static MyClass Create ( int value )
{
return new MyClass(value);
}
}

In particular, it can be useful if you've got different units. Look at
TimeSpan.FromSeconds, TimeSpan.FromMilliseconds etc.
 
G

Guest

Indeed. One of the patterns prevalent in the .NET BCL is the avoidance of
parameterized constructors in favour of a factory method. Conversion methods
like FromXXX are great examples. It's much more clear to have a
TimeSpan newTimeSpan = TimeSpan.FromMilliseconds(10000);
than
TimeSpan newTimeSpan = new TimeSpan(10000);

Although the TimeSpan class still has public constructors.


--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
 

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