Reputation: 2172
I'm inside an anonymous inner class and wanting to reference the instance of the outside class. In Java it can be referenced using .this
preceded by the class name to resolve ambiguity. Like: MainActivity.this
. How can I achieve the same in Kotlin? Because the compiler is complaining 'expression 'this' cannot be a selector(occur after a dot)'
when I do that. Thanks!
Upvotes: 27
Views: 6479
Reputation: 691765
You can use this@MainActivity
to reference the outer class instance.
Tip: I couldn't remember the syntax either, so I just wrote a simple example in Java and asked IntelliJ to convert the class to Kotlin to find the answer.
Upvotes: 47