| Author |
Message |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Fri Dec 17, 2010 2:28 pm Post subject:
string issue |
 |
|
Hi,
It's possible to construct a string and pass this string as the instance name of a class?
f.e.
we have a class:
ourClass inst1,inst22;
and then access the class with a function like:
fun void test(string instanceNumber)
{
"inst"+"instanceNumber".doSomething(int whatever);
}
the idea is to access all the instances with the same function.
Every help will be much appreciated! |
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Fri Dec 17, 2010 6:20 pm Post subject:
|
 |
|
Hi Jirko,
you can build this functionality with an associative array:
| Code: | class A
{
int x;
fun void print()
{
<<< x >>>;
}
}
A keeper[0];
new A @=> keeper["a1"];
new A @=> keeper["a2"];
5 => keeper["a1"].x;
6 => keeper["a2"].x;
fun void test( string no )
{
keeper["a" + no].print();
}
//testing
test( "1" );
test( "2" ); |
Cheers,
Andreas |
|
|
Back to top
|
|
 |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Sat Dec 18, 2010 5:25 am Post subject:
|
 |
|
| Thanks a lot Andreas. Works perfect in my class!! |
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Sat Dec 18, 2010 10:17 am Post subject:
|
 |
|
I'm glad that it worked for you.
Just in case you would ever want to access a multi-dimensional array with
associative indices ( and IMHO there is a good reason to do this because of
the flexibility it offers when you want to construct virtual patchbays, presets'
mechanisms, etc ) there's a workaround in the syntax.So the above example
with 2 groups of 2 objects each, would become:
| Code: | class A
{
int x;
fun void print()
{
<<< x >>>;
}
}
A keeper[0][2];
new A[2] @=> keeper["group1"];
new A[2] @=> keeper["group2"];
5 => (keeper["group1"])[0].x; // notice the change in syntax
6 => (keeper["group1"])[1].x;
7 => (keeper["group2"])[0].x; // use parentheses around the
8 => (keeper["group2"])[1].x; // first dimension of the array
fun void test( string no, int index )
{
(keeper["group" + no])[index].print(); // use parantheses again here
}
//testing
test( "1", 0 );
test( "1", 1 );
test( "2", 0 );
test( "2", 1 ); |
This workaround was mentioned in the mailing list back in 2006, and it remains as such. |
|
|
Back to top
|
|
 |
jirko

Joined: Dec 15, 2009 Posts: 59
|
Posted: Tue Dec 21, 2010 5:30 am Post subject:
|
 |
|
Thanks again Andreas for clearing this matter. It's very useful to have this info here in the forum.
Cheers! |
|
|
Back to top
|
|
 |
|