0

For my school assignment, I have to do a cinema ticketing pool that calculates the prices, discounts, taxes etc. I need to show the Total, the discount and then the total after Discount. Im trying to do the total - the discount to get that answer but it wont let me and im out of ideas to fix this error.

Here is the code:

GroupLbl.Text = "Total discount: " + ((Kids * Price) * 0.75 + (Adults * Price) + (Seniors * Price) * 0.5) * 0.1;
DiscountLbl.Text = "Total after discount: " +TotalLbl.Text - GroupLbl.Text;

Operator '-' cannot be applied to operands of type 'string' and 'string'

Anyone knows the problem?

Thank you so much for the help!

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • 11
    Welcome - dont post pics, post code .. post the error, post what you tried – BugFinder Feb 22 '17 at 15:35
  • 1
    Just FYI, "it wont let me" is not a good explanation of what the problem is. – crashmstr Feb 22 '17 at 15:35
  • 2
    As this is your first post, I advise you to see [How to ask](http://stackoverflow.com/help/how-to-ask). For example, instead of a pic you should be posting your relevant code and any exception text it is throwing at you – Pikoh Feb 22 '17 at 15:36
  • 1
    Well, imagine that we have `string a = "abcd";` and `string b = "123";` what result do you expect from `string c = a - b;`, please? – Dmitry Bychenko Feb 22 '17 at 15:37
  • 1
    What part of the error is confusing you? It tells you that you can't subtract two strings. Where is your actual problem? You already have code that converts strings to numbers so I assume its not that. Is it just that you don't understand why you need to convert to numbers before subtracting things? Or is it something else? – Chris Feb 22 '17 at 15:42

6 Answers6

3

The problem is you are trying to substract strings,which is not possible and is not what you want. Your substract operation must be done using the proper type, in this case probably decimal. So you must parse the string values to decimal,substract them and then, convert the result to string to display it. You should be doing something like this:

DiscountLbl.Text= "Total after discount: " 
                + (decimal.Parse(TotalLbl.Text) - decimal.Parse(GroupLbl.Text)).ToString();

Edit

As Steve and BviLLe_Kid pointed out, as the input comes from a user and it could contain wrong values, you must be using TryParse instead of Parse. So the code would look like this:

decimal total;
decimal group;

if (decimal.TryParse(TotalLbl.Text,out total) && decimal.TryParse(GroupLbl.Text,out group))
{
     string DiscountLbl = "Total after discount: " + (total-group).ToString();
}
Pikoh
  • 7,582
  • 28
  • 53
  • Like what Steve said below in a comment, depending on what the user is typing in (can you really trust them? :) ) you might want to use `.TryParse` – Grizzly Feb 22 '17 at 15:55
  • Yes,I was trying not to give the exact solution to OP so he could maybe find that problem and had to work it out ;). But, of course...in every user input you should use `TryParse` – Pikoh Feb 22 '17 at 15:57
  • haha sorry for ruining your hidden egg! – Grizzly Feb 22 '17 at 16:01
  • 1
    No problem @BviLLe_Kid, you were right anyway. i edited my answer ;) – Pikoh Feb 22 '17 at 16:04
  • Looking at the code that the OP just posted, `decimal.Parse(GroupLbl.Text)` will *always* fail because it'll contain something like "Total Discount: 10.2". – EJoshuaS - Stand with Ukraine Feb 22 '17 at 16:08
  • You are right @EJoshuaS. My second code won't fail as it use `TryParse`, but won't also get the desired result. But that's a different problem and I think OP should deal with it, – Pikoh Feb 22 '17 at 16:17
1

You need to convert the string to an int before subtraction. Use something like int x = Convert.ToInt32(theString).

Liam
  • 27,717
  • 28
  • 128
  • 190
George Newton
  • 788
  • 8
  • 12
1

You have to cast text to double

DiscountLabel.Text = "Total after discount:" + (Convert.ToDouble(TotalLbl.Text) - Convert.ToDouble(GroupLbl.Text)).ToString();
1

You can do this:

//Since you are dealing with decimals:
string strResult = Convert.ToString(Convert.ToDouble(textBox1.Text) - Convert.ToDouble(textBox2.Text));

And don't forget to properly post your question together with your code so that people here can easily understand and assist you. For more info, you can read: https://stackoverflow.com/help/how-to-ask

Community
  • 1
  • 1
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
1

As others have indicated, you need to convert your strings to integers before you can subtract.

Also, as a general rule, pay close attention to the error message:

Operator '-' cannot be applied to operands of type 'string' and 'string'

In this case, the problem is exactly what the compiler says: '-' can't be applied to strings.

Keep in mind that certain operations are only meaningful for certain types. Consider a few examples of edge cases if you try to do arithmetic on strings:

  • "abc" - "c" (presumably, this means "string difference," in which case the result should be "ab")
  • "10" + "20": this is ambiguous because it could mean either "1020" (string concatenation) or "30" (arithmetic addition). In reality, the compiler will always interpret this as concatenation and the result will be "1020".
  • "abc" - "def" (if this is string difference, the result should be "abc")
  • "abc" * "def": No clue what this means. Think about how the compiler would interpret this (it can't). This really should be a compile error, but it's not obvious how the compiler could "know" that it should be in the general case.

As you can see, when you try to do arithmetic operations on strings, there's no general way for the compiler to figure out what you intended to do. This is especially the case given that there are hard limits on the ability to reason about programs in an automated way (see, for example, the halting problem).

One more thing - look at the value of GroupLbl.Text:

GroupLbl.Text = "Total discount: " + ((Kids * Price) * 0.75 + (Adults * Price) + (Seniors * Price) * 0.5) * 0.1;

This'll be something like "Total Discount: 10.0". What does "10.0" - "Total Discount: 10.0" mean? You're better off storing the total discount in a separate double or decimal variable.

double discount = ((Kids * Price) * 0.75 + (Adults * Price) + (Seniors * Price) * 0.5) * 0.1;
GroupLbl.Text = "Total discount: " + discount.ToString();
double totalAfterDiscount = double.Parse(TotalLbl.Text) - discount;
DiscountLbl.Text = "Total after discount: " + totalAfterDiscount.ToString();
0

You're trying to minus two label controls that contain text (some of which is numerical, which I take from the image is what you want). You cannot do that.

Provide code, other than an image.