Hello everyone,
can anyone please tell me how can we write and call function in C# console application through command prompt
For ex. My consoleapplication1 is having function add
so in command prompt i can run it as
consoleapplication1/add
and it's run successfully.
Loading
shubhangi dPosted Dec 24, 2014, 4:07 AM
if (Message != "")
{
if (Message == "add")
Add();
if (Message == "deduct")
Deduct();
}
It's work now
thanx a lot
Michal HabalcikPosted Dec 24, 2014, 4:02 AM
a suggest putting a breakpoint or writing the content of that array to the console to check what is actually inside.
foreach (var arg in args)
{
Console.WriteLine(arg);
}
shubhangi dPosted Dec 24, 2014, 4:00 AM
ConsoleApplication1/add
Michal HabalcikPosted Dec 24, 2014, 3:54 AM
shubhangi dPosted Dec 24, 2014, 3:52 AM
Michal HabalcikPosted Dec 24, 2014, 3:06 AM
I had updated my post and you didn't realize it.
shubhangi dPosted Dec 24, 2014, 3:05 AM
{
if (args.Length == 0)
{
foreach (var arg in args)
{
if (arg == "add")
Add();
if (arg == "deduct")
Deduct();
}
}
Console.ReadLine();
}
public static int Add()
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
c = a + b;
Console.WriteLine("a + b = {0}", c);
Console.ReadLine();
Console.ReadLine();
return c;
}
public static int Deduct()
{
int a;
int b;
int c;
Console.WriteLine("Enter value of 'a':");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter value of 'b':");
b = Convert.ToInt32(Console.ReadLine());
c = a - b;
Console.WriteLine("a - b = {0}", c);
Console.ReadLine();
Console.ReadLine();
return c;
}
Michal HabalcikPosted Dec 24, 2014, 3:02 AM
can you paste your code snippet here?
shubhangi dPosted Dec 24, 2014, 3:00 AM
It's not working
Michal HabalcikPosted Dec 24, 2014, 2:57 AM
If you add the above code in your Main method, you can call the Add function from command prompt by typing:
consoleapplication1 add
shubhangi dPosted Dec 24, 2014, 2:53 AM
how can we take input then
Michal HabalcikPosted Dec 24, 2014, 2:32 AM
the main method's arguments represent command prompt arguments
static void Main(string[] args)
so you need to handle input in your main method, something like this:
foreach (var arg in args)
{
if (arg == "add")
call your add method here;
}
afterwards, you can call it from command prompt like
consoleapplication1 add
More info on MSDN