| | |
please help me in this code
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2008
Posts: 5
Reputation:
Solved Threads: 0
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]);
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]);
>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:
>Console.WriteLine(array[0]);
That's not going to print the contents of a student object.
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)
student[] array = new student[x]; for ( int i = 0; i < array.length; i++ ) { array[i] = new student(); //... }
That's not going to print the contents of a student object.
Last edited by Narue; Apr 25th, 2008 at 9:30 am.
New members chased away this month: 5
>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:
Clearly my original helpful comment wasn't enough for someone who wants to be spoonfed.
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)
Console.WriteLine ( array[0].name ); Console.WriteLine ( array[0].gender ); Console.WriteLine ( array[0].tel ); Console.WriteLine ( array[0].classe );
Last edited by Narue; Apr 26th, 2008 at 9:07 am.
New members chased away this month: 5
![]() |
Similar Threads
- Code 19 Registry Error (Windows NT / 2000 / XP)
- Why won't this code work? (VB.NET)
- Need help with DirectX code (C)
- Tutorials & Code Submissions - Questions? (DaniWeb Community Feedback)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C# Forum
- Previous Thread: Limiting of username and password
- Next Thread: Delegate in List<T>.Sort()
Views: 662 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ http httpwebrequest image index input install java label list listbox login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






