3

This is a purely academic question - I have found a getaround easily enough.

While porting a VB.Net class to C#, I came upon the declaration of a field in a class which used the this keyword as a parameter in a new() statement. The compiler said the "Keyword 'this' is not available in the current context' (the VB compiler saw no problem with this state of affairs). I easily got around this by moving the field's initialization to the contructor of the class.

edit: after reading comments, I added the following code block

 
    public class cTransactions
    {
        private List Trans = new List();
        private List Archive = new List();
        private cDDs Debits = new cDDs(this); // complier error 
                                            //Keyword 'this' is not available in the current context
        private string path = Directory.GetCurrentDirectory() + "\";
        private bool dirty = false;
        private int LastID;
        // followed by Property declarations, ctor, methods etc.
        //...
    }
     

However, I cannot find any reference to the keyword 'this' not being available before the execution of a class' constructor (though I may have missed that revelation in the 500+ pages of the language specification). Is this the case or should I be looking some error in one of the lines prior to the field declaration?

  • 1
    there is no "this" keyword in vb.net – MUG4N Sep 22 '12 at 09:28
  • 1
    There *have* been some compiler bugs in this area, actually - although I can't remember the specifics. Perhaps the best thing is to post the offending line of code. – Marc Gravell Sep 22 '12 at 09:55
  • @MUG4N - I failed to explicitly write that VB.Net had no problem with the use of the keyword me in the manner outlined above. – willofirony Sep 22 '12 at 12:34
  • Welcome to SO! I wouldn't bother replying to snipes about reading the language specification etc. If everyone read every piece of documentation they should, SO would be pretty bare! So it's a bit much to criticise an SO beginner for not reading a 500 page document before asking a simple question! – weston Sep 22 '12 at 15:31
  • Can you post the original VB.NET code where you saw this? – Kev Sep 23 '12 at 00:16

1 Answers1

3

Looking at the C# Language specification section 7.6.7:

7.6.7 This access

A this-access is permitted only in the block of an instance constructor, an instance method, or an instance accessor. ... (specifics omitted) ... Use of this in a primary-expression in a context other than the ones listed above is a compile-time error. In particular, it is not possible to refer to this in a static method, a static property accessor, or in a variable-initializer of a field declaration.

Therefore, using it in a variable-initializer in the example above is a compile-time error. To fix it, move the initialization into the constructor.

akton
  • 14,148
  • 3
  • 43
  • 47
  • Thanks for that, Akton. I did read this section but failed to realise that the field declarations are, in fact, outside of the blocks specified. My bad. Thanks again. – willofirony Sep 22 '12 at 15:42