0

I've to convert an object to string only if the object is a boolean.

I've done in this manner:

public object Convert(object oldType)
{
    bool value;
    if (oldType is bool)
    {
        value = (bool)oldType;
        if (value)
            return "1";
        else
            return "0";
    }

Is it safe?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
M4tt3
  • 55
  • 2
  • 9
  • 5
    So what happens if it *isn't* a boolean? – BoltClock May 09 '14 at 16:22
  • 2
    quicker way to return the value would be `return value?"1":"0";` – csharpwinphonexaml May 09 '14 at 16:23
  • 1
    @verdesrobert it would still need to be cast though. `return (bool)value ? "1" : "0";` – Nathan A May 09 '14 at 16:24
  • 1
    Is there something wrong with [`Convert.ToBoolean()`](http://msdn.microsoft.com/en-us/library/system.convert.toboolean.aspx) or [`bool.TryParse()`](http://msdn.microsoft.com/en-us/library/system.boolean.tryparse.aspx)? – qJake May 09 '14 at 16:25
  • 1
    If you want to convert to string, maybe you should consider a method that returns string. – qqbenq May 09 '14 at 16:27
  • 1
    Because this question is nearly entirely opinion based and subjected to requirements not included in the question, I am recommending that this question be closed. Some folks may agree that this is OK, given certain circumstances and guarantees about the input, while others may say that it's a really poor idea and discussion about that is likely to result in non-constructive debate rather than fact based determinations and clear answers to your question. – Michael J. Gray May 09 '14 at 16:27
  • bool.TryParse is what I would use. – Mike Cheel May 09 '14 at 16:29
  • Way too mainstream, SpikeX. @M4tt3, you could check if it is a bool using typeof() like typeof(oldType) == typeof(bool); – Mikael Dúi Bolinder May 09 '14 at 16:29
  • @NathanA I was only referring to the return not to the whole thing – csharpwinphonexaml May 09 '14 at 16:30
  • 3
    I don't see how this is opinion based or unclear. It seems fairly straightforward what he wants to accomplish. If an object is a Boolean, then return a string representation of the Boolean. Is that really that hard to understand? – mason May 09 '14 at 16:31
  • @MikaelDúiBolinder are you sure about `typeof(oldType)` ? – csharpwinphonexaml May 09 '14 at 16:32
  • 1
    The is operator would probably be better than typeof just for readability, although typeof might be faster http://stackoverflow.com/questions/184681/is-vs-typeof – Shattuck May 09 '14 at 16:33
  • `typeof` doesn't work on variable names it only work with types – csharpwinphonexaml May 09 '14 at 16:36
  • @Spike The OP doesn't want to convert to a bool. He wants to take an object that may be bool at runtime and output a string representation of it if it is. I'm assuming that the method should just pass the original object through if it's not a bool. – Andrew Cooper May 09 '14 at 16:36
  • @mason: "Is it ok?" apparently rubs people the wrong way. – BoltClock May 09 '14 at 16:39
  • 1
    @verdesrobert, no, I wasnt. Apparently my code is uncompilable since I should have written oldType.GetType(). – Mikael Dúi Bolinder May 09 '14 at 16:52

1 Answers1

5

You can simplify without the temporary variable:

public object Convert(object value)
{
    if (value is bool)
    {
        return (bool)value ? "1" : "0";
    }
    return value;
}
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116