Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using object in different class  RSS feed

 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there, another concept Im struggling with...







I have methods defined in a Mesh class. I want to use these methods as the scrollbar is adjusted - (i.e. within the ScrollbarListener class - geo.someMethod() ) However geo is inialised in the MeshTesterGLSpace class. How can I use this object (geo) in the ScrollbarListener class?
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ScrollListener, being an inner class of MeshTester, can invoke any method on its enclosing object, even private methods:

Therefore, if you can write a MeshTester method that does what you want, then you can call it from ScrollListener.
 
Jackie Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm...

I see what you are saying. But I have a 'Mesh' object intialised in the MeshTesterGLSpace class called 'geo'.

I have a seperate Mesh class with methods.

I would like to use the object geo in ScrollListener (i.e geo.getVertices(); ). But because the Mesh object 'geo' has not been initalised in the MeshTester class I cannot use it here.

Hope I've explained this ok.

How do I get around it?

Thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You metioned that field geo has not been initialised. Why not? It's hard to send a message to an object that doesn't exist
 
Vinayak patil
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may help:

Have a MeshTesterGLSpace instance (myMeshTesterGLSpace) in the MeshTester Class.

Call the method that initializes the geo object:
myMeshTesterGLSpace.initMesh()

Then you can use the geo object as

myMeshTesterGLSpace.geo

-Vinayak
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, what's the relationship between MeshTester and MeshTesterGLSpace?
1. Does one have an instance of the other as a field?
2. Is one passed the other in some method?
3. Is one created locally in the method of the other?
There's many ways to get objects to send messages to each other.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!