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

Assign variable value to object name?  RSS feed

 
Emile Ghazzawi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Is it possible to assign a variable value to the name of a new object. For example:



I want the newTimer object to be called newTimer4, is there a way to include the variable value in the name declaration. I have tried all sorts but cannot seem to do it!

Thanks in advance for any help!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't directly. You could try to associate the name you want, with the actual instance, by storing them in a Map<String, Object>
 
Iain Rodgers
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you be happy to just create an array of objects? That way you can reference them by number.
 
Eric Daly
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is no way to do this. But whatever reason you have for doing this, there is another way. It looks to me like a simple case where you need for example 10 different objects that all have the same name, so you want to automatically add 1 through 10 to the name of that variable:

But what you really need is an array. It actually almost looks the same, but is a much better approach than creating a new variable for each element.

That makes 4 timers. Then you just use newTimer[x] to access whichever timer you want. Make sure you are aware of your boundaries. Arrays start at 0, so that means you have newTimer[0] through newTimer[3] (this is actually the 4th timer). Trying to access newTimer[4] will give you an error.
 
Emile Ghazzawi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's nearly right, but the problem is not a case of how many timers i want. I am trying to name the timer according to an ID field in a database so i do not know how many i will need in advance.

For example if the ID field is 99 i want it's associated timer to be called timer99, that way i can easily link the two objects in my code and cancel the timer if needed or generate new timers on the fly when new IDs are created. Does that make any sense?

I appreciate your help with this, i just though there might be an easy way i was completely overlooking, but it seems this might not be the case.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If all your timer numbers are consecutive and start at zero, then you could use an Arraylist instead of an array.

If your timer numbers are not consecutive, then you will need to use a HashMap using an Integer as the key.
e.g.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!