cant see sub class properties with base class List

aronmatthew

Well-known member
Joined
Aug 5, 2019
Messages
80
Programming Experience
Beginner
for some reason I cant view properties while debugging using sub class with base class List
1748109912812.png
1748109952947.png
 
In the future please post the relevant code here instead of linking to a location that may go away in the future.

For reference of people who are too scared to open some random link on the Internet:
_La1 class:
using listWrapper.a.a;

namespace listWrapper.La1.La1
{
    public class _La1 : List<_a>
    {
        private string field;
        public string Property
        {
            get { return field; }
            set { field = value; }

        }
        public _La1()
        {
            Property = default(string);
        }
        public _La1(_La1 rh)
        {
         
        }
     

    }
}

DLWa class:
using System.Collections;
using listWrapper.a.LEa;
using listWrapper.a.La;
using listWrapper.a.a;
using listWrapper.La1.La1;
namespace listWrapper.a.LWa
{
    public class DLWa
    {

        public static void driver()
        {
            //cannot see properties contained in sub class while debugging
            _La1 aListInherited = new _La1();
            _LWa aListWrapper = new _LWa();

            int i;
            for (i = 0; i < 3; i++)
            {
                _a a = new _a();
                aListWrapper.List.Add(a);

            }
            i = 0;
            foreach (_a a in aListWrapper)
            {
                aListWrapper[i].Id = aListWrapper.List.nextId("a", 3);
                i++;
            }
        }
    }
}

If you enable "Show raw structure of objects in variable windows" the you get to see the Property that is part of your class that inherits from List<T> without requiring extra clicks. Visual Studio is trying to be helpful in thinking that most people don't need to see the internal implementation of a List<T> class because most people have moved on to the follow the object oriented design principle of "composition over inheritance".
1749733549876.png



If on the other hand you follow the old school object oriented programming from the 80s through the early 90s, and you don't enable that checkbox, then you'll need to go through the extra click of expanding the "Raw View":
1749733838252.png




The reason why your workaround works is because you defined your own type that the Visual Studio heuristics doesn't recognize as a well know class and so it shows all the information regard the classes because it's likely relevant to you. For the people following along at home, the relevant code is below in the spoiler tags.
_LWa class:
using System.Collections;
using listWrapper.a.LEa;
using listWrapper.a.La;
using listWrapper.a.a;
namespace listWrapper.a.LWa
{
    public class _LWa : ALWa, Ia, IComparable, IEquatable<_LWa>,IEnumerable
    {
        public _LWa()
        {
            List = new _La();
           
        }
        public _LWa(_LWa rh)
        {
            List = new _La(rh.List);
         
        }
        public bool Equals(_LWa rh)
        {
            throw new NotImplementedException();
        }
        public int CompareTo(object obj)
        {
            if (obj == null) return 1;
            _LWa _obj = obj as _LWa;
            if (_obj != null)
                return compare(this, _obj);
            else throw new ArgumentException("obj is not a baseClass in baseClass compareTo");
        }
        public static int compare(_LWa lh, _LWa rh)
        {
            throw new NotImplementedException();
        }
        public void write(StreamWriter sw)
        {
            throw new NotImplementedException();

        }
        public bool read(StreamReader sr)
        {


            throw new NotImplementedException();

        }
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public _LEa GetEnumerator()
        {
            return new _LEa(List);
        }

    }
}

ALWa class:
using System.Collections;
using listWrapper.a.LEa;
using listWrapper.a.La;
using listWrapper.a.a;
namespace listWrapper.a.LWa
{
    public abstract class ALWa
    {

        private _La list;
     
        public _La List
        {
            get { return list; }
            set { list = value; }
        }
        public int Count
        { get { return List.Count; } }
        public _a this[int key]
        {
            get { return List[key]; }
            set { List[key] = value; }
        }




    }
}

Ia interface:
using System.Collections;
using listWrapper.a.LEa;
using listWrapper.a.La;
using listWrapper.a.a;
namespace listWrapper.a.LWa
{
    internal interface Ia
    {
        public bool Equals(_LWa rh);
        public bool read(StreamReader sr);
        public int CompareTo(object obj);
        public void write(StreamWriter sw);
    }
}

_La class:
using System.Collections;
using listWrapper.a.a;
namespace listWrapper.a.La
{
    public class _La : List<_a>, ILa, IEnumerable
    {

        public int n;
        public _La() : base() {  }
        public _La(_La rh) : base(rh) { }
        public _a this[int key]{get => getValue(key);set => setValue(key, value);}
        private string generateId(string prefix, int length)
        {
            Random rnd = new Random();
            string id = prefix;
            for (int i = 0; i < length; i++)

                id += rnd.Next(0, 10);
         
            return id;
        }
        public string nextId(string prefix, int length)
        {
            bool flag;
            string id;
            do
            {
                flag = false;
                id = generateId(prefix, length);
                foreach (_a a in this)
                {
                    if (a.Id == id)
                    {
                        flag = true;
                        break;
                    }

                }

            } while (flag);

            return id;

        }
        public _a getValue(int index)
        {
            try
            {
                if (index < 0 || index >= Count)
                    throw new IndexOutOfRangeException("index out of range in list getValue");

                return base[index];
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        private void setValue(int index, _a value)
        {
            try
            {
                if (index < 0 || index >= Count)
                    throw new IndexOutOfRangeException("index out of range in list setValue");

                base[index] = value;
            }
            catch (Exception ex)
            {

            }
        }
        public void write(StreamWriter sw)
        {

            foreach (_a obj in this)

                obj.write(sw);

        }
        public void read(StreamReader sr)
        {

            _a obj = new _a();
            while (obj.read(sr) == true)
            {
                Add(obj);
                obj = new _a();
            }

        }
        //list.sort((x, y) => x.Value.CompareTo(y.Value));
    }
}

Also notice how your workaround still fails to show your public n member that is part of your _La class unless the "Raw View" is expanded:
1749734496791.png
 
Back
Top Bottom