Click here to Skip to main content
15,868,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a usercontrol with only a textbox in it. I want to parse the string of that textbox to a double (I'm making lots of calculations). I thought it should be easy to do that in the code-file of the usercontrol, so I tried something like this:
C#
public double txt
{ 
    get { return double.Parse(txtInput.Text); } 
    set { txtInput.Text = value.ToString(); } 
}

When I do this, there is no problem. On my form I'm trying to do:
C#
vCurrent = Math.Round(uscontrTextbox.txt * velocity, 4);

But when I do this I get the following error:
Cannot implicitly convert type 'string' to 'double'
This error is related to the designer class of the form:
C#
this.uscontrTextbox.txt = "";


How can I solve this?
Posted

C#
vCurrent = Math.Round(Convert.ToDouble(uscontrTextbox.txt) * velocity, 4);


or

instead of,
C#
this.uscontrTextbox.txt = "";
use,
C#
this.uscontrTextbox.txt = 0;

Happy coding!
:)
 
Share this answer
 
v2
Comments
Frans Jan 8-Oct-12 5:21am    
I want to avoid coding like this, becasue I need to do lots of calculations with the textboxes. In this way, I still need to add the Convert.ToDouble() every time I use the control. As said, I'm trying to avoid this, and that's why I chose to use a usercontrol, hoping I could handle the parsing/converting to double only there :)

Thanks for your answer though!
Aarti Meswania 8-Oct-12 5:26am    
ok, then instead of
this.uscontrTextbox.txt = "";
use
this.uscontrTextbox.txt = 0;
Frans Jan 8-Oct-12 5:29am    
I thought of that as well, but actually I don't want the input fields to have '0' as default string.

But if this is the only way, is there a way to preset this? Else I need to change this every time in the designer. (for about 200+ controls...)
Aarti Meswania 8-Oct-12 5:35am    
ok, then use,
public double txt
{
get { return double.Parse((txtInput.Text.Trim()==""? 0 : txtInput.Text.Trim())); }
set { txtInput.Text = value.ToString(); }
}
C#
<pre>get
{
if(txtInput.Text=="")return 0;
else return double.Parse(txtInput.Text); 
}
 
Share this answer
 
In order to not set the default string as "0" I used the following code in my usercontrol:
C#
public double txt
{ 
    get { return double.Parse(txtInput.Text); } 
    set { txtInput.Text = value.ToString(); } 
}

public string text
{
     get { return txtInput.Text; }
     set { txtInput.Text = value; }
}

And in the designer class of the form I replaced
C#
this.uscontrTextbox.txt = "";
with
C#
this.uscontrTextbox.text = "";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900