Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use created Instance on all forms / How to make my instance public C# windowsforms

Hello it is probably easy question for you, I’m a beginner and I’m making my own simple game and I want to use a Class:Gamer, which I want to initialize in MainWindow(Form1.cs) from a save file. From then, I want to use it on another Forms aswell, but somehow I can’t make the instance go public.

Could you tell me what I’m doing wrong? Or is there another way how to solve this?
Thank you 🙂

Code on Form1:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;
using System.IO;

namespace THE_GAME
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static Gamer Player;
        private void MainWindow_Load(object sender, EventArgs e)
        {
            //load from savefile   lvl;hp;money;gun;armor,name
            string allData = File.ReadAllText("../../saveFile/save.txt");
            string[] dataFromSave = new string[5];
            dataFromSave = allData.Split(';');

            Player = new Gamer(dataFromSave[0], dataFromSave[1], dataFromSave[2], dataFromSave[3], dataFromSave[4], dataFromSave[5]);
        }
    }
}

Code on secondForm2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Text;

namespace THE_GAME
{
    public partial class Statistics : Form1
    {
        public Statistics()
        {
            InitializeComponent();
        }
        private void Statistics_Load(object sender, EventArgs e)
        {
            //labels stats
            labelName.Text = Form1.Player.GetName();
            labelHealth.Text = Form1.Player.GetHealth().ToString();
            labelMoney.Text = Form1.Player.GetMoney().ToString();

        }
        private void buttonBack_Click(object sender, EventArgs e)
        {
            MainMenu menu = new MainMenu();
            menu.Show();
            this.Close();
        }
    }
}

Thank you for your time.

>Solution :

To get at the Gamers Player object from a different Form just do

Form1.Player;

ie

var nam =  Form1.Player.Name;
Form1.Player.Die();

etc

PS As I said in a comment – its extremely odd to dereive a form of yours from another one of your forms. Like this

 public partial class Statistics : Form1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading