从列表中访问子类值

时间:2015-05-15 22:22:22

标签: c#

有两节课 1)父类有3个字段*所有类都有getter和setter

public class CellPhone
{
    private string Iemi;
    private string description;
    private decimal price;

    public CellPhone(){}

    public CellPhone(string Iemi, string Description, decimal Price)
    {
        this.Iemi = Iemi;
        this.description = Description;
        this.price = Price;
    }

2)Child有1个字段,但从父类继承所有frield。

public class InDate : CellPhone
{
    private string inDate;

    public InDate(){}

    public InDate(string Iemi, string Description, string InDate,
        decimal Price):base( Iemi,  Description,  Price)
    {
        this.inDate = InDate;
    }

3)我使用manage来编写所有数据,并将日期读取到内存(到列表)

 public partial class frmSellCellPhone : Form
{
    CellPhoneList newList = new CellPhoneList();

    public frmSellCellPhone()
    {
        InitializeComponent();

    }
    private void frmSellCellPhone_Load(object sender, EventArgs e)
    {
        newList.Fill();
    }

    private void btnSale_Click(object sender, EventArgs e)
    {
        int i = cmbBox.SelectedIndex;

    }     

    private void SearchItemOnList()
    {
       foreach (CellPhone c in newList)
        {                
            if (c.IEMI == txtSearchIEMI.Text)
            {
                txtDesciption.Text = c.Description;
                txtInPrice.Text = Convert.ToString(c.Price);
                txtInDate.Text = ""; //can't access inDate?
            }
        }
    }

问题是当我尝试显示时,无法访问日期。

如果提出任何建议,我会表示同意。

2 个答案:

答案 0 :(得分:0)

在以下方法中,您将逐步浏览CellPhone的列表。 CellPhone没有inDate属性。

private void SearchItemOnList() {
   foreach (CellPhone c in newList) {                
        if (c.IEMI == txtSearchIEMI.Text) {
            txtDesciption.Text = c.Description;
            txtInPrice.Text = Convert.ToString(c.Price);
            txtInDate.Text = ""; //can't access inDate?
        }
    }
}

您需要单步执行InDate的列表才能获得该值。

答案 1 :(得分:0)

你没有粘贴你的CellPhoneList类(或者它只是一个List?),但肯定是使用CellPhone而不是InDate。 将SearchItemOnList()更改为

private void SearchItemOnList()
{
   foreach (InDate c in newList)
    {                
        if (c.IEMI == txtSearchIEMI.Text)
        {
            txtDesciption.Text = c.Description;
            txtInPrice.Text = Convert.ToString(c.Price);
            txtInDate.Text = ""; //can't access inDate?
        }
    }
}