mrName
mrName

Reputation: 117

microsoft visual studio basic form

hi i'm brand new to mirosoft visual studio, i would like to create a basic circle but i have the following error: Cannot implicity convert type 'double' to int. an explicit conversion exist. (are you missing a cast?)

 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 WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {


                int radius = Convert.ToInt32(textBox1.Text);

                int circumference = 2 * Math.PI * radius;
                int area = Math.PI * Math.Pow(radius, 2);
                int volume = (4 * Math.PI / 3) * Math.Pow(radius, 3);

                Graphics paper;
                paper = pictureBox1.CreateGraphics();
                Pen pen = new Pen(Color.Red);

                paper.DrawEllipse(pen, 0, circumference, area, volume);
            }
        }
    }

Upvotes: 0

Views: 135

Answers (2)

MikeB
MikeB

Reputation: 2452

If you want to cast a double to an int, you use parantheses like this:

double myDouble = 3.211;
int myInt = (int)myDouble;

In your context:

            int circumference = (int)(2 * Math.PI * radius);
            int area = (int)(Math.PI * Math.Pow(radius, 2));
            int volume = (int)((4 * Math.PI / 3) * Math.Pow(radius, 3));

Upvotes: 0

Afshin
Afshin

Reputation: 4215

int straal = Convert.ToInt32(textBox1.Text);
double omtrek = 2 * Math.PI * straal;
        double oppervlakte = Math.PI * Math.Pow(straal, 2);
        double volume = (4 * Math.PI / 3) * Math.Pow(straal, 3);

        Graphics paper;
        paper = pictureBox1.CreateGraphics();
        Pen pen = new Pen(Color.Red);

        paper.DrawEllipse(pen, 0, Convert.ToInt32(omtrek), Convert.ToInt32(oppervlakte),   Convert.ToInt32(volume));

Upvotes: 1

Related Questions