Home > Software engineering >  Inheritance classes across Form
Inheritance classes across Form

Time:12-12

First of all sorry for my English.

Is there any way how to inheritance class across methods in my Form class? I do have class named Karta.cs and I want to use it in Form1_Load and also in Form1_Click with same data. Now im using karta = new Karta(); in both metods but that doesnt solve my problem because if I change some data in karta thats in Form1_Load and then Im gonna try use karta in karta1_click then those changes are not gonna be there. Is there any way how to insert karta = new Karta(); somewhere up so every method can see use it? Thank you.

namespace Mravenci
{
    public partial class Hra : Form
    {
        public Hra()
        {
            InitializeComponent();
    }

        private void Form1_Load(object sender, EventArgs e)
        {
            Karta karta;
            karta = new Karta();

            karta.NastavKarty(karta.ZamichejKarty());
            karta.ZobrazKartyStart(karta1, karta2, karta3, karta4, karta5, karta6, karta.VratKarty());
        }

        private void karta1_Click(object sender, EventArgs e)
        {
            Karta karta;
            karta = new Karta();

            karta.HrajKartu(1,posledniKarta);
            
        }
    }
}

CodePudding user response:

try this

public partial class Hra : Form
    {
       Karta karta = new Karta();

        public Hra()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
            karta.NastavKarty(karta.ZamichejKarty());
            karta.ZobrazKartyStart(karta1, karta2, karta3, karta4, karta5, karta6, karta.VratKarty());
        }

        private void karta1_Click(object sender, EventArgs e)
        {
          
            karta.HrajKartu(1,posledniKarta);
            
        }
    }
  • Related