4
Answers

abstract class and serialization

k

k

16y
8.5k
1
I am making a small game to illustrate abstract class, abstract method and static variables in C#. I try to serialize properties of Tribe Class through MainHouse class that inherited from Tribe Class.

But problem is: I cannot serialize properties of Tribe class because they are declared as static

In this case, How can I serialize properties in Tribe class?

Thanks in advance.

abstract public class Tribe
    {        
        static public int sum_people = 5;
        static public int sum_lumber = 200;
        abstract public void BearChild();
    }
    public class MainHouse : Tribe
    {
        public override void BearChild()
        {
            if (Tribe.sum_lumber > 50)
            {
                Tribe.sum_lumber -= 50;
                Tribe.sum_people += 1;              
            }

        }
    }
  public partial class Form1 : Form
    {
        MainHouse mainhouse = new MainHouse();
        public Form1()
        {
            InitializeComponent();
        }

        private void Save_Click(object sender, EventArgs e)
        {
            TextWriter tr = new StreamWriter("Tribe.xml");
            XmlSerializer sr = new XmlSerializer(typeof(MainHouse));
            sr.Serialize(tr, mainhouse);
            tr.Close();
            MessageBox.Show("serialized!");
        }
    }
Answers (4)