 |
Linux Format forums Help, discussion, magazine feedback and more
|
| View previous topic :: View next topic |
| Author |
Message |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Sat Jan 02, 2010 7:18 pm Post subject: Stuck at the First Hurdle! |
|
|
Hi All
I have never programmed before and decided to have a crack at C# on Monodevelop using the LXF coding academy tutorials on tuxradar. Unfortunately I have got stuck half way through the first one
This is my code:
| Code: |
using System;
using System.IO;
namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
} else {
var contents = File.ReadAllText("todo.txt");
Console.WriteLine(contents);
}
}
}
}
|
And this is the error I get:
| Code: |
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at ToDoList.MainClass.Main (System.String[] args) [0x00000] in /home/stu/Projects/ToDoList/ToDoList/Main.cs:16
|
This error occurs when I run "./ToDo.exe" without parameters. When I run it with something like ./ToDo.exe add "This is a test" it works fine.
If I understand the error correctly it is telling me that the array is not initialising properly from this line (I think):
| Code: |
if (args[0] == "add") {
|
Having gone over my code several times, and spending a long time googling the issue I have got no further on.
Can anyone please explain to me where (and why) I have gone wrong so that I can owrk out how to get it right.
Cheers
Buster _________________ My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing! |
|
| Back to top |
|
 |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Sat Jan 02, 2010 10:03 pm Post subject: |
|
|
It's okay, I've finally worked out what is going wrong!
I haven't completed enough of the code to deal with no parameters at all being entered so when I run:
it causes the error because these lines:
| Code: |
if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
|
are basically saying "if the first parameter equals 'add' then write 'parameter 2' to the file todo.txt".
Therefore when I run it with no parameters at all I get the error because the first part of the code (argument?) fails meaning that the second part cannot complete because it depends on part 1.
If I run it with a first parameter of anything but 'add', for instance:
the program correctly displays the contents of todo.txt as it should.
If anyone has the time to confirm that I have understood this correctly it would be appreciated.
Buster _________________ My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing! |
|
| Back to top |
|
 |
Bazza LXF regular

Joined: Sat Mar 21, 2009 11:16 am Posts: 1381 Location: Loughborough
|
Posted: Sat Jan 02, 2010 11:30 pm Post subject: |
|
|
Well done that man...
If this is your first programming experience and you`ve
solved it yourself then you will easily get to grips with
programming in general...
In your codes` case...
args[x] are the "arguments" that follow the executable command.
Computer definition or arguments:-
(From the WWW.)
`In programming, a value that is passed between programs,
subroutines or functions. Arguments are independent items,
or variables, that contain data or codes. When an argument
is used to customize a program for a user, it is typically
called a "parameter."`
For example...
"MyCommand My name is Bazza" has four arguments,
args[0] == My
args[1] == name
args[2] == is
args[3] == Bazza
"MyCommand" by itself has NO arguments hence, (in your case),
your ORIGINAL error.
SO........
Judging by your code it requires two arguments
ToDoList add sometext<RETURN/ENTER>
"add" is the first argument args[0] that sets the condition
for the "if" statement.
AND
Assuming "todo.txt" exists "sometext" along with a "newline"
character will be added to the end of the file.
Note: I suspect "sometext" is not critical as args[1] becomes
a NULL, (no character in this case), and only a "newline" will
be added BUT "add", args[0], must be included for it to do
this...
that`s the best way I can describe it...
And finally your conclusion was basically correct.
Welcome to the fascination world of programming... :) _________________ 73...
Bazza, G0LCU...
Team AMIGA... |
|
| Back to top |
|
 |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Sun Jan 03, 2010 9:47 pm Post subject: |
|
|
Bazza
Thanks for the comprehensive explanation, it is really appreciated
Your response raised another question for me though:
| Quote: |
Note: I suspect "sometext" is not critical as args[1] becomes
a NULL, (no character in this case), and only a "newline" will
be added BUT "add", args[0], must be included for it to do
this...
|
I can see what you are saying here; and it makes perfect sense however that is not what is happening in practice and I wondered if you (or anyone else) could perhaps explain why?
I have clipped some code output below so that you can see what is going on:
| Code: |
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add Test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add "This is a test"
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
This is a test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ ./ToDoList.exe add
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at ToDoList.MainClass.Main (System.String[] args) [0x00000]
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$ cat todo.txt
Test
This is a test
stu@xps-laptop:~/Projects/ToDoList/ToDoList/bin/Debug$
|
The source code remains as previously posted.
It's not hugely important, I'm just curious that's all.
Thanks again
Buster _________________ My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing! |
|
| Back to top |
|
 |
Ram LXF regular

Joined: Thu Apr 07, 2005 10:44 pm Posts: 1549 Location: Guisborough
|
Posted: Mon Jan 04, 2010 12:12 am Post subject: |
|
|
| Quote: | ./ToDoList.exe add
Unhandled Exception: System.IndexOutOfRangeException: Array index is out of range.
at ToDoList.MainClass.Main (System.String[] args) [0x00000] |
You have not passed a second argument (args[1]) but the following code is expecting it.
The array currently only holds one element (argument) so crashes trying to read the second element.
| Code: |
using System;
using System.IO;
namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
} else {
var contents = File.ReadAllText("todo.txt");
Console.WriteLine(contents);
}
}
} |
An array is a group or set of things
args[] is the array in the above code.
colours[red,green,orange,purple] is an array of 4 colours.
Arrays usually index (count) from zero.
colours[0] = red
colours[1] = green
colours[2] = orange
colours[3] = purple
| Code: |
Console.WriteLine("The first colour is ", colours[0] + "\n" );
|
That would just print out The first colour is red
| Code: |
Console.WriteLine("The fifth colour is ", colours[4] + "\n" );
|
Would error with a Index out of range as there is no 5th element. _________________
Ubuntu LXDE 12.04 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Last edited by Ram on Mon Jan 04, 2010 9:00 am; edited 1 time in total |
|
| Back to top |
|
 |
Buster
Joined: Sat Jan 02, 2010 5:29 pm Posts: 7
|
Posted: Mon Jan 04, 2010 8:53 am Post subject: |
|
|
Got it, thanks very much
Buster _________________ My Computer Once Beat Me at Chess, but it is No Match for Me at Kickboxing! |
|
| Back to top |
|
 |
| View previous topic :: View next topic |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|