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:
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];
}
}
}
And this code is an example of using it:Myclass obj=new Myclass();
int id=obj.CurrentItem.Id;
My problem is: I don't know that is there any way to access properties of CurrentItem directly without writing CurrentItem. like this code:Myclass obj=new Myclass();
int id=obj.Id;
Is there any way to do this?
Thanks
h.goli
Iftikar HussainPosted Sep 19, 2013, 7:57 AM
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
Abhishek JainPosted Sep 19, 2013, 7:54 AM
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