• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to implement a variable into another class?  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, I would like to implement a variable in a class that is used in another class, how can I do that?
I have a Mall class and a Customer class I would like to associate the position of the customer that is in the Mall class and also implement the same in the Customer class.
Here is a small part of the code without all the methods.

Thank you
 
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark, Can you be a bit more clear in your query?

Firstly you can make use of variables i.e the instance variables in a class by using the instance of that class. Like for example, to get the name of the Mall you could do mallInstance.getName() where Mall mallInstance = new Mall();
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by describing in simple words what you mean about position. And describe who or what “owns” the position. Also decide who or what moves the customer around the mall. That process will clear your mind about how to implement things and also give you a simple process which is easer to turn into code.

Beware of an object which is referred to from two locations. That would mean its state can be altered from both those locations.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answers, but I would like to know, if for example I decided to store the name in an x and y position in the array of Strings that belongs to object Mall; how can I also store that position in the object Customer while the position could change from both sides?
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I understood that last post up until you said "while the position could change from both sides". What's a "side" in your data design? You haven't mentioned that concept yet anywhere as far as I can see.
 
Ranch Foreman
Posts: 3068
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Nasr wrote:Thanks for your answers, but I would like to know, if for example I decided to store the name in an x and y position in the array of Strings that belongs to object Mall; how can I also store that position in the object Customer while the position could change from both sides?


How can it change "from both sides"?
Do you frequently go to the shops and magically move from one place to another without moving yourself?

You ought to think about Campbell's points. It will help clarify your requirements.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh there was a mistake; I meant when one changes the other does too
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean your Customer object moves around and the Shop objects record which Customers are in them and the Customer objects record which Shop they are in.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you should start by drawing pictures. Draw a diagram with squares on representing the shops and move buttons or counters around your piece of paper to mimic shoppers going from place to place.
 
Mark Nasr
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys I found a way to implement it
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how did you do it? Future generations of CodeRanch readers might find that uesful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!