473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

private instance constructor

Hello!

If you have private instance constructor you can then instance object from
within the class itself.
It is any point in doing this? I mean is there situation where this can be a
point doing this.

//tony
Nov 1 '07 #1
4 1422
3 common reasons:
* in 1.1, as part of a static class
* a ctor used by the public ctors
* for use from static members (such as the singleton pattern).

ctor example:

private MyClass(int value, string name) {
// do all the actual work
}
public MyClass(string value) : this(0, value) {}
public MyClass(int value) : this(value, "") {}

-------

singleton (simple field approach; other patterns are available ;-p):

private MyClass() {}
public static readonly MyClass Default = new MyClass();

-------

other static method:

private static int counter;
private MyClass(int id) {
this.id = id;
}
public static MyClass Create() {
// give each a unique id, or do something else useful ;-p
int newId = Interlocked.Increment(ref counter);
return new MyClass(newId);
// (ok, this could be done in the instance ctor, but hey!)
}

-----

Marc

Nov 1 '07 #2
Hello TonyJ,

This is mostly used when you need to control the creation of your object,
for example in Singleton pattern
when we hide the constructor and manually controlling when create the new
instance and when return existed one

---
WBR,
Michael Nemtsev [.NET/C# MVP] :: blog: http://spaces.live.com/laflour

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
TIf you have private instance constructor you can then instance object
Tfrom within the class itself.
T>
Nov 1 '07 #3
TonyJ wrote:
Hello!

If you have private instance constructor you can then instance object from
within the class itself.
It is any point in doing this? I mean is there situation where this can be a
point doing this.
How about where you want to take mostly similar but slightly different
actions on data passed when it's coming from a different constructor? e.g.:

public class A
{

public A()
: this("some","default","values",0.01m,false)
{}

public A(string a, string b, string c, decimal d)
: this(a,b,c,d,true)
{

}

private A(string a, string b, string c, decimal d, bool hidden)
{
if (hidden)
{
// Do some automatic stuff, rather than popping
// up a dialog box and asking the user.
}
else
{
// Prompt a user for some information
}

// Do stuff common to both constructors.
}
}

Chris.
Nov 1 '07 #4
Hi,

It's used in a lot of places.

In addition to the other posts, consider the case of a DataRow. It's
existence and its very same structure depends of a containing table. What
is the solution?
DataTable has a NewRow method that internally create a DataRow and return
it. You cannot create a DataRow on your own.

The above pattern assure that the DataRow will be correctly initialized.

There are more examples like this. just post back if you need more.

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"TonyJ" <jo*****************@telia.comwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
Hello!

If you have private instance constructor you can then instance object from
within the class itself.
It is any point in doing this? I mean is there situation where this can be
a
point doing this.

//tony


Nov 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Will | last post by:
Is it possible to have private instance members in a javascript class? function myObject() { var private = 1; this.getPrivate = function() { return private; } this.incrementPrivate =...
34
by: Andy | last post by:
1) Is there any use of defining a class with a single constructor declared in private scope? I am not asking a about private copy constructors to always force pass/return by reference. 2) Is...
4
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is,...
1
by: Andrew Poulos | last post by:
I having some trouble understanding how to make functions private I have created an instance of an object using a constructor function and there are 4 prototypes: Comm = function() { //blah...
5
by: zqhpnp | last post by:
class String { public: String& operator=(const String& str); private: char* pdata; } String& String::operator=(const String& str) { if(this==&str)
12
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
4
by: =?Utf-8?B?YmlsbCB0aWU=?= | last post by:
I'm perusing a class that seems to utilize a nested class for private fields. In a nutshell, it looks as follows: public class Foo { public Property1 { get/set Bar.field1 }
10
by: siddhu | last post by:
Dear Experts, I want to make a class whose objects can be created only on heap. I used the following approach. class ss { ss(){} public: void* operator new(size_t sz)
6
by: PLS | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.