views:

254

answers:

4

Hi,

I've been given a small c# project to write, which basically is to wrap a few dlls in a console application.

I've hit what I see as a weird problem. I've declared a couple of local variables within the main method to use. The idea is that when the arguments are parsed they values are store in these variables ( the arguments are in key pairs e.g. -u:username ).

Below is the code that I am using to start the process..

namespace ziptogo
{
public class ZipToGo
{


    public static void Main(string[] args)
    {

        string user = null;
        int divisionid = 0;
        string mysqlServer = null;
        string mysqlName = null;
        string mysqlUser = null;
        string mysqlPwd = null;
        string barcode = null;
        bool zipped = false;

        ZipToGo ziptogo = new ZipToGo();

        if (args.Length == 0)
        {
            ziptogo.usage();
        }

        //we look throught the arguments and extract the values.
        for (int i = 0; i < args.Length; i++)
        {

            string[] values = ziptogo.getArgValue(args[i]);

            if (values[0].Equals("-U") || values[0].Equals("-u"))
            {
                user = values[1];
            }

            if (values[0].Equals("-D") || values[0].Equals("-d"))
            {
                divisionid = Int32.Parse(values[1]);
            }
....

As I am new to writing in c# am I missing something obvious as to why the strings such as mysqlServer are being ignored by the main method??

The integer divisionid and string barcode is the only variables that are not being ignored by the method.

Thanks.

+2  A: 

To test it quickly, you can add this line just after Main():

 args = new String[] { "-u:username" };

Then go through the code step by step, using the debugger.

[Edit] If getArgValue looks something like this:

 public String[] getArgValue(String s)
 {
     return s.Split(':'); 
 }

then it should work IMHO (quick and dirty, just to get it running).

[Edit:] There are some nice solutions for command line parsing available, which remove the need to add all those conditionals, e.g. http://www.codeproject.com/KB/recipes/commandlineparser.aspx.

Groo
Er... or you can just set the debug command line options in Visual Studio (project properties)
Marc Gravell
I felt that it would require less explaining this way :)
Groo
Here's another one that I like to use: http://www.codeproject.com/KB/recipes/command_line.aspx
Kev
A: 

Do you know about debugger?

If you use Visual Studion do this.

  1. Set you caret to string user = null;
  2. Click F9 (Toggle breakpoint);
  3. Click F5 (Strat debugging);
  4. Click F10 to go through your application;

I think you quickly find what is wrong. Often this is stupid misspels...

Mike Chaliy
I've been using the debugger in VS2008 and as soon as the set the breakpoint at the variable declarations. When looking at the variable list in VS it just hasn't set any of the strings at all, but my app doesn't barf when I try and assign the variable if just moves to the next if statement??
Grant Collins
Exactly where do you set the breakpoint? Does `args` contain the list of arguments?
Groo
The breakpoint is on the first string declaration.The arguments list contains the following at the moment -u:username -dbs:servername -dbu:dbuser -dbp:dbpassword
Grant Collins
A: 

What does getArgValue( string[] ) do?

I bet if you do...

public static void Main(string[] args)    
{  
    if ( args.Length > 0 )
        System.Diagnostics.Debug.WriteLine( args[0] );

/* etc */

}

You'll see your missing "-d".

Brian Reiter
A: 

Have a look at this code and see if it helps. It should be doing exactly what you want and is a bit cleaner.

string[] args = new string[] {"-u:username", "-dbs:servername", "-dbu:dbuser", "-dbp:dbpassword"};

string user = null;
int divisionid = 0;
string mysqlPwd = null;

foreach (string arg in args)
{
    string[] parts = arg.Split(':');

    switch (parts[0].ToUpper())
    {
        case "-U":
            user = parts[1];
            break;
        case "-D":
            divisionid = Int32.Parse(parts[1]);
            break;
        case "-DBP":
            mysqlPwd = parts[1];
            break;
            // ....
        default:
            // Display usage
            break;
    }
}

Debug.WriteLine(string.Format("User {0} | Password {1}", user, mysqlPwd));

Obviously the args part should come from you command line and not be hard coded.

Mark