I'm writing a class(MyClass) that have a subclass(Item) and a property of List of that subclass(List).
MyClass have a property with name "
CurrentItem" that returns required Item.My class is like this:
C#
public class MyClass
{
private DataTable _mainDataTable;
private List _items;
public class Item
{
public Item()
{
}
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool Enabled { get; set; }
}
private MyClass()
{
}
public DataTable MainDataTable
{
get
{
if (_mainDataTable == null && _items != null)
_mainDataTable = _items.ConvertToDataTable();
return _mainDataTable;
}
set { _mainDataTable = value; }
}
public List Items
{
get
{
if (_items == null && _mainDataTable != null)
{
_items = _mainDataTable.ConvertToListOfItems();
}
return _items;
}
set { _items = value; }
}
public int CurrentItemIndex { get; set; }
public Item CurrentItem
{
get
{
if (Items == null || Items.Count == 0 || CurrentItemIndex + 1 > Items.Count)
return new Item();
return Items[CurrentItemIndex];
}
}
}Code
Myclass obj=new Myclass();
int id=obj.CurrentItem.Id;CurrentItem directly without writing CurrentItem. like this code:Code
Myclass obj=new Myclass();
int id=obj.Id;Is there any way to do this?
Thanks
h.goli

Abhishek JainRank #1,062 · 1.8K pointsPosted Sep 19, 2013, 3:54 AM EDT
You can do like this:
MyClass.Item item = new MyClass.Item();
Also, Id is not the property of MyClass, its the property of Item Class.
Regards
AJ
Iftikar HussainRank #423 · 5.9K pointsPosted Sep 19, 2013, 3:57 AM EDT
You can't access of subclass property without using CurrentItem. Otherwise you have to remove your sub-class and declare all property inside the main class
Regards,
Iftikar