Thursday, June 21, 2012

The name 'Request' does not exist in the current context

I receive this message today in Visual Studio when trying to get at the Request object of a web page and it stumped me for a bit, since it did not represent what the actually problem was.  "The name 'Request' does not exist in the current context"


I had this code in the Page_Load event:
     if (Request("myFlag")=="1") DoSomething();


Since I had seen similar code elsewhere and I was in a Page_Load I was pretty sure I had the Request object available to me.


I tried going at the base page.  I tried using "this".  None of them worked.


As I flipped things this way and that I had something pop up that made me realize I had wrapped the parm of the Request object in parenthesis instead of brackets.  Once I changed that it fixed the problem.
    if (Request["myFlag"]=="1") DoSomething();


The error message was misleading.  While a method named Request was not in the current context, the problem actually was that I was using an object like a method.  I will admit that I probably made this mistake because I bounce back and forth between VB and C#.  Perhaps developers that exclusively do C# and Java would not be tripped up by this.

No comments: