Results 1 to 16 of 16

Thread: args.length doesnt work!

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    args.length doesnt work!

    hi there.

    in my C# app, I have this:

    Code:
    static void Main(string[] args)
    {
    if(args.length == 0)
    {
      Application.Run(new Form1())
    }
    else
    {
      Console.WriteLine("args specified!");
    }
    }
    now, if i try to run this application through a command console and specify arguments, nothing happens - it doesnt display any text on the console.

    if i do not specify any arguments, the application works fine and loads the form - great.

    If i try to debug within VS.NET 2003, and specify arguments, it shows me the args specified! message

    the interesting thing is, if i do a messageBox.show() instead of Console.Writeline - it shows the message box! (with args given to the application)

    any idea what is going on? and yes, i have tried both the release and debug build types

    thanks!
    Last edited by Techno; Jun 20th, 2005 at 08:09 PM.

  2. #2
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: args.length doesnt work!

    else{
    Application.Run(new Form());
    Console.WriteLine("Started at Form with arguments...");
    }

    ?

  3. #3
    Lively Member
    Join Date
    Aug 2002
    Posts
    88

    Re: args.length doesnt work!

    Is there more code to this program? When I run it I get a "'System.Array' does not contain a definition for length" message.

    But if I capitalize "Length" in args.Length I don't get this message.

    Also, you're missing a semi-colon in the code above after "Application.Run(new Form1())" but that's not likely your problem.
    Ed Womack
    Get Milked

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: args.length doesnt work!

    If this is a WinForms app then you may not be able to run it from a command prompt and get output to the Console. I don't know that for a fact as I haven't checked, but it's possible.

  5. #5
    Junior Member Sami Antero's Avatar
    Join Date
    Jun 2005
    Location
    Helsinki, Finland
    Posts
    16

    Re: args.length doesnt work!

    Hi!

    try this :source from: http://msdn.microsoft.com/library/de...rstutorial.asp

    Code:
    // cmdline1.cs
    // arguments: A B C
    using System;
    
    public class CommandLine
    {
       public static void Main(string[] args)
       {
           // The Length property is used to obtain the length of the array. 
           // Notice that Length is a read-only property:
           Console.WriteLine("Number of command line parameters = {0}",
              args.Length);
           for(int i = 0; i < args.Length; i++)
           {
               Console.WriteLine("Arg[{0}] = [{1}]", i, args[i]);
           }
       }
    }
    The proper syntax to get the length of any given one-dimensional array is to use the Length propery of a System.Array object. By entering the length property in lowercase will result in a exception as described in a few posts back. Compile this and run it in console with arguments. It should work.

    t sami
    Last edited by Sami Antero; Jun 21st, 2005 at 01:53 AM.
    Sami Antero
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    no smileys, no funky certificates. Sorry.

  6. #6
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: args.length doesnt work!

    As said, the form shows, so the argument of length and Length is not valid in this case. There are no compile time errors occur in his code. The code posted was just a typo.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: args.length doesnt work!

    The code that Sami Antero posted is no doubt from a Console application, as opposed to a WinForms app as your's is.

  8. #8
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: args.length doesnt work!

    Are you sure, mine is made through WinApp? I can make it a Console App, just referred to System.Windows.Forms namespace to be able to access what Techno needs. By running this also, we can see the console window.

    Techno, also you can set the arguments at Project Properties->Configuration Properties->Command Line Arguments... Just to make you not run the code at command line. Of course, this assumes you have the studio. But if you are in the command line, (hat's off) you'll be fine.

  9. #9
    Junior Member Sami Antero's Avatar
    Join Date
    Jun 2005
    Location
    Helsinki, Finland
    Posts
    16

    Re: args.length doesnt work!

    Yup! I've waken up. It is true that the example I gave was a bit offline with the topic. Sorry. Well at least that got me started.

    I tried to replicate Tenchnos problem but got the whole thing working ok. While in vs2003 the args printout in the output window ok (defined in the project properties) and console with args works ok.

    Techno states that the question of lowercase and uppercase is not valid in this question. How did you get the application to compiled if you are referring to System.Arrays length property?
    Sami Antero
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    no smileys, no funky certificates. Sorry.

  10. #10
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: args.length doesnt work!

    I did not. He said, it works ok, so I assume the code is just typed barely from his mind (not copy pasted from the working editor). But you are too keen to spot .length with .Length.... Well of course, Techno didn't have his original code .length because (as he said) it runs Ok, there's a logical bug. Not -- a syntax error.

    BTW, your code is good for manipulating the args. Just a reference of how to doing this stuff. Nice. Also, the link you've given is broken. The (:) should be removed as the last letter of the link. Please edit. Thanks.

  11. #11
    Junior Member Sami Antero's Avatar
    Join Date
    Jun 2005
    Location
    Helsinki, Finland
    Posts
    16

    Re: args.length doesnt work!

    True, the problem mus be a logical one. The code as I imagine it must be has no flaws in it. The reason I emphasised on the Length argument was ewomacks post on 'System.Array does not ....' post earlier so sorry about it (I end up excusing everything, should have stayed in bed.. ). It would be nice to know though if techno got this sorted.

    ps the link ok now, thanks for the advice
    Sami Antero
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    no smileys, no funky certificates. Sorry.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: args.length doesnt work!

    wow...tyvm guys!

    I am very sorry - the .length was a typo as i was typing in the code! it's .Length...

    I will try the other suggestions.....however i remember doing this stuff in another project at work but forgotten how it was done, im sure it was the same way I written it here...

    I will update you with these resolutions..

    ty!

  13. #13
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: args.length doesnt work!

    Are you compiling this as an exe or a winexe?

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: args.length doesnt work!

    winexe

  15. #15
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: args.length doesnt work!

    try compiling to exe then the output to the console should show up, and you'll still be able to display the Window when no args are applied.

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Re: args.length doesnt work!

    that fixed it!

    thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width