Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a class student and its related properties like
C#
public class student
{
public string name{get;set;}
public string address{get;set;}
}
and student array is
   student[] arrStudent = new student[3];

  hence i created object of student class
student objstudent =new student();

for (int i = 0; i < arrStudent.Length; i++)
               {

objstudent.name = "Rahul";
objstudent.address ="Nagpur,India"  // assingned properties
}

the foreach have applied is to store the object at perticular index

and finally i have assigned all object to array
arrStudent[i]=objstudent;

lets college detail is an another array which includes student array 
  CollegeDetails[] arrCollegeDetails = new CollegeDetails[1];

now i want to all three objects in the student array to arrCollegeDetails at its zeroth index. something like

 arrCollegeDetails[0].arrStudent[i] = objstudent;

pls help me out
Posted
Updated 22-Feb-15 19:47pm
v2

1 solution

Hi,

Following is your solution:

Business Classes:
C#
public class student
{
    public string name { get; set; }
    public string address { get; set; }
}

public class CollegeDetails
{
    public string name { get; set; }
    public student[] students { get; set;   }
}


Implementation:
C#
// Array created for students i.e. Three studetns.
        student[] arrStudent = new student[3];

        // Here you also can create an array for College Details for example I created one single object.
        CollegeDetails objOneCollage = new CollegeDetails();

        // for loop to create three students quickly
        for (int i = 0; i < arrStudent.Length - 1; i++)
        {
            //for each student we have to create new object.
            student objStudent = new student();
            objStudent.address = "Address for Student " + i.ToString();
            objStudent.name = "Name for Student " + i.ToString();

            //Assign new student details to students array
            arrStudent[i] = objStudent;
        }

        // Now assign all students array to College object. if it is in array or single you can assign according.
        objOneCollage.students = arrStudent;

        //Now you can access all objects here
        for (int i = 0; i < objOneCollage.students.Length - 1; i++)
        {
            Response.Write(String.Format("Student Name:{0} \t Student Address:{1} </br>", objOneCollage.students[i].name, objOneCollage.students[i].address));
        }
 
Share this answer
 
Comments
ank170989 24-Feb-15 5:31am    
thank you really helpful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900