How can I overload WriteLine to support my object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

WriteLine has default handlers for basic types and object. The object
versions call ToString(), so overloading ToString() would normally do the job.

However, I have an implicit operator bool() in the object, so the compiler
converts my object to bool and calls the bool version of WriteLine instead of
the object version.

Is there any way around this, other than typecasting my object when calling
WriteLine()?
 
Aaron,

Depending on what type you are making the call to WriteLine to (there
are many classes that have this method in the framework), you might be able
to extend the object and overload the method.

However, this is probably impractical. Why not just call the ToString()
method yourself and pass that into the WriteLine method, or, as you
suggested, cast it to object?

Hope this helps.
 
Hi Aaron

the problem is, there is an overload of the WriteLine method htat takes one
bool parameter.
The compiler preferes this overload over that with one parameter of type
object.
You can solve the problem, by casting the value to object. Then the
ovlerload with the bool
parameter is no more apllicalble:

Console.WriteLine((object)value);

Christof
 
Unfortunately, that means the user calling WriteLine() has to do something
different, and I would prefer avoid that.

I'm thinking that in C# implicit casts to a type should only be used if the
object "is" that type, for example if I create my own boolean type (e.g. for
interop), then an implicit cast to bool makes sense, but the C++ trick of
using operator bool to return the status (e.g. C++ streams) is really a
shortcut. Is there a C# best practice regarding this sort of thing?

Nicholas Paldino said:
Aaron,

Depending on what type you are making the call to WriteLine to (there
are many classes that have this method in the framework), you might be able
to extend the object and overload the method.

However, this is probably impractical. Why not just call the ToString()
method yourself and pass that into the WriteLine method, or, as you
suggested, cast it to object?

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Aaron Queenan said:
WriteLine has default handlers for basic types and object. The object
versions call ToString(), so overloading ToString() would normally do the
job.

However, I have an implicit operator bool() in the object, so the compiler
converts my object to bool and calls the bool version of WriteLine instead
of
the object version.

Is there any way around this, other than typecasting my object when
calling
WriteLine()?
 
the problem is, there is an overload of the WriteLine method htat takes one
bool parameter.
The compiler preferes this overload over that with one parameter of type
object.
You can solve the problem, by casting the value to object. Then the
ovlerload with the bool
parameter is no more apllicalble:

Console.WriteLine((object)value);

Christof
Yes, that's right.
It's, Overload Resolution, IIRC.
Roger
 

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

Back
Top