C# constructor

  • Thread starter Thread starter Ronny
  • Start date Start date
R

Ronny

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
 
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
 
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 :-)
 
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().
 
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).
 
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.
 
Ronny said:
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.
 
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.
 
Back
Top