-3

First form's code (Main Menu):

public partial class MainMenu : Form
{
    public class Player
    {
        public string name, curWeapon, wpn1, wpn2, wpn3, wpn4,  wpn5, curArmor, arm1,
                      arm2, arm3, arm4, arm5, activeQuest, curPlace, Gurgonauth = "Gurgonauth",
                      Klebbetoth, Shalthazaar, Khurx, dungeon, dunEnemy, dun1, dun2; 
        public int dmg, armor, maxArmor, strength, endurance, accuracy, luck, hp, maxHp,
                   xp, xpmax, lvl, gold, bank, monstersKilled;
    }

    private void Form1_Load(object sender, EventArgs e) => name.MaxLength = 15;

    public void button1_Click(object sender, EventArgs e)
    {
        var p       = new Player();
        p.name      = name.Text;
        p.hp        = 100;
        p.maxHp     = 100;
        p.gold      = 0;
        p.lvl       = 1;
        p.strength  = 0;
        p.luck      = 0;
        p.xp        = 0;
        p.xpmax     = 100;
        p.curWeapon = "Bare Hands";
        p.curPlace  = p.Gurgonauth;
        p.endurance = 0;
        p.dmg       = 5;
        p.curArmor  = "None";
        p.monstersKilled = 0;
        Overview ovr = new Overview(p);
        ovr.Show(this);
        this.Hide();
    }
}

Second form's code (Overview):

 public partial class Overview : Form
 {
     var p = new MainMenu.Player();
     public Overview(MainMenu.Player p)
     {
         InitializeComponent();
         name.Text   += p.name;
         hp.Text     += p.hp.ToString();
         xp.Text     += p.xp.ToString() + "/" + p.xpmax.ToString();
         level.Text  += p.lvl.ToString();
         curWpn.Text += p.curWeapon;
         curArm.Text += p.curArmor;
         gold.Text   += p.gold.ToString();          
    } // It does what I want it to do

    private void Overview_FormClosing(object sender, FormClosingEventArgs e) => Application.Exit();
    public void button1_Click(object sender, EventArgs e) => p.gold += 10; // Shouldn't this work?
}

The problem is in the second form's code (Overview). As you can see from my comments, it does what I want it to do, but that p.gold += 10 doesn't work. Why doesn't it work? I cannot understand why it doesn't work. What should I write to make it work?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • 5
    what you mean by doesn't work? – Rahul Aug 12 '18 at 21:14
  • 3
    “Doesn’t work” has got to be the most irritating and unhelpful way to report a problem. Try specifying an expected and actual outcome. – John Wu Aug 12 '18 at 21:20
  • 1
    What do you mean by "doesn't work"? What happens when you run your program? What do you want it to do differently? – Code-Apprentice Aug 12 '18 at 21:22
  • @JohnWu Well, pretty obviously, I expected it to increase the gold's value (which is in the class Player) by 10 when I press the button. It doesn't. –  Aug 12 '18 at 21:35
  • 2
    [Duplicate 1](http://stackoverflow.com/questions/3062575/), [duplicate 2](http://stackoverflow.com/questions/7800731/), [duplicate 3](http://stackoverflow.com/questions/17032484/), [duplicate 4](http://stackoverflow.com/questions/17836398/), [duplicate 5](http://stackoverflow.com/questions/25316230/), [duplicate 6](http://stackoverflow.com/questions/29092707/) ... – Dour High Arch Aug 12 '18 at 21:43
  • @DourHighArch Thank you, but none of those has what I need. –  Aug 12 '18 at 21:56
  • 1
    "I expected it to increase the gold's value (which is in the class Player)." No, it isn't. The value of `gold` is held in an *instance* of the class, otherwise known as an object. Maybe the problem is that you have confused the two. I assure you `'p.gold +=10` "works" in that it will indeed increase the value of `p.gold` by 10, for whatever instance `p` represents. – John Wu Aug 12 '18 at 23:01
  • `p.gold += 10;` will increase the value of `p.gold` by 10. Did you debug the code and check? What exact behavior you want from this line of code? Again, what you do mean by `it doesn't work`? – Chetan Aug 13 '18 at 00:52
  • Possible duplicate of [WinForms passing data between Forms](https://stackoverflow.com/questions/29092707/winforms-passing-data-between-forms) – Dour High Arch Mar 20 '19 at 21:58

1 Answers1

1

That's mostly cause p here is an empty instance as in below code

MainMenu.Player p = new MainMenu.Player();

You probably rather just want to assign it like

 MainMenu.Player p = null;
 public Overview(MainMenu.Player p)
 {
     InitializeComponent();
     this.p = p;           // initialize here
     name.Text += p.name;
     hp.Text += p.hp.ToString();
     xp.Text += p.xp.ToString() + "/" + p.xpmax.ToString();
     level.Text += p.lvl.ToString();
     curWpn.Text += p.curWeapon;
     curArm.Text += p.curArmor;
     gold.Text += p.gold.ToString();          
}

BTW, you should consider refactoring your Person class to a separate file rather rather than defining it inside form

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • Um, I think you didn't read the comments in my code. This piece of code works perfectly. The lower piece of code has the problem. –  Aug 12 '18 at 21:45
  • @LoadGame If that piece worked perfectly then you shouldn't post it. It confuses us and we end up reading stuff irrealavent to the problem. You can use sharplab.io to edit the code to be as minimal as it can be so we can duplicate the issue and help you. For example, do you really need all 40 of your variables included, or could you have duplicated the problem with only using one variable? (You only needed one). Also, you have way way way too many variables for one class. You should refactor into much smaller classes and structs. – AustinWBryan Aug 13 '18 at 02:49
  • 1
    @LoadGame The important line of Rahul's answer is `this.p = p; // initialize here`. This will solve your problem. The simple reason is that `p` in `var p = new MainMenu.Player();` has no relation to `p` in `public Overview(MainMenu.Player p)`, so you have to assign one to the other so that they both point to the same instance. – ProgrammingLlama Aug 13 '18 at 02:54