Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1] 2   Go Down

Author Topic: Help with a C# tutorial?  (Read 2225 times)

0 Members and 1 Guest are viewing this topic.
Help with a C# tutorial?
« on: May 17, 2008, 08:53:50 am »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Well, I'm having a look at the tutorials on C# station, and I've put this code into my program.
Code: [Select]
// Namespace Declaration
using System;

// Program start class
class NamedWelcome
{
    // Main begins program execution.
    static void Main(string[] args)
    {
        // Write to console
        Console.WriteLine("Hello, {0}!", args[0]);
        Console.WriteLine("Welcome to the C# Station Tutorial!");
    }
}
Unfortunately, whenever I try running this I get an IndexOutOfRange exception. The site doesn't say anything about something like this. Can anyone help?
And I'm using visual studio 08.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #1 on: May 17, 2008, 12:36:10 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Its probably because you haven't passed it a command line.
Logged
Re: Help with a C# tutorial?
« Reply #2 on: May 17, 2008, 12:41:22 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Its probably because you haven't passed it a command line.
The thing is I can't even get that far, the program runs then VS returns to focus and highlights the line Console.WriteLine("Hello, {0}!", args[0]); in yellow. Then it gives me the error. After I return to the com prompt I can't input anything.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #3 on: May 17, 2008, 12:42:40 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Its probably because you haven't passed it a command line.
The thing is I can't even get that far, the program runs then VS returns to focus and highlights the line Console.WriteLine("Hello, {0}!", args[0]); in yellow. Then it gives me the error. After I return to the com prompt I can't input anything.
Again you need to give it a command line argument. The args[0] part is trying to access the command line argument array, which if you haven't passed anything will be null or of length 0.
Logged
Re: Help with a C# tutorial?
« Reply #4 on: May 17, 2008, 12:48:18 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
Well what's the point of them giving me a tutorial that doesn't work, only to find that to make it work I need to do something which I have absolutely no idea about.
Oh, and link - http://www.csharp-station.com/Tutorials/Lesson01.aspx then Listing 1-2. Getting Command-Line Input: NamedWelcome.cs.
Even when I copy and paste that exact code rather than writing it myself it gives me the same error.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #5 on: May 17, 2008, 12:50:46 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Actually it works fine, and it explains exactly what to do and what its meant to do. Just read the dam thing. Its trying to explain how to capture command line input, which if you haven't given any is obviously not going to work >.>.
Logged
Re: Help with a C# tutorial?
« Reply #6 on: May 17, 2008, 12:58:41 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
All I can understand from this is that if you type NamedWelcome Joe into the command line, it will continue running and substitute the {0} with the name Joe. Even starting a brand new console app didn't work.

This is what happens. I run the program, and before I get a chance to do anything, it minimizes, and this comes up.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #7 on: May 17, 2008, 01:02:34 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
*sigh* Ok, show me what command lines you have passed to it?
Logged
Re: Help with a C# tutorial?
« Reply #8 on: May 17, 2008, 01:05:50 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
That's the problem. I can't even do that. The program runs, I see it for a sec, it minimizes, and that message pops up.
Sorry if I'm being a pain, but I'm trying to explain this as best as possible, and trying to move on from GM... I can't even get the first tutorial working :(.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #9 on: May 17, 2008, 01:06:53 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
... Do you even know what a command line is?
Logged
Re: Help with a C# tutorial?
« Reply #10 on: May 17, 2008, 01:12:01 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
A line that runs the command written into it with the specified arguments?

I have a feeling that it's trying to get the data from the array before the array even has a value in it. But then it doesn't let me write a value in the command prompt before it runs the lines of code. because this code (the next step on in the tutorial) works:
Code: [Select]
    // Namespace Declaration
    using System;

    // Program start class
    class InteractiveWelcome
    {
        // Main begins program execution.
        public static void Main()
        {
            // Write to console/get input
            Console.Write("What is your name?: ");
            Console.Write("Hello, {0}! ", Console.ReadLine());
            Console.WriteLine("Welcome to the C# Station Tutorial!");
        }
    }
EDIT: put the wrong code there before :D.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #11 on: May 17, 2008, 01:16:01 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
No your completely wrong. Thats not what a command line argument is. Read this;

http://en.wikipedia.org/wiki/Command_line_arguments
Logged
Re: Help with a C# tutorial?
« Reply #12 on: May 17, 2008, 01:16:14 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
The array is given by the command prompt.

For example, if you had the exe called "Hello.exe", and entered into the command prompt (cmd.exe):
Hello kill death pain
then the array would contain three arguments, the string "kill", the string "death" and the string "pain".
Logged
Re: Help with a C# tutorial?
« Reply #13 on: May 17, 2008, 01:23:17 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
...Does anyone realise my main problem is that instantly after running the program nothing can be written in the command prompt?
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #14 on: May 17, 2008, 01:24:32 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6629
Do you realise that your not meant to?
Logged
Re: Help with a C# tutorial?
« Reply #15 on: May 17, 2008, 01:25:00 pm »
  • (y)(;>.<;)(y)
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3293
*facepalm*

You use the command prompt to execute the program! You do not run the program by double-clicking on the exe or using run in the editor. Do you want me to demonstrate this using pictures?
Logged
Re: Help with a C# tutorial?
« Reply #16 on: May 17, 2008, 01:33:11 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
No, I didn't. :'(
I'm going to bed.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #17 on: May 17, 2008, 04:13:49 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
XD XD XD, This topic is made of so much fail that it's become an epic win. xD;

Darklight, the args array only stores any information if you pass it command-line arguments WHEN STARTING THE PROGRAM. As in, in your specific case, you open the Windows Command Line, go to whatever folder your program is saved at, and type:
"NamedWelcome Jim", it would output:
Quote
Hello, Jim!
Welcome to the C# Station Tutorial!

If you want, you can set command-line arguments that are sent when you debug the program (when you press F5) by going to your project properties (Project Menu-><Project Name> Properties), going to Debug, and typing various arguments in the Command-Line Arguments box. Keep in mind, though, that this ONLY APPLIES WHEN *YOU* RUN IT THROUGH VISUAL STUDIO, NOT WHEN YOU RUN IT AS AN EXE OR WHEN YOU GIVE IT TO OTHERS.
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Re: Help with a C# tutorial?
« Reply #18 on: May 17, 2008, 05:00:11 pm »
  • *
  • Reputation: +3/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3374
THANK YOU, finally something I can understand. :)
I know I'm a complete noob when it comes to programming in any language than GM, so it would be nice to get a hold on this, because I'd like to.
I know you all mean well with your cryptic clues and answers, but I know nothing about this language, so I can't really just be pointed in the general direction.
Logged
Quote from: Jason
Your community is a bunch of stuck up turds.
Re: Help with a C# tutorial?
« Reply #19 on: May 17, 2008, 06:12:59 pm »
  • Minalien
  • *
  • Reputation: +10/-1
  • Offline Offline
  • Gender: Female
  • Posts: 2119
"Cryptic clues and answers"?

What was cryptic at all about any of them? o.O
Logged
Quote
There's such a double standard about religion in the modern world. Catholics can gather, wear white robes, and say "In nomine Patris, et Filii, et Spiritus Sancti" and be considered normal.

But if my friends and I gather, wear black robes, and say  "Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn", we're considered cultists.
  • Development Blog
Pages: [1] 2   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.023 seconds with 73 queries.