|
-
Jun 20th, 2005, 08:04 PM
#1
Thread Starter
PowerPoster
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.
-
Jun 20th, 2005, 09:21 PM
#2
Fanatic Member
Re: args.length doesnt work!
else{
Application.Run(new Form());
Console.WriteLine("Started at Form with arguments...");
}
?
-
Jun 20th, 2005, 09:27 PM
#3
Lively Member
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.
-
Jun 20th, 2005, 11:37 PM
#4
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.
-
Jun 21st, 2005, 12:27 AM
#5
Junior Member
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.
-
Jun 21st, 2005, 12:34 AM
#6
Fanatic Member
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.
-
Jun 21st, 2005, 12:40 AM
#7
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.
-
Jun 21st, 2005, 12:58 AM
#8
Fanatic Member
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.
-
Jun 21st, 2005, 01:35 AM
#9
Junior Member
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.
-
Jun 21st, 2005, 01:48 AM
#10
Fanatic Member
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.
-
Jun 21st, 2005, 02:00 AM
#11
Junior Member
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.
-
Jun 21st, 2005, 06:29 AM
#12
Thread Starter
PowerPoster
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!
-
Jun 21st, 2005, 06:35 AM
#13
Re: args.length doesnt work!
Are you compiling this as an exe or a winexe?
-
Jun 21st, 2005, 06:35 AM
#14
Thread Starter
PowerPoster
Re: args.length doesnt work!
-
Jun 21st, 2005, 06:44 AM
#15
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.
-
Jun 21st, 2005, 06:48 AM
#16
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|