-2

So here I am gona explain the problem: There are 3 textboxes, in two of them we should type some numbers to add and the third one should show the total of these two numbers.

error: cannot implicity convert type 'string' to 'double'

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

    private void button1_Click(object sender, EventArgs e)
    {
        double nr1 = Convert.ToDouble(textBox1.Text);
        double nr2 = Convert.ToDouble(textBox2.Text);
        double nr3 = nr1 + nr2;
        string shfaq = Convert.ToString(nr3);
        textBox3.Text = shfaq;
    }
}
}

I can't figure it out

Albografi
  • 13
  • 6
  • 1
    the error is telling you exactly what the problem is.. what can't you figure out..? what happens when you debug the code..? – MethodMan May 31 '17 at 21:27
  • I don't see an error in your code - it should tell you what line has the error? – NetMage May 31 '17 at 21:27
  • 1
    hint - use debugger to check values of `textBox1.Text` and `textBox2.Text`. Sooner or later you should learn how to use debugger. So why not start learning now? – Sergey Berezovskiy May 31 '17 at 21:27
  • I know where the problem is but I cannot figure out what should I do to fix it. – Albografi May 31 '17 at 21:28
  • where is the problem then what values are in TextBox 1 and 2 – MethodMan May 31 '17 at 21:28
  • Which decimal separator has your OS, and which decimal separator are you typing? – hardkoded May 31 '17 at 21:29
  • I think I just fixed it : the problem was here i guess double nr3 = nr1 + nr2; so I changed it to this double nr3 = (nr1 + nr2); – Albografi May 31 '17 at 21:30
  • look up how to convert double to a string.. here is a free-B https://stackoverflow.com/questions/533931/convert-double-to-string – MethodMan May 31 '17 at 21:31
  • 1
    +1 to @SergeyBerezovskiy - I agree, you should try and learn to use the debugger here to investigate the values. – Cody May 31 '17 at 21:59

2 Answers2

0

When dealing with textboxes which can be null or empty, I have found it best to use the TryParse family to do conversions.

private void button1_Click(object sender, EventArgs e) {
    double nr1, nr2;

    double.TryParse(textBox1.Text, out nr1);
    double.TryParse(textBox2.Text, out nr2);
    double nr3 = nr1 + nr2;
    string shfaq = nr3.ToString();
    textBox3.Text = shfaq;
}
Mad Myche
  • 1,075
  • 1
  • 7
  • 15
0

You can use double.TryParse to do the conversion. TryParse takes a string input and a double out parameter, which will contain the converted value if it passes. TryParse returns false if the conversion fails, so you can check that and do something different on failure:

private void button1_Click(object sender, EventArgs e)
{
    double nr1;
    double nr2;

    if (!double.TryParse(textBox1.Text, out nr1))
    {
        MessageBox.Show("Please enter a valid number in textBox1");
        textBox1.Select();
        textBox1.SelectionStart = 0;
        textBox1.SelectionLength = textBox1.TextLength;
    }
    else if (!double.TryParse(textBox2.Text, out nr2))
    {
        MessageBox.Show("Please enter a valid number in textBox2");
        textBox2.Select();
        textBox2.SelectionStart = 0;
        textBox2.SelectionLength = textBox2.TextLength;
    }
    else
    {
        double nr3 = nr1 + nr2;
        textBox3.Text = nr3.ToString();
    }
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43