• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simple question..........

 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have the below class, Score:

class Score
{
int gamescore;

public void putscore(int s)
{
gamescore = s;
}

public int getscore()
{
return gamescore;
}
}

as u can see its a very simple class, and this is used to keep track of scores in a game. and everytime i call it from another class(which i have to), for example, s.putscore(3), i expect the variable gamescore to be changed to the value that i last inputed. however once it goes out of putscore the gamescore seems to get resetted to 0. i dont know why!! can anyone help?
 
Simon Baker
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that you are calling getscore() on the same instance of Score that you called putscore() on?

Additionally, you ought to declare gamescore private, and the accessor methods public or protected (whichever is the most restrictive you can get away with).
Also, being pedantic, convention would suggest naming the methods getScore() and putScore(), and the instance variable gameScore. Note the capitalisation.
 
Dominic Steng�rd
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, from my point of view thats impossible, at least from looking at the code you just presented. One thought though, could it be that the instance you are using to call getScore() on is a newly created one or that its another instance object than the one you called putScore() on?

Because when a new Score class is created the default value of gamescore will be 0.
[ October 06, 2004: Message edited by: Dominic Steng�rd ]
 
Nigel Browne
Ranch Hand
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You class works fine, are you sure that you are not creating a new instance of Score each time you call it ? Check out the code below
If you want to create one and only one instance of score, you will need to apply the singleton pattern. As shown below
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tan kian:

[ snip... ]

as u can see its a very simple class, and this is used to keep track of scores in a game. and everytime i call it from another class(which i have to), for example, s.putscore(3), i expect the variable gamescore to be changed to the value that i last inputed. however once it goes out of putscore the gamescore seems to get resetted to 0. i dont know why!! can anyone help?


Can you please post the exact code that produces the behavior described here? As other's have mentioned, your class works fine if you use it correctly. I suspect the problem is with your other code, not this class.

Layne
 
Rikko Verrijzer
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

another possibility, one that is only possible when you 'cleaned' the class before posting it here. Is that you have another variable declared with the same name inside your method putscore(int). By assigning the value to gamescore you actually asigns the value to a local variable (with a scope limited to the method putscore(int)), instead of the global (class member) variable. This results in keeping the global variable gamescore at 0.

Hope this helps?
Rikko
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i cant really post my code, cause it involves about 3 files, and its gonna take up lots of space.......anyway i'll try the suggestions u guys gave and i'll get back here again.
 
tan kian
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i found out the prob. as i said, my whole code involves 3 files, and i had created the Score() in one file, and created new instances of it in the current AND another file. so i used Nigel's method of Instance and it works fine. =) thanks all!!
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by tan kian:
i cant really post my code, cause it involves about 3 files, and its gonna take up lots of space.......anyway i'll try the suggestions u guys gave and i'll get back here again.


I'm sorry I should have been more specific. I was only requesting the portion of the code that does what you described in words in your original post. I doubt that we needed to see the whole program, but we did need to see more of the code than what you posted.

Anway, it looks like you've figured it out anyway. Good job and Keep Coding!

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic