Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Noob needs help with C#

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
Judda



Joined: Sun Jan 10, 2010 9:41 pm
Posts: 9

PostPosted: Sun Jan 10, 2010 9:59 pm    Post subject: Noob needs help with C# Reply with quote

Been Ubuntu user for a long while now and I'd thought I'd have a good at learning to program (no previous experience). So I bought the recent special edition "Paul Hudson's Coding Academy".

Fell at the first hurdle Sad

Having trouble with homework(1/3) on Project One. Done 2 & 3 with ease.

How would I count the number of items (it's a to do list) I have in an array (single dim) as I need to print out their total?

Any help/suggestions welcome and appreciated.
Back to top
View user's profile Send private message
ollie
Moderator


Joined: Mon Jul 25, 2005 12:26 pm
Posts: 2749
Location: Bathurst NSW Australia

PostPosted: Mon Jan 11, 2010 2:47 am    Post subject: Reply with quote

You may find C# Count Array Elements useful, it is only a new posting.

HTH
Back to top
View user's profile Send private message
Judda



Joined: Sun Jan 10, 2010 9:41 pm
Posts: 9

PostPosted: Mon Jan 11, 2010 12:01 pm    Post subject: Reply with quote

Thanks Ollie I'll have a look.
Back to top
View user's profile Send private message
Judda



Joined: Sun Jan 10, 2010 9:41 pm
Posts: 9

PostPosted: Mon Jan 11, 2010 12:14 pm    Post subject: Reply with quote

I've just had a read and that's too advanced for what is expected for Project1.

Here is the code:

using System;
using System.IO;

namespace ToDoList
{
class MainClass
{
public static void Main(string[] args)
{
if (args.Length == 0) {
Console.WriteLine("Usage: list | add <item> | del <num>");
return;
}

if (args[0] == "add") {
File.AppendAllText("todo.txt", args[1] + "\n");
} else if (args[0] == "del") {
var contents = File.ReadAllLines("todo.txt");
File.Delete("todo.txt");

var counter = 1;
var deleteme = int.Parse(args[1]);

foreach(var item in contents) {
if (counter != deleteme) {
File.AppendAllText("todo.txt", item + "\n");
}

counter++;
}
} else {
var contents = File.ReadAllLines("todo.txt");
var counter = 1;

foreach(var item in contents) {
Console.WriteLine(counter + ": " + item);
counter++;
}
}
}
}
}

I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented
Back to top
View user's profile Send private message
pasti



Joined: Wed Sep 17, 2008 5:36 pm
Posts: 2

PostPosted: Mon Jan 11, 2010 1:15 pm    Post subject: Reply with quote

Hello

I just got the Lxf special edition "coding Academy", but I'm stuck at the first project although I don't seem to be the only person who is, one of my problems is with the first command you have to enter into the terminal:-

cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"

And when I enter this line:-

echo Meh > todo.txt

nothing at all happens, just a blinking cursor next to my name

then I get completely stuck, both from the magazine & the online tutorial, especially the part where it says you have to change the line "using System" to "using System.IO;", I mean do you delete the first line, add another line, spacing or what?,
anyway where do I enter:-

var contents = File.ReadAllText("todo.txt");

as in the magazine it doesn't tell you, but online it says this:

"Put that into your program just before the Console.WriteLine() line; we'll be using it soon.",
but there aren't any screenshots or anything, And I really feel cheated out of 10, I didn't need the dvd I use ubuntu, and there seems to be more info for free in the online version, all in all feel very frustrated, and as I said a little bit cheated
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1658
Location: Guisborough

PostPosted: Mon Jan 11, 2010 1:40 pm    Post subject: Reply with quote

pasti wrote:
cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"


This is the directory you should have enter in MonoDevelop Location Field just below the Name Field when you named the Project.

If you have not changed it from the default, then you'll find it in /home/hudzilla/Projects/ToDoList/ToDoList/bin/Debug

Provided that you have ran the initial Hello World program by pressing F5

pasti wrote:
And when I enter this line:-

echo Meh > todo.txt

nothing at all happens, just a blinking cursor next to my name



This will have created a text file in the directory you ran the command from, probably your home directory as you had not cd as above..

pasti wrote:

then I get completely stuck, both from the magazine & the online tutorial, especially the part where it says you have to change the line "using System" to "using System.IO;", I mean do you delete the first line, add another line, spacing or what?,



The line actually says:

Change that one using line to this:
Code:
 
using System;
using System.IO;



pasti wrote:
I didn't need the dvd I use ubuntu, and there seems to be more info for free in the online version, all in all feel very frustrated, and as I said a little bit cheated


You'll find all the code on the DVD, just browse it if you don't wish to use it.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
pasti



Joined: Wed Sep 17, 2008 5:36 pm
Posts: 2

PostPosted: Mon Jan 11, 2010 4:32 pm    Post subject: Reply with quote

ram

thanks for the reply, I'm still hopelessly lost however, I don't have an "hudzilla" directory anywhere, I'm not using the "live" dvd, just my install of karmic with monodevelop, I know where mono has created the directory, but it hasn't created a text file anywhere in it, I've gone through both the magazine instructions & the online tutorial and still get the same problem, I just don't know what to do next, the instructions aren't exactly newbie programmer friendly, I say that as I'm not new to linux, ah well!
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1658
Location: Guisborough

PostPosted: Mon Jan 11, 2010 5:10 pm    Post subject: Reply with quote

As you not a linux newbie then you should that echo Meh > todo.txt will create a file in the current directory that your terminal is using. echo is a stand unix command to display something usually on screen, in this case it's used with > which redirects it from the screen to a file, so it echos Meh to a file call todo.txt. If the file did not exist it would create it.

to test. open a terminal and do the following

Code:

echo test > todo.txt
ls -ltr


The last file in the list should be todo.txt, is it ?
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1658
Location: Guisborough

PostPosted: Mon Jan 11, 2010 6:30 pm    Post subject: Reply with quote

Judda wrote:

I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented


You could, I did first time round, but there is a one line solution.
It does not involve counter but "XXXX.Length"
I'll let you work out what replaces the XXXX

P.S.
For indentation use the code tags
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
compo



Joined: Sat Dec 12, 2009 8:24 pm
Posts: 4

PostPosted: Sat Jan 16, 2010 8:42 pm    Post subject: Reply with quote

[quote="pasti"]Hello

I just got the Lxf special edition "coding Academy", but I'm stuck at the first project although I don't seem to be the only person who is, one of my problems is with the first command you have to enter into the terminal:-

cd ToDoList/ToDoList/bin/Debug

when I hit enter all I get is "no such file or directory"

Hi pasti,

The files are stored in a folder/directory called "Projects" in your home directory. The command in the magazine should read:-

cd Project/TodoList/ToDoList/bin/Debug

It took me a while to discover this. I know everyone makes mistakes but if your trying to teach someone to write code little things like that can really put you off.

compo
Back to top
View user's profile Send private message
Judda



Joined: Sun Jan 10, 2010 9:41 pm
Posts: 9

PostPosted: Sun Jan 17, 2010 1:02 pm    Post subject: Reply with quote

Ram wrote:
Judda wrote:

I was thinking is there any way to just take the value of the counter when it finishes as this will have counted the items?

P.S. sorry it won't stay indented


You could, I did first time round, but there is a one line solution.
It does not involve counter but "XXXX.Length"
I'll let you work out what replaces the XXXX

P.S.
For indentation use the code tags


Thanks for the pointer, much much simplier than using the counter Smile

Thanks again Smile
Back to top
View user's profile Send private message
branmask



Joined: Thu Feb 18, 2010 1:19 am
Posts: 14

PostPosted: Mon Mar 01, 2010 7:57 pm    Post subject: noob needs help Reply with quote

Hello,

I'm having trouble actually deleting something from the file. When I try running the del 1(or 2 or 3) command I get this error message:

Unhandled Exception: System.IO.FileNotFoundException: Could not find file "/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.".
File name: '/home/.../Projects/ToDoList/ToDoList/bin/Debug/todo.txt.'
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000]
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
at System.IO.File.OpenRead (System.String path) [0x00000]
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize) [0x00000]
at System.IO.StreamReader..ctor (System.String path) [0x00000]
at (wrapper remoting-invoke-with-check) System.IO.StreamReader:.ctor (string)
at System.IO.File.OpenText (System.String path) [0x00000]
at System.IO.File.ReadAllLines (System.String path) [0x00000]
at ToDoList1.MainClass.Main (System.String[] args) [0x00000]

This is my code:


if(args.Length==0){
Console.WriteLine("Usage:list|add<item>|del<num>");
return;
}
if (args[0]=="add"){
var contents=File.ReadAllLines("todo.txt");
var counter=1;
foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}
}else if(args[0]=="del"){
var contents=File.ReadAllLines("todo.txt.");
File.Delete("todo.txt");
var counter=1;
var deleteme=int.Parse(args[1]);
foreach(var item in contents){
if(counter!=deleteme){
File.AppendAllText("todo.txt",item +"\n");
}

counter++;
}
}else{
var contents=File.ReadAllLines("todo.txt");
var counter=1;

foreach(var item in contents){
Console.WriteLine(counter+":"+item);
counter++;
}

}
}
}
}

I haven't been able to figure out what I'm doing wrong as of yet. Any help would be most appreciated. Thanks
Back to top
View user's profile Send private message
Ram
LXF regular


Joined: Thu Apr 07, 2005 10:44 pm
Posts: 1658
Location: Guisborough

PostPosted: Mon Mar 01, 2010 10:29 pm    Post subject: Reply with quote

branmask no need to multi post.
_________________

lubuntu LXDE 13.10 running on AMD Phenom II*4; ASUS Crosshair III Formula MB; 4 GB Ram.....
Back to top
View user's profile Send private message MSN Messenger
branmask



Joined: Thu Feb 18, 2010 1:19 am
Posts: 14

PostPosted: Tue Mar 02, 2010 12:58 am    Post subject: Reply with quote

Ram,
"branmask no need to multi post."

sorry for the multi post! thanks for the info.
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
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
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast