0

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:

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.

Hgrbz
  • 185
  • 3
  • 12
koli
  • 3
  • 1
  • Does this answer your question? [Communicate between two windows forms in C#](https://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – JohnG Apr 12 '22 at 20:29
  • You should not be deriving Statistics from Form1, thats a very odd thing to do. – pm100 Apr 12 '22 at 20:30
  • FWIW: it's unusual to have an event handler with a name like ` MainWindow_Load` in a Forms class named `Form1`. That's not what the tools will auto-name the handler? Do you have it wired up correctly? If you put a breakpoint in it, does it get hit? – Flydog57 Apr 12 '22 at 20:49
  • Yes, it's working fine, I have changed the name of the Form1 recently so that's why. – koli Apr 12 '22 at 21:03

1 Answers1

0

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
pm100
  • 48,078
  • 23
  • 82
  • 145
  • Hello, but the code: public static Gamer Play; gives me an error: Error CS0052 Inconsistent Availability: The Gamer field type is less available than the Form1.Player field. – koli Apr 12 '22 at 20:42
  • And thank you for the advice in postscript, I have changed it already. – koli Apr 12 '22 at 20:44
  • @koli did you make the Gamer class public? You didnt show it, not the field, the actual class defintion – pm100 Apr 12 '22 at 20:47