Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 mattkw80   Members   -  Reputation: 102

Like
0Likes
Like

Posted 15 April 2012 - 03:55 PM

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.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9497

Like
0Likes
Like

Posted 15 April 2012 - 04:29 PM

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.

#3 mattkw80   Members   -  Reputation: 102

Like
0Likes
Like

Posted 15 April 2012 - 04:50 PM

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.

#4 SiCrane   Moderators   -  Reputation: 9497

Like
0Likes
Like

Posted 15 April 2012 - 04:58 PM

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).

#5 Nypyren   Crossbones+   -  Reputation: 4021

Like
0Likes
Like

Posted 15 April 2012 - 05:09 PM

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)

#6 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 15 April 2012 - 08:30 PM

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

#7 Postie   Members   -  Reputation: 884

Like
0Likes
Like

Posted 15 April 2012 - 10:44 PM

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?
Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler

#8 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 16 April 2012 - 06:30 AM


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?


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.

#9 Postie   Members   -  Reputation: 884

Like
0Likes
Like

Posted 16 April 2012 - 06:38 AM

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.
Currently working on an open world survival RPG - For info check out my Development blog: ByteWrangler

#10 mattkw80   Members   -  Reputation: 102

Like
0Likes
Like

Posted 16 April 2012 - 08:35 PM

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)

#11 mattkw80   Members   -  Reputation: 102

Like
0Likes
Like

Posted 16 April 2012 - 08:37 PM

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)



I think my big problem is - VB.NET let me get away with alot of bad tricks to make this work. C# seems to be pretty strict - OOP or nothing.
It's going to take me a while to re-learn the proper way.

#12 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 16 April 2012 - 09:04 PM

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)


The InventoryForm has to see the player's inventory, not the player. I would have an inventory class, that the player has as part of it. Then when you create the InventoryForm you use the Form constructor to pass the player's inventory. InventoryForm inform = new InventoryForm(player.Inventory);

So why do this? Because then you can have other things with inventories. Merchants, monsters, chests, doesn't matter. They could all be handled by a single InventoryForm.

#13 mattkw80   Members   -  Reputation: 102

Like
0Likes
Like

Posted 16 April 2012 - 11:07 PM

Not a bad idea at all. For the moment, I need to keep it really simple - but long term - that is where I will go with it.

#14 TheTroll   Members   -  Reputation: 882

Like
0Likes
Like

Posted 17 April 2012 - 07:24 AM

Not a bad idea at all. For the moment, I need to keep it really simple - but long term - that is where I will go with it.


Since you are learning, I would suggest you do it the "right" way when ever possible. It take a little bit more work but you will learn more that way.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS