-2

我在完成这项工作时遇到了一个重大问题。我需要做的就是让我的阵列显示。

namespace OOP_3
{
    public partial class Add_Child : Form
    {
        public Add_Child()
        {
            InitializeComponent();
        }

        private void btnAddChild_Click(object sender, EventArgs e)
        {
            Mother m = new Mother();
            m.MyChildren = new Child[3];

            int ID = int.Parse(txtID.Text);
            string FNAME = txtFname.Text;
            string LName = txtLname.Text;
            DateTime DOB = DateTime.Parse(txtDob.Text);
            //Add children

            label5.Text = m.GetMyChildrenDetails();
            if (addtoarray(m,ID,FNAME,LName,DOB) == true)
            {
                MessageBox.Show("added", "Add Child");
            }
            else
            {
                MessageBox.Show("cannot add", "Child add - full");
            }
        }

        public bool addtoarray(Mother m, int ID, string FNAME, string LName,DateTime DOB)
        {
            for (int i = 0; i < m.MyChildren.Length; i++)
            {
                if (m.MyChildren[i]== null)
                {
                    m.MyChildren[i] = new Child(m); //See comment below
                    m.MyChildren[i].ChildId = ID;
                    m.MyChildren[i].FirstName = FNAME;
                    m.MyChildren[i].LastName = LName;
                    m.MyChildren[i].DateOfBirth = DOB;
                    return true;
                }
            }
            return false;
        }
    }
}

来自代码评论:这一行破坏了堆中的所有内容并重新创建了数组,导致我的值不会出现在我的 label5.text 中我一直在思考研究了几个小时,我认为我要么发疯,要么只是编码的菜鸟哪个IAM :)请一些帮助会很好:)....如果需要,我将发布我的班级和主要表格:)

public class Mother
{
    //Fields
    private int motherId;
    private string firstName;
    private string lastName;
    private string mobile;
    private Child[] myChildren; //mother "has a" many children

    //props
    public Child[] MyChildren
    {
        get { return myChildren; }
        set { myChildren = value; }
    }

    public string Mobile
    {
        get { return mobile; }
        set { mobile = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public int MotherId
    {
        get { return motherId; }
        set { motherId = value; }
    }

    //constructors

    //methods
    //Get Mother Details
    public override string ToString()
    {
        return motherId + ", " + firstName + ", " + lastName + ", " + mobile;
    }

    //AddChild
    public bool AddChild(Child myChild)
    {
        for (int i = 0; i < myChildren.Length; i++)
        {
            if (myChildren[i] != null)
            {
                myChildren[i] = myChild;
                return true;
            }
        }

        return false;
    }

    //GetMyChildrenDetails
    public string GetMyChildrenDetails()
    {
        string msg = "";
        for (int i = 0; i < myChildren.Length; i++)
        {
            if (myChildren[i] != null)
            {
                msg += "\n" + myChildren[i];
            }
        }
        return msg;
    }

public class Child
{
    //fields
    private int childId;
    private string firstName;
    private string lastName;
    private DateTime dateOfBirth;
    private Mother myMother; //child "has a" mother

    //props
    public Mother MyMother
    {
        get { return myMother; }
        set { myMother = value; }
    }

    public DateTime DateOfBirth
    {
        get { return dateOfBirth; }
        set { dateOfBirth = value; }
    }

    public string LastName
    {
        get { return lastName; }
        set { lastName = value; }
    }

    public string FirstName
    {
        get { return firstName; }
        set { firstName = value; }
    }

    public int ChildId
    {
        get { return childId; }
        set { childId = value; }
    }

    //constructors
    //Child cannot be created without a mother
    public Child(Mother myMother)
    {
        this.myMother = myMother;
    }
    //Child cannot be created without a mother
    public Child(Mother myMother, int childId)
    {
        this.myMother = myMother;
        this.childId = childId;
    }

    //methods
    //Get Child Details
    public override string ToString()
    {
        return childId + ",  " + firstName + ", " + lastName + ", " + dateOfBirth;
    }

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void btnRegister_Click(object sender, EventArgs e)
    {

    }

    private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    }

    private void closeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }

    private void BtnAddChild_Click(object sender, EventArgs e)
    {
        Add_Child child = new Add_Child();
        child.Show();
    }

    private void btnRegister_Click_1(object sender, EventArgs e)
    {
        //create a mother object
        Mother m = new Mother();
        m.MotherId = int.Parse(txtID.Text);
        m.FirstName = txtFname.Text;
        m.LastName = txtLname.Text;
        m.Mobile = txtMobile.Text;
4

2 回答 2

0

Giving the new child a reference to its parent object is not the cause of your issue. In summary, you state:

"to have values that will not show up in my label5.text "

This would indicate that it is either your binding that has an issue, you have not implemented INotifyPropertyChanged, or your UI updating mechanism is not working (if you are not using binding). As you haven't stated what you are using for the UI, that is about as much as I can help...

于 2012-10-27T02:29:27.087 回答
0

我的建议,你可以用List数组代替,因为不是所有的妈妈都有三个孩子。示例代码:

public static class Program
{
    static void Main(string[] args)
    {
        Mother mother = new Mother {
            FirstName = "M First", LastName = "M Last", Contact = "225632655"
        };

        //Add dependents
        mother.AddChild(new Child{ChildID = 1, FirstName = "Child FirstName 1", LastName = "Child LastName 1"});
        mother.AddChild(new Child{ChildID = 2, FirstName = "Child FirstName 2", LastName = "Child LastName 2"});
        mother.AddChild(new Child{ChildID = 3, FirstName = "Child FirstName 3", LastName = "Child LastName 3"});

        Console.WriteLine(mother);
        //List all the mother dependents
        foreach(Child c in mother.Children)
        {
            Console.WriteLine(c);
        }
        Console.ReadKey();
    }
}

class Mother
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public string Contact {get; set;}
    private List<Child> _child;
    public Mother()
    {
        _child = new List<Child>();
    }

    public void AddChild(Child child)
    {
        _child.Add(child);
    }

    public List<Child> Children
    {
        get { return _child; }
    }

    public override string ToString()
    {
        return string.Format("{0}, {1} ({2})", LastName, FirstName, Contact);
    }
}

class Child
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public int ChildID {get; set;}

    public override string  ToString()
    {
        return string.Format("{0} {1}, {2}", ChildID, LastName, FirstName);
    }
}

我希望这个能帮上忙。

于 2012-10-27T02:39:09.073 回答