Jump to content
  • Advertisement
Sign in to follow this  
mattkw80

C# - Question about OOP , using objects across multiple forms

This topic is 2283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble understanding OOP in general, but specifically in C#.

If I have a player object created on my main form... (Form1), how do I talk to that
same player object in another form? ie: FormInventory.

Hopefully I've worded all that ok.

Share this post


Link to post
Share on other sites
Advertisement
Pass a handle to the player object to the FormInventory object. This can be part of the FormInventory's constructor or another function or even a property or variable.

Share this post


Link to post
Share on other sites
I've never heard of handles, maybe this is why I'm having trouble doing this.

I will try to to figure this out, thanks SiCrane.

Share this post


Link to post
Share on other sites
Sorry, in C# they're called references. I think you're overthinking this. It's really as simple as something like:

Player player = new Player;
FormInventory inventory = new FormInventory;
inventory.SetPlayer(player);

or something similar (assuming Player is a reference type).

Share this post


Link to post
Share on other sites
In C#, any variable with a 'reference type' (i.e. the variable's type is a class or string) could be considered 'handles'.

In C#, the code inside a method can 'see':

  • Variables that got passed to the method.
  • Variables that got created in the method.
  • Member variables of the class that contains the method.
  • Public Static (or "global") variables inside any class.

    There are several possible ways to make your two classes 'share' data:

    • When you 'new' a class, you can pass data via the constructor.
    • If you have a reference to a class, you can assign to any of its public fields or public property setters.
    • If you have a reference to a class, you can call any of its public methods and pass your data as an argument (SiCrane's example).
    • You can make your data 'public static' and then any class can access it directly (try to avoid using this unless you have to).

      Examples:

      FormInventory inventory = new FormInventory(player); // Passing it via the constructor.

      inventory.Player = player; // Using a field/property setter.

      inventory.SetPlayer(player); // Passing it to a method.

      var player = Form1.Player; // Accessing it via a global (public static) field/property. (This code would be written in FormInventory's methods)

Share this post


Link to post
Share on other sites

What I don't understand is why your InventoryForm needs to access the player Object?


At a guess, I'd say it would be to retrieve the contents of the player's inventory and other parameters like carrying capacity?

Share this post


Link to post
Share on other sites

[quote name='TheTroll' timestamp='1334543437' post='4931615']
What I don't understand is why your InventoryForm needs to access the player Object?


At a guess, I'd say it would be to retrieve the contents of the player's inventory and other parameters like carrying capacity?
[/quote]

That would mean the player object would need to access the inventory, not the other way around. I can't see a reason inventory needs to access the player.

Share this post


Link to post
Share on other sites
Hypothetically, if InventoryForm was also used to display other entity's inventory, such as a shop keeper or someone you're pick pocketing, you could pass a reference to the person whose inventory you're going to be displaying to the form and have it sort it out.

Share this post


Link to post
Share on other sites
Thanks for all the reply's, I'll read these through and make sure I understand.

And yeah... the Inventory needs to see the player's inventory, so it can display the player's inventory. I've done the inventory this way....

player.hasSword = true
player.hasShield = false
player.qtyFood = 5
player.gold = 5

(The inventory item's are variables in the player class - for simplicity sake)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!