C# 2008 help.

Project X

Expert Member
Joined
May 16, 2006
Messages
1,615
Reaction score
1
Ok this is embarrassing:o I cant get the hello world app to work.

Heres my code

Code:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(args[0]);
            Console.ReadKey();   
        }
    }
}
ok its not really the hello world app its the second step. anyway this is the error i get. indexOutOfRangeException was unhandled Index was outside the bounds of the array.:confused:
 
You didn't provide any arguments for the console app... ;) When running it, call it with something like:

ConsoleApplication1.exe "Hello World!"

The "args" parameter is an array of arguments you provide for the application upon calling it. If you don't provide any parameters, then the args[] is empty. If you try and access an item in the array at a specific point (i.e. accessing item "0" in the array, accesses the first item) and the item isn't there, it is outside the bounds of the array, hence your exception. Does that make any sense? :p
 
Last edited:
You didn't provide any arguments for the console app... ;) When running it, call it with something like:

ConsoleApplication1.exe "Hello World!"

The "args" parameter is an array of arguments you provide for the application upon calling it. If you don't provide any parameters, then the args[] is empty.

The thing is im following some tutorial and here what i did at first Console.WriteLine("Hello, " + args[0]); but still
 
Just do :
Console.WriteLine("Hello World");
Console.ReadLine();
 
Just do :
Console.WriteLine("Hello World");
Console.ReadLine();

That works but i want to pass in arguments

and i have a problem i cant really move on to the next page without having accomplished what was required on the previous page.
 
The idea with the tutorial is that it should print something like "Hello, Project X", where "Project X" is input into the application upon calling it.

So, when running the app, don't just press F5 in Visual Studio to run it. Build the application, open up the Windows Command Line interface, browse to the "bin" folder of your application and run it as follows:

ConsoleApplication1.exe "Project X"

You have to specify an argument for the application for it to be able to access the first item in the "args" array. If you don't, you will keep getting that exception...
 
In VS 2008. Go to the properties of your project->Debug->Start Options->Command Line Arguments.

Type Your greeting in there.

commandline.jpg
 
The idea with the tutorial is that it should print something like "Hello, Project X", where "Project X" is input into the application upon calling it.

So, when running the app, don't just press F5 in Visual Studio to run it. Build the application, open up the Windows Command Line interface, browse to the "bin" folder of your application and run it as follows:

ConsoleApplication1.exe "Project X"

You have to specify an argument for the application for it to be able to access the first item in the "args" array. If you don't, you will keep getting that exception...
Ok i tried that but it just shoows the "hello," but not the name i entered in the console
 
No common i want to do it the normal way
That is the right way - and it works. I copied your code into a new project, replicated your error exactly, did exactly what was suggested (Properties -> Debug -> Start Options -> Command line arguments), and it worked. We're trying to help you here. :(
 
Project X, have you tried doing it ^ that way?

Yes it finally worked thanks:D I think it was because i didn't add the strings (that does make a diff right?) .... Im surprised no one flipped for me not being able to do such an easy task:rolleyes:

Sorry for late reply gad problems with my connection
 
Last edited:
That is the right way - and it works. I copied your code into a new project, replicated your error exactly, did exactly what was suggested (Properties -> Debug -> Start Options -> Command line arguments), and it worked. We're trying to help you here. :(

The windows command line works but thanks... Is that way exactly like the windows command way? or is that easier cause if it is id rather do the harder one.
 
Top
Sign up to the MyBroadband newsletter
X