-1

I have implemented a method that requires 2 parameters (arguments) : the first one is the source of the xml file ( type string) and the second one is the destination path of the generated pdf file (type string) This application will be used by another application that will assign automatically the 2 parameters. My question is how should I declare the 2 arguments until I can assign external parameters?

in other meaning : I implemented a console appication . When calling it from cmd , it looks like that : C:> name_of_apllication "path1" "path2". How should I implement the parameters if the called method until they will replaced by "path1" and "path2"?

this is the code of the main class : the method that will be used is : GeneratePDF_CTAF

static void Main(string[] args)
        {

            string input = "";
            string output =  "";

            GeneratePDF.GeneratePDF_Ctaf( ref  input, ref  output );

        }

this is the error screen , it is in french and that means can not find file enter image description here

user1503496
  • 191
  • 4
  • 13
  • 1
    It's very hard to understand what you're asking. If you want your function to accept two parameters, just declare them in the parameter list. – p.s.w.g Feb 03 '14 at 14:41
  • I implemented a console appication . When calling it from cmd , it looks like that : C:> name_of_apllication "path1" "path2". How should I implement the parameters if the called method until they will replaced by "path1" and "path2"? – user1503496 Feb 03 '14 at 14:47
  • See [Command Line Parameters Tutorial](http://msdn.microsoft.com/en-us/library/aa288457(v=vs.71).aspx) – p.s.w.g Feb 03 '14 at 14:50
  • Thanks for the tutorial , but main main method calls the generatePDF_CTAF method that requires the 2 arguments – user1503496 Feb 03 '14 at 14:55
  • @user1503496 - This is a linking error. You can't find a DLL. It has nothing to do with function parameters or xml or oop. Review your linking options in visual studio. Close visual studio and restart, re-install COM components etc. Nothing to do with "programming". – Hogan Feb 03 '14 at 17:14

2 Answers2

1

The command line arguments are passed to the Main method as an array of strings. This is the args parameter in your code, so you can simply extract the parameters you need from there:

static int Main(string[] args)
{
    if (args.Length != 2) 
    {
        Console.Error.WriteLine("This program requires exactly 2 parameters");
        return 1; // error code
    }

    string input = args[0];
    string output = args[1];

    GeneratePDF.GeneratePDF_Ctaf(input, output);
    return 0; // no error
}

Note here, I've modified Main to return an int. A non-zero return value is often used in console applications to provide error information to the calling program. I've also removed the ref keyword from your parameters because it's almost never necessary to use ref parameters in .NET.

Community
  • 1
  • 1
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • exception external limit oh the table ! it shows This program requires exactly 2 parameters – user1503496 Feb 03 '14 at 15:15
  • @user1503496 Did you execute it with command line arguments? If you're debugging in VS, make sure you set the command line arguments in the debug section of the project properties. (See http://msdn.microsoft.com/en-us/library/2kf0yb05.aspx) – p.s.w.g Feb 03 '14 at 15:18
  • from the cmd , it shows me the exception : can not load the file publickeytocken = null – user1503496 Feb 03 '14 at 15:28
  • @user1503496 My guess is you tried to copy the `.exe` out and didn't copy all the `.dll` necessary files *or* you tried to execute it from outside that directory. Try `cd`-ing into the bin directory (the output of your VS build) and executing it from there. – p.s.w.g Feb 03 '14 at 15:54
  • this is what i had done :( – user1503496 Feb 03 '14 at 16:15
0

I think there is something fundamental about using a function that you are not understanding so I will give a short example -- if it does not solve your problem please explain why not:

 void Main(string[] args)
 {
     aFunction(args[1], args[2]);

 }


 void aFunction(string arg1, string arg2)
 {
    Console.WriteLine(arg1);
    Console.WriteLine(arg2);
 }
Hogan
  • 69,564
  • 10
  • 76
  • 117