Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginner problems

Status
Not open for further replies.

beginnnner

Programmer
Apr 23, 2005
1
HR
Hi,I am a total beginner in C# so i decided to learn it myself because i have programming expirience in other programming languages,
i was about to write my(i think) 10. application in C# and i stucked,its a simple question :
How can i add two numbers,i tried :
i seted up 2 text boxes (textBox1 and 2) one button (Button1) and a label (Label1)

void Button1Click(object sender, System.EventArgs e)
{
string se = this.textBox1.Text ;
Convert.ToDecimal(se);
string se1 = this.textBox2.Text ;
Convert.ToDecimal(se1);
this.label1.Text = se + se1 ;
}

But i get result (6 + 3 = 63),anyone help??
 
what john means is that the Convert.ToDecimal does not convert an object of type string to an object of type Decimal.
it takes an object of whatever type and returns a decimal.
therefore you have to take the return value and store it somewhere. i.e.

Code:
Decimal d1 = Convert.ToDecimal(txtValue1.Text);
Decimal d2 = Convert.ToDecimal(txtValue2.Text);
txtValue3.Text = string.Format("{0}", d1 + d2);

you're probably better off using Decimal.Parse, though, since this is culture neutral in the way it parses thousand separators and decimal points. e.g.

Code:
Decimal d1 = Decimal.Parse(txtValue1.Text, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint);

i.e. Convert.ToDecimal won't cope with an entry of "1,112.00" but Decimal.Parse will, and if the App is run in France, say, only the latter will cope with "1.112,00".

don't forget you'll need a reference to System.Globalization in the using section.


mr s. <;)

 
But i get result (6 + 3 = 63),anyone help??

you declared se1 and se as string. so string conc. as 63.
 
void Button1Click(object sender, System.EventArgs e)
{
string se = this.textBox1.Text ;
float newSE = Convert.ToDecimal(se);

string se1 = this.textBox2.Text ;
float newSE1 = Convert.ToDecimal(se1);

this.label1.Text = (newSE1 + newSE).ToString();
}

You must be a Java programmer...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top