Simple question for new boy

  • Thread starter Thread starter Ian
  • Start date Start date
I

Ian

Hi

I just started with C# and already i have a problem.

i need to transalt this line from VB6 to C#.

txtAnswer.text = (txtNumberOne.text - 32) * 5/6

Thanks

ian
 
Hi,

I suppose txtAnswer and txtNumberOne are textboxes:

txtAnswer.Text = (txtNumberOne.Text - 32) * 5/6;
 
Yes they are but that line does not work, I have tried it.

The application wont even start up. it says that you cant do calculations on
text data types.
 
Ups. My mistake :)

int number1 = int.Parse (txtNumberOne.Text);

int calc = (number1 - 32) * 5/6;

txtAnswer.Text = calc.ToString ();
 
txtAnswer.text = ((int.Parse(txtNumberOne.text) - 32) * 5/6.0).ToString();
I've seen/read conversions done with Int32.Parse() but you're doing what
appears to me to be the samething with int.Parse(). I guess I'm confused
between the different methods. Do they do the same? What is used in real
life coding? Thanks.
 
All object types have a ToString() method that will convert the content to a
string. In the case of numeric types these are usually the text values of
the content of the variable, inthe case of classes the default is the class
name but other implementations incluse more useful information.

Try using Rectangle.ToString for example.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
I just think it is not particulaly readable. This is better IMO:

int val = int.Parse(txtNumberOne.text);
double d = (val - 32) * 5/6.0;
txtAnswer.text = d.ToString();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

Thanks

Why not is that a bad thing



Richard Blewett said:
txtAnswer.text = ((int.Parse(txtNumberOne.text) - 32) * 5/6.0).ToString();

Not that I'd recommend doing it all on one line ;-)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Hi

I just started with C# and already i have a problem.

i need to transalt this line from VB6 to C#.

txtAnswer.text = (txtNumberOne.text - 32) * 5/6

Thanks

ian



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.799 / Virus Database: 543 - Release Date: 19/11/2004



[microsoft.public.dotnet.languages.csharp]
 
Hi Flip

int is the C# alias for System.Int32 (just as VB.NET has Integer). You will
see some documentation saying using the aliased type names is preferred,
other people saying using the Framework type - I guess it's up to you!

HTH

Nigel Armstrong
 
Int32.Parse() s the same as System.Int32.Parse()

and works because of the

using System;

at the top of the file.

int is the C# alias for the CLR's version of the type, System.Int32.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk
txtAnswer.text = ((int.Parse(txtNumberOne.text) - 32) * 5/6.0).ToString();
I've seen/read conversions done with Int32.Parse() but you're doing what
appears to me to be the samething with int.Parse(). I guess I'm confused
between the different methods. Do they do the same? What is used in real
life coding? Thanks.
 
If the value in the text box can be a decimal value, then you need to use
double.Parse(Text) or float.Parse(Text);
 
Back
Top