Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 12th, 2009, 12:05 PM
Authorized User
 
Join Date: Sep 2008
Posts: 54
Thanks: 8
Thanked 1 Time in 1 Post
Default How to take input simple integer in C#?

Hi!

I am a novice in C#, I was trying hard to take simple integer as input in C# but I was having problems, so please could anybody write me a simple program through which I can take simple integer as input.

Thank u.
__________________
How to do programming?
 
Old July 12th, 2009, 12:42 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

What kind of application are you building (desktop, console, web etc) and what have you tried so far?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old July 12th, 2009, 02:32 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

http://dotnetperls.com/console-readline
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old July 16th, 2009, 08:23 AM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default Creating an Integer

Most of the time, input from the user is collected via a textbox, say txtInput. The string data in the textbox is then converted to an integer using one of C# conversion methods. While several are possible, TryParse() can catch many errors without your own exception handlers. A code fragment might be:


bool errorFlag;
int myInteger;

flag = int.TryParse(txtInput.Text, out myInteger);
if (flag == false)
{
MessageBox.Show("Input error: Likely a non-digit input", "Input Error");
txtInput.Focus(); // Assumes you want them to try again.
return;
}

TryParse() accepts the input string from the textbox as input, but also sends the memory location (lvalue) of the integer you're trying to create (myInteger) as the second argument. If the conversion is successful, flag equals true. If an integer cannot be formed from the input, flag is false. The keyword out tells the code to send the memory location (lvalue) of myInteger rather than a copy of it's current value (rvalue). The compiler then knows where to put the converted data when the routine finishes, so myInteger will contain the converted integer if flag is true.

I hope this helps.

Dr. Purdum
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
 
Old July 31st, 2009, 09:19 AM
Authorized User
 
Join Date: Mar 2009
Posts: 74
Thanks: 5
Thanked 0 Times in 0 Posts
Default

if its a console Application then try this: this program calculates the velocity of, say a moving vehicle, thesame concept works for, windows Applications

int dist;
float time=2.3;
float vel;
//assign values
Console.WriteLine("Enter value for distance");

dist = Integer.ParseInt(Console.Read());

vel = dist / time;

Console.WriteLine(vel);





Similar Threads
Thread Thread Starter Forum Replies Last Post
text vs integer paul20091968 Access VBA 2 February 13th, 2007 10:50 PM
testing integer rjonk XSLT 2 July 27th, 2006 03:11 PM
Integer Size Miquella C++ Programming 7 November 6th, 2005 10:31 PM
Testing for integer MattLeek Excel VBA 8 March 7th, 2004 09:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.