473,387 Members | 1,493 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,387 software developers and data experts.

C# constructor

Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
....
}
regrards
Ronny
Nov 10 '08 #1
9 4309
Ronny used his keyboard to write :
Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
...
}
regrards
Ronny
It is at least *very* unusual, but what happens when you compile it?

Why not have that "out" value as a readonly (get; private set;)
property of the constructed class?

Hans Kesting
Nov 10 '08 #2
I've never seen it done, but I can't think why you shouldn't be able to do
such a thing. Mostly though I am curious as to what you intend to use it
for, please tell :-)

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Nov 10 '08 #3
Peter Morris skrev:
I've never seen it done, but I can't think why you shouldn't be able to
do such a thing. Mostly though I am curious as to what you intend to
use it for, please tell :-)
I can see many reasons, where one of the reasons is to return some
status value telling that the constructor actually failed and there is
no reason to go further, and as we know a constructor cannot return null.

Classical situation: Some TakePicture diaglog and the constructor
detects that there is no camera present.

Personally it is probably smarter to have a get {} property you can test
on before firing for example ShowDialog().

--
Bjørn Brox
Nov 10 '08 #4
I would disagree with this usage. If the constructor fails, then
allowing the object to be created would be a bad design decision. An
exception should be thrown to prevent the construction of the object, and
the calling code should look for this vs a return code (in this case).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Bjørn Brox" <bp****@gmail.comwrote in message
news:49********@news.broadpark.no...
Peter Morris skrev:
>I've never seen it done, but I can't think why you shouldn't be able to
do such a thing. Mostly though I am curious as to what you intend to use
it for, please tell :-)
I can see many reasons, where one of the reasons is to return some
status value telling that the constructor actually failed and there is
no reason to go further, and as we know a constructor cannot return null.

Classical situation: Some TakePicture diaglog and the constructor
detects that there is no camera present.

Personally it is probably smarter to have a get {} property you can test
on before firing for example ShowDialog().

--
Bjørn Brox

Nov 10 '08 #5
The only way for a constructor to fail is to throw an exception isn't it?

--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com
Nov 10 '08 #6
Peter Morris skrev:
The only way for a constructor to fail is to throw an exception isn't it?
Well, fail is probably the wrong word in my example above, - it is just
useless to continue, and better to show a decent error/warning message,
but I agree on the conclusion that it is best to
throw an exception.

--
Bjørn Brox
Nov 10 '08 #7
On Mon, 10 Nov 2008 12:39:26 -0800, Peter Morris
<mr*********@spamgmail.comwrote:
The only way for a constructor to fail is to throw an exception isn't it?
It depends on how you define "fail".

That certainly is the canonical way, and in the sense that it's the only
way to prevent an object reference from being returned to the code site
instantiating the object with "new", you're right. But exceptions are far
from the only way to report a failure, and a class _could_ decide to
always allow the object to be instantiated and require the caller to check
some other state, such as the value in an "out" argument, a property,
calling some validation method, or even checking some global state (the
first two were even mentioned as possible methods in this thread already).

All of those approachs, and others not even mentioned, could be considered
a legitimate way to report "failure".

Pete
Nov 10 '08 #8
Ronny wrote:
Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
...
}
regrards
Ronny
The out keyword is rarely used in that way, as has been mentioned. An OO
approach is usually preferred.

An exception would be used if the constructor should actually fail. If
the condition is not really a failure, you could use a static method
that would either call the constructor to create an instance, or return
a null reference.

--
Göran Andersson
_____
http://www.guffa.com
Nov 11 '08 #9
Ronny kirjoitti:
Is it correct to define a constructor with a return value in one of the
paramters as follows -
public constructor1(bool boolvalue, out int intvalue)
{
...
}
regrards
Ronny

System.Threading namespace's Mutex class has a constructor that uses out
on parameters. Mutex(bool initiallyOwned, string name, out bool
createdNow) returns createdNow that tells if the calling thread was
granted initial ownership of the mutex.

--
Arto Viitanen
Nov 12 '08 #10

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

Similar topics

3
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: Marcelo Pinto | last post by:
Hi all, In practice, what is the diference between a default constructor and an explicit default constructor? class Ai { public: Ai() {} };
18
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
8
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
13
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.