Click here to Skip to main content
15,041,266 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
First I want to say that I am very noob before you go bashing at how bad my code is, this is the first program I have ever written and I started learning just today. The error I have is on row 27

Also if I could ask another question in my current question. Where is the correct place to put global variables? As you see I put one inside form1 I think. I'm not sure where else to put. I am reading and watching some tutorials but not all my questions are answered. :/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
    public partial class Form1 : Form
        int currentmana = 200;
        public Form1()

        private void button1_Click(object sender, EventArgs e)
            if (currentmana >= manareq.Value && currentmana == manaactive.Value)
                label1.Text = spell.Text;
                currentmana = currentmana - manareq.Value;
            else if (currentmana != manareq.Value || manaactive.Value > currentmana)
               MessageBox.Show("You don't have enough mana");

        private void timer1_Tick(object sender, EventArgs e)
            label2.Text = (currentmana.ToString());


        private void timer2_Tick(object sender, EventArgs e)
            currentmana = 1 + currentmana;
Updated 20-Jan-13 6:11am

You cannot set one type to another without a conversion or explicit cast. I assume that in the line:
currentmana = currentmana - manareq.Value;

that manareq.Value is the decimal type, so you need to cast it to integer thus:
currentmana = currentmana - (int)manareq.Value;

See[^] for further information on explicit casting.
Sergey Alexandrovich Kryukov 20-Jan-13 13:27pm
Explained, a 5. I would also add: it's good to think about choosing more appropriate types where the members/variables are defined in first place, to minimize or eliminate the need for casting. Also, if, in your design, casting is always successful, choosing a better type is usually possible, if not — the questionable casting should be guarded by dynamic cast (which has a performance price) or local exception handling; it's the best to avoid it.
Richard MacCutchan 20-Jan-13 13:31pm
Exactly so, but I find that most of these questions would not arise in the first place if posters actually spent some time learning the language and its rules, before trying to write the code. And I suspect the fault lies with their teachers as much as themselves.
Sergey Alexandrovich Kryukov 20-Jan-13 13:38pm
Quite agree.
Thank you.
An explicit conversion is required to convert decimal to integer.
Use Convert.ToInt[^].

E.g. currentmana = currentmana - Convert.ToInt32(manareq.Value);
Inzann 20-Jan-13 12:21pm
Thanks this seems to have worked, but does this mean that manareq.value is in decimal? I thought decimal was only values that could be 1.1 0.34 etc? I just want it to make sense to me. :P
Abhinav S 20-Jan-13 12:27pm
The variable manareq is not defined in the code snippet you have provided so its really difficult to figure out what it is, but from the error, it does appear it was a decimal.
Vote if it helped.
Inzann 20-Jan-13 12:43pm
So should I define it in the future? If so how do I do that? Or can I just use your solution in the future for the same problem?
Abhinav S 20-Jan-13 21:40pm
You have defined it already. It is a control on the form.
Richard MacCutchan 20-Jan-13 13:32pm
That is a conversion, not a cast.
Abhinav S 20-Jan-13 21:41pm
Corrected my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900