By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,627 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,627 IT Pros & Developers. It's quick & easy.

Custom Object and Profile Object

P: n/a
a
I need to create an instance of a custom object 'School.Teacher' and use it
in a Profile object.

I'm developing a bad case of "Pretzel Logic" thinking about this.

Filling the custom object 'School.Teacher' as an ArrayList creates the
proper information (see aspx code below), but I'm unable to use this
ArrayList in the Profile object. The aspx code below shows the attempt and
error message.

Any ideas?

-----------------------------------------------------------------------------------------------------------------------------------

The Custom object 'School.Teacher' is in Section 3 on this page

Section 1 ================================================== = web.config
Profile property

<add name="School" type="System.Collections.ArrayList" serializeAs="Xml"
allowAnonymous="true"/>

Section 2 ================================================== = aspx code

protected void Button1_Click(object sender, EventArgs e)

{

//School.Teacher is a SINGLE object, NOT a collection.

ArrayList teachers = new ArrayList(); // Create a new ArrayList

// Add
Teacher-------------------------------------------------------------------------

School.Teacher teacher = new School.Teacher("Marc"); // Create a new Teacher

// Add 10 students to the
teacher------------------------------------------------------

for (int i = 0; i < 10; i++)

{

teacher.Students.Add(new School.Student("Student " + i.ToString()));

}

// Add Classes
------------------------------------------------------------------------

// Create 5 classes for this teacher AND add students 4,6 and 10 to each
class.

for (int i = 0; i < 5; i++)

{

School.sClass newClass = new School.sClass("Class " + i.ToString());

teacher.Classes.Add(newClass);

teacher.Students.Add(teacher.Students[3]);

teacher.Students.Add(teacher.Students[5]);

teacher.Students.Add(teacher.Students[9]);

}

// Add this teacher(that now has classes and students) to the teachers
ArrayList

teachers.Add(teacher);

// Return 'teachers' array of the type School.Teacher[] (cast from the type
'School.Teacher')

// This equation raise the cast error:

// Can't cast 'School.Teacher[]' to 'School.Teachers'(ie Profile.Teachers)

Profile.Teachers.Add((School.Teacher[])teachers.ToArray(typeof(School.Teacher)));

}

Section 3 ================================================== ======= Custom
Object

using System;

using System.Collections;

using System.Xml.Serialization;// need for 'Teacher Students Property'
decoration

namespace School

{

#region Student
Object++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++

[Serializable()]

public class Student

{

#region Private members (Fields) ++++++++++++++++++++

int id;

string firstName;

string lastName;

int gPA;

int currentGradeLevel;

#endregion -------------------------------------------

#region Public Accessors (Properties) ++++++++++++++

public int Id

{

get { return this.id; }

set { this.id = value; }

}

public string FirstName

{

get { return this.firstName; }

set { this.firstName = value; }

}

public string LastName

{

get { return this.lastName; }

set { this.lastName = value; }

}

public int GPA

{

get { return this.gPA; }

set { this.gPA = value; }

}

public int CurrentGradeLevel

{

get { return this.currentGradeLevel; }

set { this.currentGradeLevel = value; }

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++

// Empty constructor

public Student()

{ }

// Partial Constructor

public Student(string FirstName)

{

this.firstName = FirstName;

}

// Full Constructor

public Student(int Id, string FirstName, string LastName, int GPA, int
CurrentGradeLevel)

{

this.id = Id;

this.firstName = FirstName;

this.lastName = LastName;

this.gPA = GPA;

this.currentGradeLevel = CurrentGradeLevel;

}

#endregion -------------------------------------------

}

#endregion

[Serializable()]

public class Students : CollectionBase

{

#region Public Accessors (Properties) ++++++++++++++

public Student this[int index]

{

set

{

List[index] = value;

}

get

{

return (Student)List[index];

}

}

public int Add(Student value)

{

return List.Add(value);

}

public int IndexOf(Student value)

{

return List.IndexOf(value);

}

public void Insert(int index, Student value)

{

List.Insert(index, value);

}

public void Remove(Student value)

{

List.Remove(value);

}

public bool Contains(Student value)

{

return List.Contains(value);

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++

public Students()

{ }

#endregion -------------------------------------------

}



#region sClass
Object++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++

[Serializable()]

public class sClass

{

#region Private members (Fields) ++++++++++++++++++++

private string className;

private DateTime startTime;

private DateTime endTime;

#endregion -------------------------------------------

#region Public Accessors (Properties) ++++++++++++++

public string Name

{

get { return className; }

set { className = value; }

}

public DateTime StartTime

{

get { return this.startTime; }

set { this.startTime = value; }

}

public DateTime EndTime

{

get { return this.endTime; }

set { this.endTime = value; }

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++

// empty constructor

public sClass()

{ }

// Partial Constructor1

public sClass(string ClassName)

{

this.className = ClassName;

}

// Full Constructor

public sClass(string ClassName,DateTime StartTime, DateTime EndTime)

{

this.className = ClassName;

this.startTime = StartTime;

this.endTime = EndTime;

}

#endregion -------------------------------------------

}

#endregion

[Serializable()]

public class Classes : CollectionBase

{

public sClass this[int index]

{

set

{

List[index] = value;

}

get

{

return (sClass)List[index];

}

}

#region Public Accessors (Properties) ++++++++++++++

public int Add(sClass value)

{

return List.Add(value);

}

public int IndexOf(sClass value)

{

return List.IndexOf(value);

}

public void Insert(int index, sClass value)

{

List.Insert(index, value);

}

public void Remove(sClass value)

{

List.Remove(value);

}

public bool Contains(sClass value)

{

return List.Contains(value);

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++

public Classes()

{ }

#endregion -------------------------------------------

}



#region Teacher
Object++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++

[Serializable()]

public class Teacher

{

#region Private members (Fields) ++++++++++++++++++++

private string name;

School.Classes classes = new School.Classes();

School.Students students = new School.Students();

#endregion -------------------------------------------

#region Public Accessors (Properties) ++++++++++++++

public string Name

{

get { return name; }

set { name = value; }

}

public School.Classes Classes

{

get { return this.classes; }

set { this.classes = value; }

}

//[XmlIgnore] // XmlIgnore is need for proper nesting of the xml output

public School.Students Students

{

get { return this.students; }

set { this.students = value; }

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++
public Teacher()

{ }

public Teacher(string name)

{

this.name = Name;

}

public Teacher(string Name, School.Classes Classes)

{

this.name = Name;

this.classes = Classes;

}

public Teacher(string Name, School.Classes Classes, School.Students Students)

{

this.name = Name;

this.classes = Classes;

this.students = Students;

}

#endregion -------------------------------------------

}

#endregion

[Serializable()]

public class Teachers : CollectionBase

{

#region Private members (Fields) +++++++++++++++

#endregion --------------------------------------

#region Indexer +++++++++++++++++++++++++++++++++

public Teacher this[int index]

{

set

{

List[index] = value;

}

get

{

return (Teacher)List[index];

}

}

#endregion ------------------------------------

#region Public Accessors (Properties) ++++++++++++++

public int Add(Teacher value)

{

return List.Add(value);

}

public int IndexOf(Teacher value)

{

return List.IndexOf(value);

}

public void Insert(int index, Teacher value)

{

List.Insert(index, value);

}

public void Remove(Teacher value)

{

List.Remove(value);

}

public bool Contains(Teacher value)

{

return List.Contains(value);

}

#endregion -------------------------------------------

#region Constructors +++++++++++++++++++++++++++++++++

public Teachers() { }

#endregion -------------------------------------------

}

}
Feb 17 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.