Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:
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:
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:
this.uscontrTextbox.txt = "";
 
How can I solve this?
Posted 8-Oct-12 0:13am
Frans Jan2.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

vCurrent = Math.Round(Convert.ToDouble(uscontrTextbox.txt) * velocity, 4);
 
or
 
instead of,
this.uscontrTextbox.txt = "";
use,
this.uscontrTextbox.txt = 0;
Happy coding!
Smile | :)
  Permalink  
v2
Comments
Frans Jan at 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 at 8-Oct-12 5:26am
   
ok, then instead of
this.uscontrTextbox.txt = "";
use
this.uscontrTextbox.txt = 0;
Frans Jan at 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 at 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(); }
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

<pre>get
{
if(txtInput.Text=="")return 0;
else return double.Parse(txtInput.Text); 
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In order to not set the default string as "0" I used the following code in my usercontrol:
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
this.uscontrTextbox.txt = "";
with
this.uscontrTextbox.text = "";
  Permalink  

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

  Print Answers RSS
0 Maciej Los 230
1 OriginalGriff 202
2 Richard MacCutchan 185
3 Tomas Takac 146
4 CPallini 125
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100