4

This should be simple. I have a custom controller like so

    public class MyClass {
    .
    .
    .
    public class MyType{
      String a;
      Integer b;
      List<Account> c;
    }
    .
    .
    .
    }

Now I am trying to instantiate MyType object from a controller extension, like so:

public class MyClassExtension {

    public MyClassExtension(MyClass controller) {
        // This is where I need help
        controller.MyType myTypeObj = new controller.MyType();
    }

How do I do this? A couple of syntaxes I tried resulted in errors. TIA.

1
  • I get Error: Compile Error: Invalid type: controller.MyType at line 4 column 42. Something is definitely up... the code is too long and complex to paste here. Feb 23, 2015 at 18:36

1 Answer 1

9

To instantiate a subclass you would need to prefix the parent class name, not its variable reference. Try this.

MyClass.MyType myTypeObj = new MyClass.MyType();

3
  • Good catch. I initially missed that. Sometimes the easiest solution is the right one!
    – sfdcfox
    Feb 23, 2015 at 18:35
  • You guys are awesome. Actually, more than that. Thanks a ton! Feb 23, 2015 at 18:39
  • It possible to miss the obvious fact, however simple/basic that seems, at times :) Feb 23, 2015 at 19:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .