c# - How do I access higher level variables/objects in a child object? -


something haven't ever figured out how in c# following:

say have 2 classes: game.cs, , player.cs.

i find belongs game.cs must passed on through arguments of function belongs player.cs. example, if had sound effect loaded in game, , player child of game, , player has function "playsoundeffect()", way player access sound if pass through arguments: "playsoundeffect(soundeffect sound)"

what want know this:

  1. how can access object(s) without including them argument?
  2. why can't access them so: "game.sound", though sound in game?

just make global variables in game class static - meaning, aren't part of game instance, , instead globally accessible.

eg, in game.cs
soundeffect sound;
becomes
static soundeffect sound;

now player class can do
game.sound.play();
or whatever needs.


Comments

Popular posts from this blog

How to mention the localhost in android -

php - Calling a template part from a post -

c# - String.format() DateTime With Arabic culture -