views:

425

answers:

6

Hi, I'm really new to all of this. I need to write an exe application in C#. What i need to do is to be able to pass values through the console into a function. But I'm unsure as to how I can store the values that are entered through the console...

I know we can use Read() to read what has been entered but, when I'm dealing with multiple values how do i do this? Any help will be greatly appreciated!! Thanks in advance

A: 

You want to programmatically compile text into code? If so you should read this KB entry from Microsoft. How to programmatically compile code using C# compiler

Or if you want to get input from the user in the console, you should use Console.ReadLine().

Console.Write("Enter your name: ");
string name = Console.ReadLine();

Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());

Console.Write("Hello {0}, you are {1} years old.", name, age);
Samuel
I think you've shot way over their head with this answer. They want to know how to read input in a console program, not use CodeDOM.
Robert Paulson
Yes I realized that, so I added simple input gathering.
Samuel
And yes, you should use int.TryParse, but I left it out for simplicity.
Samuel
+2  A: 

Are you referring to passing command line parameters to a console application? If so, there is a string array parameter (e.g. args) that holds them. See this code.

static void Main(string[] args)
{
}

You can also use Environment.GetCommandLineArgs.

dommer
+5  A: 

You start with choosing the Console Application template (in New Project)

And then, in the Main function, you can read a line at a time with

string line = Console.ReadLine();

This probably shifts your question to : How do I get values from a string?

If you have a single int at a time, it is

int x = int.Parse(line);
Henk Holterman
A: 

You can basically do a parsing work manually. For example, if the input is a name follows by age.

Natthawut 22

You can use Console.ReadLine() to get the string "Nattawut 22". And then use String.Split(' '); to get an array of {"Natthawut","22"}. Then convert the second element to integer using Convert.ToInt32(val);

I believe there must be a better way to do this but this is how I usually do it :)

m3rLinEz
+1  A: 

Hmm I think he is wondering how to repeatitly read some value and pass it to a function.

For that you can use a simple while loop.

string data = Console.ReadLine();
do {
  dummyFunction(data);
  data = Console.ReadLine();
} while (data != "");
schmrz
A: 
int year, month, day;
Console.WriteLine("Please enter your birth year : ");
year = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter your birth month : ");
month = int.Parse(Console.ReadLine());
Console.WriteLine("Please enter your birth day : ");
day = int.Parse(Console.ReadLine());
Console.Write("You are ");
Console.Write(CalculateAge(year, month, day));
Console.Write(" years old.");

An alternative way is this:

Console.WriteLine("Please enter your birthday in the format YY-MM-DD : ");
string line = Console.ReadLine();
string[] parts = line.Split(new char[] { '-' });
int age = CalculateAge(parts[0],parts[1],parts[2]);
Console.WriteLine("You are {0} years old.", age);

And -PLEASE- do some input checking.

Mike Christiansen