473,396 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

referencing an object

Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick event.

I do not want to declare myPlayer's color as static. I want to solve this
with references but how?

Any help would be appriciated.
Nov 16 '05 #1
7 1363
Hi,

Could you give more details what you want to do
with that player's color?

As i understand, You want change a color of player
on timer's tick... but:
- do you want to do a "coloured flash" efect on player?
- do you want to restore original color after this "flashing"?

Cheers!

Marcin
Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick event.

I do not want to declare myPlayer's color as static. I want to solve this
with references but how?

Any help would be appriciated.

Nov 16 '05 #2
Hello

Make the myTimer event handler an instance member of the Player class.

myTimer.Tick += new EventHandler(myPlayer.ChangeColor);

class Player
{
// other Player class members
public void ChangeColor(object sender, EventArgs args) {
this.Color = Colors.White;
}
}

Best regards,
Sherif

"Arda Coskun" <a@a.com> wrote in message
news:#U**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick event.
I do not want to declare myPlayer's color as static. I want to solve this
with references but how?

Any help would be appriciated.

Nov 16 '05 #3
Hi Marcin,

I want to change the original color of myPlayer from Bonus class.

"Marcin Grzębski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:ce**********@atlantis.news.tpi.pl...
Hi,

Could you give more details what you want to do
with that player's color?

As i understand, You want change a color of player
on timer's tick... but:
- do you want to do a "coloured flash" efect on player?
- do you want to restore original color after this "flashing"?

Cheers!

Marcin
Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick event.
I do not want to declare myPlayer's color as static. I want to solve this with references but how?

Any help would be appriciated.

Nov 16 '05 #4
Hi,

Then i can recomand to do it on:
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

e.g. you can do it in a body of property "Player":

public class Bonus {
// ...
private Player myPlayer;

public Player Player {
get {
return myPlayer;
}
set {
if( myPlayer!=value ) {
if( myPlayer!=null ) {
// stop timer
}
myPlayer=value;
if( myPlayer!=null ) {
// start timer
}
}
}
}

private timer_TickHandler(object sender, EventArgs e) {
myPlayer.Color = anyColor; // or generated by random
}

// ...
}
Hi Marcin,

I want to change the original color of myPlayer from Bonus class.

"Marcin Grzębski" <mg*******@taxussi.no.com.spam.pl> wrote in message
news:ce**********@atlantis.news.tpi.pl...
Hi,

Could you give more details what you want to do
with that player's color?

As i understand, You want change a color of player
on timer's tick... but:
- do you want to do a "coloured flash" efect on player?
- do you want to restore original color after this "flashing"?

Cheers!

Marcin

Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick


event.
I do not want to declare myPlayer's color as static. I want to solve
this
with references but how?

Any help would be appriciated.


Nov 16 '05 #5
Thank you Sherif for kind reply,

I wanted to mean, here my Bonus class.

public class Bonus
{
Timer bonusTimer;
const int BONUS_TIME_MAX = 5;
Player tempPlayer

public Bonus(Player myPlayer)
{
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

public void setProperties(ref Player myPlayer)
{
tempPlayer = myPlayer
tempPlayer.ChangeColor(Color.White);
}

public void setPropertiesBack(Player tempPlayer)
{
tempPlayer.ChangeColor(Color.Black);
bonusTimer.Enabled = false;
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
setPropertiesBack(tempPlayer);
}
}
}

In Game class;

I'm creating a bonus,

Player playerOne = new Player(Color.Red);
Bonus myBonus = new Bonus(playerOne);

When user gets bonus, I want just invoke setProperties(playerOne method and
myBonus object will change color of playerOne to white first and after 5
seconds to black.

How can I do this?

Thanks.
"Sherif ElMetainy" <el*************@wayout.net.NOSPAM> wrote in message
news:ua**************@TK2MSFTNGP10.phx.gbl...
Hello

Make the myTimer event handler an instance member of the Player class.

myTimer.Tick += new EventHandler(myPlayer.ChangeColor);

class Player
{
// other Player class members
public void ChangeColor(object sender, EventArgs args) {
this.Color = Colors.White;
}
}

Best regards,
Sherif

"Arda Coskun" <a@a.com> wrote in message
news:#U**************@TK2MSFTNGP12.phx.gbl...
Hi,

I have a problem with references in C#.

I have three classes; Game class, Player class and Bonus class.

In Game class,

I have created a Player object and named it myPlayer and set myPlayer's
color to White.
I have created a Bonus object and named it myBonus and send myPlayer to
myBonus as a parameter.

In Bonus class,

I have created a Timer object and named it myTimer.
The thing I want to do is change myPlayer's color from myTimer's Tick

event.

I do not want to declare myPlayer's color as static. I want to solve this with references but how?

Any help would be appriciated.


Nov 16 '05 #6
Hi,

<snip>
I wanted to mean, here my Bonus class.

public class Bonus
{
Timer bonusTimer;
const int BONUS_TIME_MAX = 5;
Player tempPlayer
// was missing
int seconds;
public Bonus(Player myPlayer)
{
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

public void setProperties(ref Player myPlayer)
{
tempPlayer = myPlayer
tempPlayer.ChangeColor(Color.White);
}
If Player is a class... then "ref" is not welcome here.
A Objects (class instances) are passed by reference,
so you don't need to pass the references to object
variables.
public void setPropertiesBack(Player tempPlayer)
{
tempPlayer.ChangeColor(Color.Black);
bonusTimer.Enabled = false;
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
setPropertiesBack(tempPlayer);
// In this case you can easily turn the timer off
// because after this line player's color will be
// BLACK
bonusTimer.Enabled=false;
}
}
}
When user gets bonus, I want just invoke setProperties(playerOne method and
myBonus object will change color of playerOne to white first and after 5
seconds to black.


Do You want that color to change after 5 next seconds or to stay black?

Cheers!

Marcin
Nov 16 '05 #7
Hi Arda,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to change the color for a
player object after certain interval. If there is any misunderstanding,
please feel free to let me know.

Base on the code you have provided, I think we can make some changes to the
constructor and the Tick event handler.

public Bonus(Player myPlayer)
{
this.tempPlayer = myPlayer;
bonusTimer = new Timer();
bonusTimer.Enabled = true;
bonusTimer.Interval = 1000;
seconds = 0;
isBonusFinished = false;
this.bonusTimer.Tick += new
System.EventHandler(this.bonusTimer_Tick);
}

private void bonusTimer_Tick(object sender, System.EventArgs e)
{
if(++seconds == BONUS_TIME_MAX)
{
this.tempPlayer.ChangeColor(Color.White);
}
}

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." +...
11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
10
by: Macka | last post by:
A few pieces of information first: * I have a class called Folder which represents a row of data in a database table. The data access side of things is not an issue. * The table has a parent...
12
by: Mark Broadbent | last post by:
Hi guys, just going through remoting at the moment and a couple of questions relating to .net in general has surfaced. Firstly I have seen in the designer that for the namespace and many of its...
16
by: PromisedOyster | last post by:
Hi I have a situation where I want to use circular referencing. I have cut down the example for demonstration purposes. Say we have one executable (main.exe) and two DLLS (A1.dll and A2.dll)....
9
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of...
21
by: cmd | last post by:
I have code in the OnExit event of a control on a subform. The code works properly in this instance. If, however, I put the same code in the OnExit event of a control on a Tab Control of a main...
2
by: HankD | last post by:
Hi, I am having a problem with instantiating two custom objects so they DO NOT point to the same memory location. What is happening is that changes I am making to my object1 are changing object2. I...
11
by: ozTinker | last post by:
I'm sure this shouldn't be too difficult, but I lack familiarity with the MS object model. Suppose I have a table "Purchase_Orders" and a form "TEMP" which I am using to look up a customer's...
2
by: Andrus | last post by:
I need compile in-memory assembly which references to other in-memory assembly. Compiling second assembly fails with error Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0, Culture=neutral,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.