Jump to content

  • Log In with Google      Sign In   
  • Create Account


mattkw80

Member Since 13 Apr 2012
Offline Last Active Oct 29 2012 06:14 PM
-----

Posts I've Made

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

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.

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

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.

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

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)

In Topic: NPC interaction - need help with Game Design

16 April 2012 - 08:31 PM

Sorry guys, thought the Beginners section might have been the place to post this. I'll know for next time.

Thanks for the advice... Zael nailed it I think.... "

separate this logic from the game code (SQL, XML, Lua, somthing)"


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

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.

PARTNERS