h goli

h goli

  • NA
  • 1
  • 2k

Accessing properties of one property of a class, directly

Sep 19 2013 7:49 AM
Hello,
 
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<Item> _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<Item>();
                return _mainDataTable;
            }
            set { _mainDataTable = value; }
        }
        public List<Item> Items
        {
            get
            {
                if (_items == null && _mainDataTable != null)
                {
                    _items = _mainDataTable.ConvertToListOfItems<Item>();
                }
                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

Answers (2)