Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi,
I have 3 class files.

C#
namespace M
{
    class Add
    {
        static void Main(string[] args)
        {
          Add();
        }
    Public static void Add()
    {
     int a=10;
     int b=10;
     int c=a+b;
    }
    }
}

namespace N
{
    class Mul
    {
        static void Main(string[] args)
        {
          Mul();
        }
    Public static void Mul()
    {
     int a=10;
     int b=10;
     int c=a*b;
    }
    }
}

namespace P
{
    class Mul
    {
        static void Main(string[] args)
        {
          Sub();
        }
    Public static void Sub()
    {
     int a=10;
     int b=10;
     int c=a-b;
    }
    }
}


Above three class file i have to use in another class file i.e

Here i need to pass Arguments using Command Line, so how to pass arguments to the below class.
If i pass argument /add then first case statement to be executed.
If i pass argument /Multhen second case statement to be executed.
If i pass argument /Sub then third case statement to be executed.

C#
namespace Maths
{
    class Mathematics
    {
        static void Main(string[] args)
        {
          foreach(string str in args)
          {
           GetAllTasks(str);
          }
        }
    Public static void GetAllTasks(string str)
    {
     switch(args)
     {
      case "/add":
      "Here i need to execute the First Class Method ADD()"
      case "/Mul":
      "Here i need to execute the Second Class Method Mul()"
      case "/sub":
      "Here i need to excute the third class method Sub()"
      break;
     }
    }
    }
}

Please help me....
Posted
Updated 29-Feb-12 16:45pm
Comments
Sergey Alexandrovich Kryukov 29-Feb-12 20:26pm    
The problem is not clear. Any arguments can be passed as the arguments of a method. Why command line arguments present any difficulty?
--SA

First, what's with all the Main methods?? You only ever need one of them and that's the entry point of your application, not of a class. Get rid of them.

Second, you can put all of your Math methods (add, mul, and sub) into a single class. You don't need a seperate class for each method.

You also don't need all the namespaces either. Just one will suffice.

Seriously, pickup a beginners book on C#. You've got so many misconceptions in this code, I don't know where to start explaining it all.
 
Share this answer
 
v2
Comments
Mohammad A Rahman 1-Mar-12 3:13am    
Cover everything.
I updated your code a bit to make it run.

C#
using System;

namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string str in args)
            {
                GetAllTasks(str);
            }
        }
        public static void GetAllTasks(string str)
        {
            switch (str)
            {
                case "/Add":
                    Addition.Add();
                    break;
                case "/Mul":
                    Multiplication.Mul();
                    break;
                case "/Sub":
                    Subtraction.Sub();
                    break;
            }
        }
    }
    public class Addition
    {
        public static void Add()
        {
            int a = 10;
            int b = 10;
            int c = a + b;
            Console.WriteLine(c);
        }
    }

    public class Multiplication
    {
        public static void Mul()
        {
            int a = 10;
            int b = 10;
            int c = a * b;
            Console.WriteLine(c);
        }
    }

    public class Subtraction
    {
        public static void Sub()
        {
            int a = 10;
            int b = 10;
            int c = a - b;
            Console.WriteLine(c);
        }
    }
}

Usage:
VB
C:\>ConsoleApplication20.exe /Add /Mul /Sub
20
100
0
C:\>


There is always a point of beginning......

Hope it helps.

:)
 
Share this answer
 
The question looks very naive. Please see my comment to the question. I don't understand how it could be difficult. You can create any kind of class method (including its constructor) and pass any kinds of argument to this method. This is the answer.

The code you attempt to right also looks very naive. Anyway, please looks at my article where I offer my easy-to-use and easy to maintain command line parser with all the code samples. Please see:
Enumeration-based Command Line Utility[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900