User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 423,581 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,422 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 395 | Replies: 8
Reply
Join Date: Apr 2008
Posts: 4
Reputation: cresol is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cresol cresol is offline Offline
Newbie Poster

please help me in this code

  #1  
Apr 24th, 2008
it is code for array it run but didn't print the array element please help me



Console.WriteLine("Enter the Number of student :");
int x = int.Parse(Console.ReadLine());
student []array = new student[x];



int count=1;
for (int i = 0; i < array.Length; i++)
{
Console.WriteLine("Enter info of student {0}",count);
Console.WriteLine("Name :");
array[0].name = Console.ReadLine();
Console.WriteLine("Gender :");
array[0].gender = Console.ReadLine();
Console.WriteLine("Tel :");
int tel = int.Parse(Console.ReadLine());
array[0].tel = tel;
Console.WriteLine("class :");
array[0].classe = Console.ReadLine();
count++;

}

Console.WriteLine(array[0]);
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: please help me in this code

  #2  
Apr 25th, 2008
>student []array = new student[x];
This creates an array of references to student objects. It doesn't create the actual student objects. You need to do that as well in your loop:
  1. student[] array = new student[x];
  2.  
  3. for ( int i = 0; i < array.length; i++ ) {
  4. array[i] = new student();
  5.  
  6. //...
  7. }
>Console.WriteLine(array[0]);
That's not going to print the contents of a student object.
Last edited by Narue : Apr 25th, 2008 at 8:30 am.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Apr 2008
Posts: 4
Reputation: cresol is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cresol cresol is offline Offline
Newbie Poster

Re: please help me in this code

  #3  
Apr 25th, 2008
thanks alot but it didn't print the array elements it print like this
school.student only
but i want to print element with index [0]
which have name,tel,class,..

thanks for helping

Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: please help me in this code

  #4  
Apr 26th, 2008
>thanks alot but it didn't print the array elements
I know that. That's why I said, and I quote, because you obviously didn't read it: "That's not going to print the contents of a student object.".WriteLine doesn't know what a student class is, so how can you expect it to print the members correctly? It's treated as an object and printed accordingly. If you want to print the members, you do so manually:
  1. Console.WriteLine ( array[0].name );
  2. Console.WriteLine ( array[0].gender );
  3. Console.WriteLine ( array[0].tel );
  4. Console.WriteLine ( array[0].classe );
Clearly my original helpful comment wasn't enough for someone who wants to be spoonfed.
Last edited by Narue : Apr 26th, 2008 at 8:07 am.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Apr 2008
Posts: 4
Reputation: cresol is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cresol cresol is offline Offline
Newbie Poster

Re: please help me in this code

  #5  
Apr 26th, 2008
Thanks alot
I own for you
but new problem is
i want now insert new element without lost old elements in the array can you help me
Reply With Quote  
Join Date: Apr 2008
Location: Belgium
Posts: 47
Reputation: Jens is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
Jens's Avatar
Jens Jens is offline Offline
Light Poster

Re: please help me in this code

  #6  
Apr 27th, 2008
The use of an array with dynamic adding of entries is a little bit more...complex I think.

I'd advise to use an arraylist for that. Do know that if you store an object in an arraylist, that you will still have to parse the item in your arraylist to a student - object.

Hope this helps,
Jens
Reply With Quote  
Join Date: Sep 2004
Posts: 6,303
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 28
Solved Threads: 456
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: please help me in this code

  #7  
Apr 27th, 2008
>I'd advise to use an arraylist for that.
I'd advise against the non-generic collections unless you have a good reason (such as compatibility with the framework prior to .NET 2.0). Rather than ArrayList, use List<> from System.Collections.Generic.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Apr 2008
Location: Belgium
Posts: 47
Reputation: Jens is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 5
Jens's Avatar
Jens Jens is offline Offline
Light Poster

Re: please help me in this code

  #8  
Apr 27th, 2008
Ah, true. Forgot about generics.
I was quite tired when I wrote that, so disregard arraylists.

Thanks for correcting me narue.
Reply With Quote  
Join Date: Apr 2008
Posts: 4
Reputation: cresol is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
cresol cresol is offline Offline
Newbie Poster

Re: please help me in this code

  #9  
Apr 28th, 2008
thanks for you all
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C# Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 7:34 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC