Goran Stojanovic Goran Stojanovic - 19 days ago 4
C# Question

Cannot implicitly convert string - C#

I'm building very basic BMI Calculator in C# and Win Forms using VS 2012, also I'm quite new to C#. I'm followed some examples and this code should work, but when run code, I'm getting those errors.

Error 3 Argument 1: cannot convert from 'System.Windows.Forms.TextBox' to 'string' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 44 31 bmi_calc
Error 5 Argument 1: cannot convert from 'System.Windows.Forms.TextBox' to 'string' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 45 31 bmi_calc
Error 1 Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox' c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 39 25 bmi_calc
Error 2 The best overloaded method match for 'double.Parse(string)' has some invalid arguments c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 44 17 bmi_calc
Error 4 The best overloaded method match for 'double.Parse(string)' has some invalid arguments c:\users\dell\documents\visual studio 2012\Projects\bmi_calc\bmi_calc\Form1.cs 45 17 bmi_calc


Here is my 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 bmi_calc
{
public partial class Form1 : Form
{
double v;
double t;
double r;


public Form1()
{
InitializeComponent();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{
txtTezina.Clear(); //Btn that resets height and weight field values.
txtVisina.Clear();
txtBmiRez = "";
}

private void button1_Click(object sender, EventArgs e)
{
v = Double.Parse (txtVisina);
t = Double.Parse (txtTezina);

r = t / (v * v);

txtBmiRez.Text = String.Format("{0:f}", r);

}

private void button3_Click(object sender, EventArgs e)
{
Application.Exit(); // Close app
}

}
}


If someone could explain me this, I would be eternally grateful.

Answer

Both txtVisina and txtTezina are TextBox objects, not the strings that are contained within them. You need to use the .Text property coming off those objects to access the string value within the user interface.

For example:

v = Double.Parse (txtVisina.Text);
t = Double.Parse (txtTezina.Text);

and:

txtBmiRez.Text = "";

Funnily enough, your second usage of txtBmiRez is actually correct.

When it comes to parsing doubles (or any object from a string), it's also recommended to handle any potential errors with that which can be done with TryParse. Parse would throw an exception if the strings were not valid numbers whereas TryParse would return false. For example changing your Click method to something like this is advantageous and would reduce any potential for crashing:

private void button1_Click(object sender, EventArgs e)
{
    if(Double.TryParse (txtVisina.Text, out v) &&
         Double.TryParse (txtTezina.Text, out t)) {
        r = t / (v * v);
        txtBmiRez.Text = String.Format("{0:f}", r);
    } else {
        // Handle failure to parse
        MessageBox.Show("Failed to parse text to number.");
    }
}