Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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()
        {
            InitializeComponent();
        }
 
        
        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
Inzann117
Edited 20-Jan-13 7:11am
v2
Rate this: bad
good
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 http://msdn.microsoft.com/en-us/library/yht2cx7b(VS.80).aspx[^] for further information on explicit casting.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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.
—SA
Richard MacCutchan at 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 at 20-Jan-13 13:38pm
   
Quite agree.
Thank you.
—SA
Rate this: bad
good
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);
  Permalink  
v3
Comments
Inzann at 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 at 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 at 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 at 20-Jan-13 21:40pm
   
You have defined it already. It is a control on the form.
Richard MacCutchan at 20-Jan-13 13:32pm
   
That is a conversion, not a cast.
Abhinav S at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
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