Serkroth
Serkroth

Reputation: 13

Visual C#: Cannot use the Convert method in winforms anymore

Maybe I am missing something entirely simple here, but in any new windows form I create, I can no longer use the Convert method. I am pretty sure all my using directives are there and I am using no extensions. I have done nothing to change VS since the last time I used it, when everything worked.

Could it be a hiccup of VS 2011 Beta or just a simple error on my part?

Here is my code along with the error displayed:

'System.Windows.Forms.Button' does not contain a definition for 'ToInt32'

and no extension method 'ToInt32' accepting a first argument of type

'System.Windows.Forms.Button' could be found

(are you missing a using directive or an assembly reference?)

Here's the code:

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 TempConvert
{
    public partial class Form1 : Form
    {
        int degreesEntered;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void Convert_Click(object sender, EventArgs e)
        {
            degreesEntered = Convert.ToInt32(Degrees.Text);
        }
    }
}

Upvotes: 1

Views: 970

Answers (4)

jorel
jorel

Reputation: 808

Use fully qualified name as System.Convert

Upvotes: 0

MilkyWayJoe
MilkyWayJoe

Reputation: 9092

Your button Convert should be renamed as it has the same name of the class Convert and you might be using this class in several other parts of the application

Upvotes: 1

Paul Phillips
Paul Phillips

Reputation: 6259

I believe the problem is you have a button named Convert which is hiding the static class - you can try using the full namespace to reference it, or doing an int.Parse

System.Convert.ToInt32(Degrees.Text);

Upvotes: 4

dwerner
dwerner

Reputation: 6602

Use Int32.Parse() when parsing text into integers.

Upvotes: 1

Related Questions