8

I have the following initialization of a constructor:

public partial class WizardPage1 : WizardPage
{
    public WizardPage1()
        : base(0, getLocalizedString(this.GetType(), "PageTitle"))
    {
    }
}

where

public static string getLocalizedString(Type type, string strResID)
{
}

but this.GetType() part causes the following error:

error CS0027: Keyword 'this' is not available in the current context

Any idea how to resolve it?

c00000fd
  • 20,994
  • 29
  • 177
  • 400
  • Have a look at: http://stackoverflow.com/questions/2446413/keyword-this-me-is-not-available-calling-the-base-constructor – miltonb Sep 26 '13 at 03:50
  • @miltonb: I'm not sure what am I missing? One of the posters there simply moved `this` type out of the base constructor. I can't do that, or I'd rather go with some other method to get the type of `this` class? – c00000fd Sep 26 '13 at 03:58

3 Answers3

11

The 'this' keyword refers to the current instance of the class. In the constructor, you don't have access to the instance because you are about to create one... So try below:

public partial class WizardPage1 : WizardPage
{
    public WizardPage1()
        : base(0, getLocalizedString(typeof(WizardPage1), "PageTitle"))
    {
    }
}
Alex
  • 192
  • 3
  • 13
Damith
  • 62,401
  • 13
  • 102
  • 153
  • Is there any way to do it as `typeof(this)`? I'm afraid I may miss changing `typeof(WizardPage1)` part in a "template" class that may lead to trouble... – c00000fd Sep 26 '13 at 04:00
  • 1
    @c00000fd - A possible alternative could be to use generics or reflection in the base class so it can glean what you mean automagically - but there's no bulletproof solution. If you can't trust your developers (including yourself) then plan for mistakes and hours of painful debugging. – M.Babcock Sep 26 '13 at 04:06
  • @c00000fd can you update the question with base constructor code? – Damith Sep 26 '13 at 04:14
0

The this keyword refers to the current instance of a class, however as you are calling this within the constructor, you don't yet have an instance to refer to (as it is being constructed).

Perhaps an alternate solution would be to have a property in your base class that you can override in the child class. E.g.

public class WizardPage
{
   public virtual string PageTitle { get; }
   ...
}

public class WizardPage1 : WizardPage
{
   public override string PageTitle
   {
      get 
      {
          return getLocalizedString(this.GetType(), "PageTitle");
      } 
   }
}

The key thing here is that you are calling GetType() when you already have an instance of the object.

codemonkeh
  • 2,054
  • 1
  • 20
  • 36
0

@Damith is correct on why this doesn't work but one way to deal with this simpler could be to (ignoring implementation specifics):

public abstract class WizardPage
{
    // Replace or override existing constructor with this
    public WizardPage(int unknownInt, Type currentType, string str)
    {
        if (currentType == null)
            currentType = System.Reflection.MethodBase()
                              .GetCurrentMethod().GetType();

        var localString = getLocalizedString(currentType, str);

        // Existing logic here
    }
}

And change your child class to:

public partial class WizardPage1 : WizardPage
{
    public WizardPage1()
        : base(0, this.GetType(), "PageTitle")
    {
    }
}

Unfortunately, this approach require adding a layer of abstraction if you don't have access to the code of the base class.

Community
  • 1
  • 1
M.Babcock
  • 18,753
  • 6
  • 54
  • 84