Drumming Ace
Drumming Ace

Reputation: 99

Cannot implicitly convert string to bool

I am trying to make code in a windows form application (visual studios) that should take in text from a text box and check if it is saying something in peculiar and do the operation the text box says, for example if it says "1 + 1" the other text box (in the code it is called "output") should say "2". please check my code and just saying I am a beginner. Also i did try a few things like "Convert.ToBoolean(string)" but it didn't work.

      private void TextEditor_TextChanged(object sender, EventArgs e)
    {
        TextEditor.Text = "";
        Convert.ToBoolean(TextEditor);

        if (TextEditor.Text = "1 + 1")
        {
            Output.Text = "2";
        }

    }

Upvotes: 2

Views: 3123

Answers (4)

MetaColon
MetaColon

Reputation: 2871

First of all: I don't think you need the line (actually, you're not even using the return value of ToBoolean)

Convert.ToBoolean(TextEditor);

Secondly, you'd get an exception thrown, as you can't convert a textBox to a boolean value - you'd have to something like this:

Convert.ToBoolean(TextEditor.Text);

But I don't think this is useful in this context, so leave it away.

Thirdly, you only used one equality operator - this should do:

if (TextEditor.Text == "1 + 1")
//...

Otherwise you assign the text of the texteditor with "1+1" and check afterwards wether the assignment result (this is what was assigned, so in that case it's "1 + 1") is true. Obviously, as the assignment was a string, it can't be true, so the compiler tells you that you can't convert a string ("1 + 1") to bool.

Anyway, if you want to write a calculator, I wouldn't hardcode every result of every calculation. E.g. for addition you could do something like this:

Output.Text = TextEditor.Text.Split('+').Select(s => Convert.ToInt32(s.Trim())).Sum().ToString();

This takes the text, splits it at the + sign (It'd be the same for subtraction or other stuff), converts the (trimmed) operands to integers and sums them, using LINQ.

However, as your numbers could be wrong (your comment pointed to something like that), you should surround this with a try catch - and, as you don't want an empty input to result in an error, you could add the RemoveEmptyEntries option:

try
{
    Output.Text = TextEditor.Text.Split(new []{'+'}, StringSplitOptions.RemoveEmptyEntries).Select(s => Convert.ToInt32(s.Trim())).Sum().ToString();
}
catch (Exception e)
{
    Output.Text = "Wrong input!";
}

Upvotes: 5

A few things:

    TextEditor.Text = "";

You don't want to do this because you're clearing out whatever the user entered.

    Convert.ToBoolean(TextEditor);

This isn't needed because you don't store or use the result. It won't actually convert TextEditor to a boolean "in place." Also, this shouldn't compile, it should be TextEditor.Text.

    if (TextEditor.Text = "1 + 1")

As others have indicated, this is assignment (not comparison), which is the cause of the problem. This is like saying

if ("1 + 1")

which isn't a boolean.

Also, as others have indicated, you don't want to hardcode this because what if people did, for example, "1 + 2"? Also, what if they typed it differently, like "1+1"?

Upvotes: 2

FinnTheHuman
FinnTheHuman

Reputation: 1155

Looks like you're trying to build an interpreter. You should look into Regular Expressions and Expression Trees. I won't lie, it's not very simple, but the result will be awesome if you follow through. You could create your own language on top of C#.

Upvotes: 0

pm101
pm101

Reputation: 1424

if requires a Boolean operator, in this case '==' :)

if (TextEditor.Text == "1 + 1")

Upvotes: 3

Related Questions