0

I am doing study and creating a program related to command line arguments.In this program i am going into if statement. Now I have question here why here string[] args length is 0?How can set args value so that I can get the desired result

Here is my code which I am using

static void Main(string[] args)
{
    double val1 = 0.0;
    double val2 = 0.0;
    if (args.Length == 0)
    {
        Console.WriteLine("No argument has been specified");
        Console.ReadLine();
        return;
    }
    val1 = double.Parse(args[0].ToString());
    val2 = Math.Sqrt(val1);
    Console.WriteLine("Square Root of the val2 is: {0}\n",val2);
    Console.ReadLine();
}

Please resolve this problem experts

p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
azad chouhan
  • 3
  • 2
  • 5

4 Answers4

2

You have to run your program with arguments (ie: program.exe args0 args1 args2). If running from VS, then you have to set the project property for debug command arguments

  • With a project selected in Solution Explorer, on the Project menu, click Properties.
  • Click the Debug tab.
  • In the Command line arguments field, enter the command-line arguments you wish to use.
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180
  • from where i can set this property can u please tell me ?? – azad chouhan Dec 12 '13 at 18:44
  • @user3096672 I provided a link and copied the pertinent portion – Justin Pihony Dec 12 '13 at 18:44
  • Im not sure what you are looking for. It basically runs the program and passes in the arguments just like would happen when running the program from the cmd line. Do you just mean that you are not familiar with command line args at all? That is something you should be able to google... – Justin Pihony Dec 12 '13 at 18:50
  • yes you are right mr @JustinPihony i am presently reading and doing practice about command line aurgument and I get this problem thats why I ask. I have one more question How can i pass aurgument using c# code – azad chouhan Dec 12 '13 at 18:56
  • @azadchouhan: command-line arguments are used to pass some info/arguments from outside the application , those arguments should come outside the application, can not be supplied from c# program. – Sudhakar Tillapudi Dec 12 '13 at 18:59
  • This is your entry point, so it is passed in directly. So, if you want to call this directly, then you would do something like Process.Start(programname, arguments) http://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx but that would be another program calling your program, not your program calling itself – Justin Pihony Dec 12 '13 at 18:59
  • @SudhakarTillapudi if possible can you please explain it with any example? – azad chouhan Dec 12 '13 at 19:04
  • @azadchouhan I gave an example as part of my answer – Justin Pihony Dec 12 '13 at 19:06
  • @JustinPihony okk sir i am read that code sir. Its means if i can only pass aurgument from outside my program not from my program?For this I have to create another program or function and i have to use that function or program . Am i right sir? – azad chouhan Dec 12 '13 at 19:11
  • Another program...another function would only work if you are starting a new instance of yourself – Justin Pihony Dec 12 '13 at 19:14
  • @JustinPihony thanks for the help. Now I am clear about it :) – azad chouhan Dec 12 '13 at 19:28
1

Go to project properties -> Debug and Command line arguments

I hope this answers your question, you need to pass cmd args from VS project properties

DotNetUser
  • 6,494
  • 1
  • 25
  • 27
0

Solution : Passing CommandLine arguments From Visual Studio IDE

1.Right click on project.
2.Goto Properties.
3.Select Debug tab from the left panel.
4.you can enter commandline arguments in Command line arguments box under Startup Options by using sace as delimiter.

Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
0

The string[] args may contain any number of command line arguments which we want to pass to Main() method.

If we were executing the application through command prompt we could see how it would work.

For a method as shown

static int Main(string[] args)
{

  for(int i = 0; i < args.Length; i++)
  Console.WriteLine("Arg: {0}", args[i]);
  Console.ReadLine();
  return -1;
}

enter image description here

cation is a text editor we can open the text file like this.

The Main method can be declared with or without a string[] parameter that contains command-line arguments. When using Visual Studio to create Windows Forms applications, you can add the parameter manually or else use the Environment class to obtain the command-line arguments. Parameters are read as zero-indexed command-line arguments. Unlike C and C++, the name of the program is not treated as the first command-line argument.

For more details please refer here

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234