5 Replies - 241 Views - Last Post: 31 January 2013 - 07:02 PM Rate Topic: -----

#1 ZkP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 12

Problem with subtracting

Posted 30 January 2013 - 11:18 PM

I'm new to this so bare with me... I'm tying to make a label decrease in a number but I keep getting errors when I try to.

The codes are part of a timer, the error is in the 4th line of this:

            int y = 1;
            string n = "60";
            label1.Text = n;
            label1 = label1.Text - y;


That's my most recent attempt, can anyone tell me what its suppose to be?

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with subtracting

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,382
  • Joined: 04-October 09

Re: Problem with subtracting

Posted 31 January 2013 - 12:27 AM

The label is a String. You can't subtract an int from a String. You might want to Convert the string to an int, to the subtraction, then convert the result back to a string.
Was This Post Helpful? 4
  • +
  • -

#3 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1174
  • View blog
  • Posts: 1,659
  • Joined: 24-August 11

Re: Problem with subtracting

Posted 31 January 2013 - 06:50 AM

Take a look at the int32.TryParse() method.
Was This Post Helpful? 3
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 45
  • View blog
  • Posts: 376
  • Joined: 04-January 11

Re: Problem with subtracting

Posted 31 January 2013 - 06:13 PM

The error should be on this line

label1 = label1.Text - y;



You are assigning the result to a label which is label1 instead of label1.Text

It should be

label1.Text = label1.Text - y;



Also, like the others said, you should parse the string so that it will be converted to integer or float or double (depends on what data type you need)

After you add validation, you may parse it

label1.Text = int.Parse(label1.Text) - y;


This post has been edited by November-06: 31 January 2013 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Problem with subtracting

Posted 31 January 2013 - 06:50 PM

The previous post has:
label1.Text = int.Parse(label1.Text) - y;

This still needs to be cast back to a string:
label1.Text = (int.Parse(label1.Text) - y).ToString();

But TryParse (as mentioned earlier) would be better so that an Exception isn't raised.

This post has been edited by andrewsw: 31 January 2013 - 06:53 PM

Was This Post Helpful? 1
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2875
  • View blog
  • Posts: 9,541
  • Joined: 12-December 12

Re: Problem with subtracting

Posted 31 January 2013 - 07:02 PM

            int theNumber;
            bool success = int.TryParse(label1.Text, out theNumber);
            if (success) {
                label1.Text = (theNumber - y).ToString();
            } else {
                // Doh! Not an integer..
            }

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1