Click here to Skip to main content
11,923,958 members (68,322 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0 VS2012
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;
Posted 20-Jan-13 7:08am
Edited 20-Jan-13 7:11am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151125.3 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100