Hello!
I want to create an object as a property within the class and access properties of the sub class...
My problem is if I want to create several objects of the sub class i am not able to do that
Can anyone suggest me somesolution to this ?
Please ...
Loading
AlanPosted Aug 16, 2008, 8:07 AM
Just expanding a bit on Alex's example, you could do this:
public ParentObject
{
private SubObject foo;
public SubObject Foo
{
get
{
return foo;
}
set
{
foo = value;
}
}
}
public SubObject
{
private string name;
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
}
// code to set Foo property
ParentObject po = new ParentObject();
SubObject so = new SubObject();
so.Name = "Abhay";
po.Foo = so;
// code to get Foo property
SubObject so2 = po.Foo;
string name = so2.Name;
If you may want to include several SubObjects within ParentObject, then declare the property (and backup field) as being of type SubObject[] or List rather than just SubObject.
Abhay MhatrePosted Aug 15, 2008, 6:44 PM
I mean I want to assign values to properties of subObject
Abhay MhatrePosted Aug 15, 2008, 6:41 PM
thank you very much
AlexPosted Aug 15, 2008, 6:38 PM
public SubObject Foo
{
get
{
return new SubObject();
}
}
Now, every time you access the Foo property, you have a new instance of the sub object.