943,522 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 774
  • C# RSS
Apr 24th, 2008
0

please help me in this code

Expand Post »
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]);
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cresol is offline Offline
5 posts
since Apr 2008
Apr 25th, 2008
1

Re: please help me in this code

>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:
C# Syntax (Toggle Plain Text)
  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 9:30 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2008
0

Re: please help me in this code

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

Reputation Points: 10
Solved Threads: 0
Newbie Poster
cresol is offline Offline
5 posts
since Apr 2008
Apr 26th, 2008
1

Re: please help me in this code

>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:
C# Syntax (Toggle Plain Text)
  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 9:07 am.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 26th, 2008
0

Re: please help me in this code

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cresol is offline Offline
5 posts
since Apr 2008
Apr 27th, 2008
0

Re: please help me in this code

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
Reputation Points: 13
Solved Threads: 6
Light Poster
Jens is offline Offline
47 posts
since Apr 2008
Apr 27th, 2008
1

Re: please help me in this code

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 27th, 2008
0

Re: please help me in this code

Ah, true. Forgot about generics.
I was quite tired when I wrote that, so disregard arraylists.

Thanks for correcting me narue.
Reputation Points: 13
Solved Threads: 6
Light Poster
Jens is offline Offline
47 posts
since Apr 2008
Apr 28th, 2008
0

Re: please help me in this code

thanks for you all
Reputation Points: 10
Solved Threads: 0
Newbie Poster
cresol is offline Offline
5 posts
since Apr 2008
May 2nd, 2011
0
Re: please help me in this code
public void Number() please this code displays must declare a body because it not marked abstract, extern or partial.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Prince Adusei-O is offline Offline
2 posts
since May 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Very quick question, initialisng variable
Next Thread in C# Forum Timeline: SQL Connection





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC