Jump to content
  • Advertisement
Sign in to follow this  
The Lazy One

[.net] Can't get simple c# example to work

This topic is 4608 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

this is my code: class Program { static void Main(string[] args) { System.Console.WriteLine(args.Length); } } it's supposed to write out the length of the command line arguments(i got it off of one of microsofts examples), but all it writes is a boolean if i change args.Length to args.ToString my compiler gives me an error: Error 1 The best overloaded method match for 'System.Console.WriteLine(bool)' has some invalid argument Error 2 Argument '1': cannot convert from 'method group' to 'bool'

Share this post


Link to post
Share on other sites
Advertisement
I ran it with VS2005 and it worked fine.
Console.WriteLine(args.Length.ToString()); // ToString is a function, so include ()
Console.WriteLine(args.Length); // also worked

Share this post


Link to post
Share on other sites
It writes "true" to the console???

Or does it write "1"?

I've seen your "Error 2" message before. That occurs on overloaded functions when you pass it a type that it doesn't have an overload for - it'll print that it can't convert to (some type), where (some type) is the "first" overload of the function. In Console.WriteLine and BitConverter.GetBits (the function that I've seen this error with), the "first" overload is the (bool) overload.

The correct behavior of that code is that it will print the number of command line arguments, not the total length of characters on the command line.

Array.Length returns the number of elements in the array (it has nothing to do with the lengths of the strings in your array).

If it's printing "1", this may be why.

Try this:


class Program
{
static void Main(string[] args)
{
for (int i=0; i<args.Length; ++i)
{
Console.WriteLine("Arg #" + i + ": " + args);
}
}
}

Share this post


Link to post
Share on other sites
If I'm not mistaken, I think you, the programmer, are the one who's soppose to supply the command line argument in order for you to get anything out of string[] argv. Here's an excerpt from one of my C# books:

Quote:

it is possible to enter data (here called arguments) into the Main() method from the console window (or a Windows application) when the program initially is started. To enable this functionality, we need to include a formal parameter of type reference-to-array-object of base type string in the method header of Main() as in the following:

public static void Main(string [] args)

where the array variable, as per tradition, is called args.

Passing arguments from the console into Main() can now be done simply by typing these next to the text you usually type (typically just the name of the program) to invoke the program. For example, to pass the arguments


Planet Blipos is far away


into the Main() method of the program in Listing 10.10 called ConsoleArguments.cs, you must write the following commands in the console window, after having created the ConsoleArguments.cs file with the editor. First, the program is compiled as usual:

C:\MyC#Programs>csc ConsoleArguments.cs<enter>

Then, the arguments are typed next to the name of the program as follows:

C:\MyC#Programs>ConsoleArguments Planet Blipos is far away<enter>

In this case, our program simply prints the content of the args array variable back to the screen, resulting in the sample output shown after the listing.



Listing 10.10 ConsoleArguments.cs
01: using System;
02:
03: class ConsoleArguments
04: {
05: public static void Main(String [] args)
06: {
07: for (int i = 0; i < args.Length; i++)
08: {
09: Console.WriteLine(args);
10: }
11: }
12: }

Planet
Blipos
is
far
away



xeddiex.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!