CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2002
    Posts
    17

    Out of context Problem

    Hello,

    I seem to have a lack of understanding here and need some help.

    When I try and compile this in MS Visual Web Developer 2010 I am getting this error

    Error 5 The name 'DropDown1' does not exist in the current context

    I do not understand why this happening.

    Could someone please explain why this would be happening.


    Thanks
    Mike


    if (!LabelIsDone)
    {
    DropDownListText = "";
    LabelIsDone = true;
    CtrlNbr++;
    DropDownListId = "List1" ;
    DropDownList DropDown1 = new DropDownList();
    DropDown1.ID = DropDownListId; DropDown1.Width = 255;
    DropDown1.Items.Add("TEST ENTRY");
    this.PlaceHolder1.Controls.Add(DropDown1);
    else
    {
    DropDown1.Items.Add("ENTRY 2);
    }

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Out of context Problem

    Greetings Mike,

    First, if you put code in a post, use the [code][/code] tags around the code to make reading easier for everyone (as seen below).

    So, take a look at your code as it is formatted below, and pay attention to where you are creating the "new DropDownList();". Since you are only creating it if your LabelIsDone is false, it doesn't exist in the code that gets executed if LabelIsDone is true. So when you try to reference it there, you get that message... hope that makes sense for you.

    Do some reading on "scope" to learn more about where objects are available depending on how and where they are declared and created, etc.

    Also, remember that { and } must match (be paired up), if you open a block of code with a {, at some point, you need to close that block of code with a } ... look just above the "else" line, you are going to need to remedy that too.

    If you still need help or clarifications... feel free to ask some more... best of luck!
    Code:
                if (!LabelIsDone)
                {
                    DropDownListText = "";
                    LabelIsDone = true;
                    CtrlNbr++;
                    DropDownListId = "List1" ;
                    DropDownList DropDown1 = new DropDownList(); 
                    DropDown1.ID = DropDownListId; DropDown1.Width = 255;
                    DropDown1.Items.Add("TEST ENTRY");
                    this.PlaceHolder1.Controls.Add(DropDown1);
                else
                {
                    DropDown1.Items.Add("ENTRY 2);  
                }

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Out of context Problem

    You have defined your dropdown1 inside the If portion of your code but then try to access it in the else portion. If you want to use it in both places then you need to define it before the If portion.

    Objects are only valid within the scope they are created.

    Edit fcronin beat me to it
    Always use [code][/code] tags when posting code.

  4. #4
    Join Date
    Sep 2002
    Posts
    17

    Re: Out of context Problem

    Hello,

    Thank you for the information. This if is actually contained withing a while loop, so the rational behind the if (!LabelIsDone) id teel me I have created the dropdownlist and now I am filling in values. I guess I need to rethink the creation of the dropdown.

    The missing } was just a miss on transcribing the selected code to the board.

    Well back to the code and see what I can do to rework it.

    Mike

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Out of context Problem

    Quote Originally Posted by mfbrowne View Post
    Hello,

    Thank you for the information. This if is actually contained withing a while loop, so the rational behind the if (!LabelIsDone) id teel me I have created the dropdownlist and now I am filling in values. I guess I need to rethink the creation of the dropdown.

    The missing } was just a miss on transcribing the selected code to the board.

    Well back to the code and see what I can do to rework it.

    Mike
    Just create the DropDownList instance once outside the while loop. After you've cycled through the while loop and added values to the list, check the dropdown list count and add it to the controls collection if the count is greater than 0.



    Code:
    DropDownList DropDown1 = new DropDownList(); 
    dropDown1.ID = "List1";
    dropDown1.Width = 255;
    
    while( somecondition )
    {
      // add items to the dropdown as appropriate
    }
    
    if( dropDown1.Items.Count > 0 )
    {
      PlaceHolder1.Controls.Add( dropDown1 );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured