简体   繁体   中英

how can I access a class from a subclass of type list

I have the following code in C# and I am trying to find out why I am not able to access from a subclass that is from type list the elements of a class address. This is the code

MemberList list = MemberDB.GetMembers("sql", m_page, 
    m_RecordPerPage, out count, _state);

/*******************************/

public static MemberList GetMembers(string sql, int page, 
    int pageSize, out int count, string parState)
{
    MemberList retval = new MemberList();

    SqlParameter pOut = new SqlParameter("@Count", SqlDbType.Int, 4);
    pOut.Direction = ParameterDirection.Output;

    SqlParameter[] param = new SqlParameter[]{
    new SqlParameter("@Sql", sql),
    new SqlParameter("@parState", parState),
    new SqlParameter("@Page", page),
    new SqlParameter("@PageSize", pageSize),
    pOut };

    using (SqlDataReader reader = SqlHelper.ExecuteReader(
        Helper.ConnectionString, CommandType.StoredProcedure, 
        "[app_Member_Search]", param))
    {
        while (reader.Read())
        //if (reader.Read())
        {
            retval.Add(Read(reader));
            //retval = Read(reader);
        }
    }

    count = Helper.ToInt32(pOut.Value);

    return retval;
}



public class Member
{
    private Address m_address;

    public Address Address
    {
        get { return m_address; }
        set { m_address = value; }
    }

    public Member()
    {
        m_address = new Address();
    }
}

public class MemberList : List<Member>
{
    public MemberList() { }
}


case "Address": 
    retval.Address.Address1 = Helper.ToString(reader[i]);
    //throw new Exception(Helper.ToString(reader[i]));
    //retval.Address1 = Helper.ToString(reader[i]);
    break;

case "Address2": 
    retval.Address.Address2 = Helper.ToString(reader[i]); 
    //retval.Address2 = Helper.ToString(reader[i]); 
    break;

case "City": 
    retval.Address.City = Helper.ToString(reader[i]);
    //retval.City = Helper.ToString(reader[i]); 
    break;

case "State": 
    retval.Address.State = Helper.ToString(reader[i]);
    //retval.State = Helper.ToString(reader[i]); 
    break;

I think your problem may be coming from being confused about the inheritance hierarchy of your MemberList class. It isn't actually a subclass of member at all, it is a subclass of List(Of T). When you replace the T with some class name all you are saying is that the list should be a list of that type, but the list is still a list not a subclass of that type. This is using a .net feature called generics. You can read more about generics here .

If the switch statement in your example is referring to the same retval that can be seen in your fist code snippet you will need to specify an index in the list for it to work. Such as:

//index should be set to the correct index for the Member you are modifying
retval[index].Address.Address1 = "Some Value";

However this will require you to add a new Member to the list before this can work. I would probably make a new Member instance, set is properties in the switch statement and then add it to the MemberList at the end. That way you avoid having to index into the list each time you want to set a property so it is a cleaner and slightly faster solution (in both execution time and writing it time).

When referencing a public or protected method or property on the base class, be sure to prefix it with base.

I wrote a very abstract example that should lead you in the right direction on how to call functions in a parent class from a subclass

using System;

namespace simpletest
{
    class Program
    {
        static void Main(string[] args)
        {
            Bird sparrow = new Bird();
            Console.WriteLine("Is the bird flying? " + sparrow.IsFlying + "\n");

            Console.WriteLine("Make the bird fly.");
            sparrow.Fly();
            Console.WriteLine("Is the bird flying? " + sparrow.IsFlying + "\n");

            Console.WriteLine("Make the bird land.");
            sparrow.Land();
            Console.WriteLine("Is the bird flying? " + sparrow.IsFlying);
            Console.ReadLine();
        }
    }

    public abstract class Animal
    {
        bool _isMoving = false;

        protected bool IsMoving
        {
          get { return _isMoving; }
        }

        protected void StartMoving()
        {
            _isMoving = true;
        }

        protected void StopMoving()
        {
            _isMoving = false;
        }
    }

    public class Bird : Animal, IFlyable
    {
        public void Fly()
        {
            base.StartMoving();
        }

        public void Land()
        {
            base.StopMoving();
        }

        public bool IsFlying
        {
            get { return base.IsMoving; }
        }
    }

   interface IFlyable
   {
       void Fly();
       bool IsFlying { get; }
       void Land();
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM