converting text box to double
Using .net 4.0 and VS 2010. Trying to learn programming and struggling my way through. Got this program to work as a console app, but cannot get it to work correctly as a windows form.
I struggle with getting the text box values to convert correctly from string to double.
Doing conversion of american units to metric units. After some research I found this:
double miles = 0;
try { miles = double.Parse(txtmiles.Text); }
catch{}
double yards = 0;
try { yards = double.Parse(txtyards.Text); }
catch { }
double feet = 0;
try { feet = double.Parse(txtfeet.Text); }
catch { }
double inches = 0;
try { inches = double.Parse(txtinches.Text); }
catch { }
This successfully allowed me to take the text box input and convert it to double. The problem is the variables stay = 0. I need them to take the value of the text box.
Also, if I ever figure out how to get the text box value to be used correctly, how do I pass the value typed into the text box to the driver in order to do the correct calculations? VS gives me an error that miles, yards, feet, inches are all unassigned uses of local variables. How do I avoid double defining the variable, but passing the variables to the driver correctly?
what.setmiles(miles);
what.setyards(yards);
what.setfeet(feet);
what.setinches(inches);
Thanks for any help anyone can give. Like I said, I'm trying to learn how to program and I cannot find what I need online to figure this out.
Re: converting text box to double
Let's see your full code. double.Parse doesn't just return 0. You have a problem with your scope somewhere but we need to see the full method(s). A couple of things aside from that though...
1. Empty catch blocks are pretty ugly. If your users are entering invalid data you shouldn't just continue on as if nothing went wrong.
2. You don't need a separate try/catch block for each parse call. Do it like this:
Code:
double miles = 0;
double yards = 0;
double feet = 0;
double inches = 0;
try
{
miles = double.Parse(txtmiles.Text);
yards = double.Parse(txtyards.Text);
feet = double.Parse(txtfeet.Text);
inches = double.Parse(txtinches.Text);
}
catch( FormatException e)
{
// something went wrong, do something about it
}
3. You shouldn't use a TextBox if you can only accept numerical data. Use a NumericUpDown control.
Re: converting text box to double
This is my object class.
namespace WindowsFormsApplication1
{
class Class1
{
double miles, feet, yards, inches;
double kilo, centi, meters;
double m;
public double getmiles()
{
return miles;
}
public double getfeet()
{
return feet;
}
public double getyards()
{
return yards;
}
public double getinches()
{
return inches;
}
public void setmiles(double m)
{
miles = m;
}
public void setfeet(double f)
{
feet = f;
}
public void setyards(double y)
{
yards = y;
}
public void setinches(double i)
{
inches = i;
}
public double getkilo()
{
return kilo;
}
public double getcenti()
{
return centi;
}
public double getmeters()
{
return meters;
}
public void conversion1(double i, double f, double y, double z)
{
double me, k, k2, me2, c, c2;
m = miles * 63360;
i = inches;
f = feet * 12;
y = yards * 36;
z = m + i + f + y;
k = z / 39370.0787;
k2 = z % 39370.0787;
me = k2 / 39.3700787;
me2 = k2 % 39.3700787;
c = me2 / 0.393700787;
c2 = me2 % 0.393700787;
kilo = k;
meters = me;
centi = c;
}
}
}
This is my driver class.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btncalc_Click(object sender, EventArgs e)
{
TextBox txtmiles = new TextBox();
TextBox txtyards = new TextBox();
TextBox txtfeet = new TextBox();
TextBox txtinches = new TextBox();
Label lblkilo = new Label();
Label lblme = new Label();
Label lblcenti = new Label();
Class1 what = new Class1();
double k, m, c;
double miles;
try { miles = double.Parse(txtmiles.Text); }
catch{}
double yards;
try { yards = double.Parse(txtyards.Text); }
catch { }
double feet;
try { feet = double.Parse(txtfeet.Text); }
catch { }
double inches;
try { inches = double.Parse(txtinches.Text); }
catch { }
what.setmiles(miles);
what.setyards(yards);
what.setfeet(feet);
what.setinches(inches);
what.conversion1(inches, feet, yards, miles);
k = what.getkilo();
m = what.getmeters();
c = what.getcenti();
lblkilo.Text = k.ToString("N2");
lblme.Text = m.ToString("N2");
lblcenti.Text = c.ToString("N2");
}
}
}
I know the math isn't quite perfect on the actual conversion part, but for now it works as I try to make sure everything else works the way I want it to.
This is the code that I have right now. I still need to go through and add validation for the inputs, but that part I understand.
The thing that keeps giving me problems right now is getting this to read the text box and convert it from string to double the way I need it to.
Re: converting text box to double
C# has properties, so instead of
Code:
int feet;
int getFeet()
{
return feet;
}
you can:
Code:
int feet;
int Feet
{
get
{
return feet;
}
}
Re: converting text box to double
Why are you creating a bunch of new textboxes? That's why the code doesn't work. They are new objects that are not related to your form UI in any way and the user has never entered anything into them. That is why your variables are always 0, and this is why swallowing exceptions is a bad idea. The Text property for all of those new TextBoxes is an empty string, so double.Parse throws an exception, and the variables all maintain their default value of 0.
Re: converting text box to double
Ok. So everytime I do
TextBox txtmiles = new TextBox();
I create a new text box different from the one on the GUI?
So instead I should just reference the one on the GUI in order for it to read the correct input?
I didn't realize that created new text boxes every time.
Re: converting text box to double
FYI - but you might also want to look at the TryParse of the number values, which means you do not manually have to enclose the "Parse" in a Try/Catch block.
It works (almost) like parse, except it does not throw an exception when it can't parse. Very nifty.
Re: converting text box to double
Or better yet; design your UI so that users aren't able to enter invalid data in the first place.
Re: converting text box to double
Now that I know I was making two text boxes so it wasn't reading the input I wanted it to, I can get the correct output onto the label the way I was trying to. I've been stuck on this for probably a week now in a few separate programs. I should be able to get them all to work now. Thanks for the help!
Re: converting text box to double
Glad to help. I would recommend spending some more time learning the basics though. The fact that this took you so long to figure out means that there is a whole in your understanding of the language that needs to be filled.