如何将一个对象数组转换为另一个对象数组 [英] how to one array of object to another array of object assign
本文介绍了如何将一个对象数组转换为另一个对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个班级学生及其相关属性,例如
i have a class student and its related properties like
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
推荐答案
您好,
以下是您的解决方案:
商务课程:
Hi,
Following is your solution:
Business Classes:
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:
// 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));
}
这篇关于如何将一个对象数组转换为另一个对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文