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

Private constructor idiom

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


P: n/a
On 2007-11-06 15:25:46 -0800, PLS <no****@nowhere.comsaid:
[...]
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

Nov 7 '07 #2

P: n/a
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
"PLS" wrote:
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
Nov 7 '07 #3

P: n/a
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
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
Nov 7 '07 #4

P: n/a
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);
}
}
--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"Michael C" wrote:
"PLS" <no****@nowhere.comwrote in message
news:MP************************@msnews.microsoft.c om...
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
Nov 7 '07 #5

P: n/a
Peter Ritchie [C# MVP] <PR****@newsgroups.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 8 '07 #6

P: n/a
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#
"Jon Skeet [C# MVP]" wrote:
Peter Ritchie [C# MVP] <PR****@newsgroups.nospamwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 8 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.