Hey....hello all
i am writing a very simple function of calculating a square of a number entered by the user and then displaying it. While writing the function in c# i am getting n number of errors regarding conversion type....plz help me out of this .....here is the function...

function square(int x)
    {
      int result = 0;
       int val = (Convert.ToInt32(TextBox1.Text)); // is this correct ??
       result = val * val;
       return (result); // error of type conversion
       
    }
and for calling this function shall i write call square(textbox1.text)
or simply the function name ??? plz help me out soon !

cya
Rohan

Recommended Answers

All 2 Replies

private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "100";
int x= square(Convert.ToInt32(textBox1.Text));
textBox2.Text =Convert.ToString(x);

}
public int square(int x)
{
int result = 0;
int val = (Convert.ToInt32(textBox1.Text));
result = val * val;
return (result);

}

i hope this will help you


---------------------------------------------------------------

Hey....hello all
i am writing a very simple function of calculating a square of a number entered by the user and then displaying it. While writing the function in c# i am getting n number of errors regarding conversion type....plz help me out of this .....here is the function...

function square(int x)
    {
      int result = 0;
       int val = (Convert.ToInt32(TextBox1.Text)); // is this correct ??
       result = val * val;
       return (result); // error of type conversion
       
    }
and for calling this function shall i write call square(textbox1.text)
or simply the function name ??? plz help me out soon !

cya
Rohan

You could do it this way:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        // This is the square method
        // C# has no funtions
        // Watch out for overflow here
        int Square(int x)
        {
            return x * x;
        }

        // This is the Click handler of a button I put on the form
        private void Compute_Click(object sender, EventArgs e)
        {
            int X;
            // Parse the text of the textbox
            // You should test here that it contains only digits
            X = int.Parse(inputTextbox.Text);
            // Put the output of the calculation back in the sam place
            inputTextbox.Text = Square(X).ToString();
        }

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.