dcsimg
Results 1 to 4 of 4

Thread: BC30616: Variable 'username' hides a variable in an enclosing block.

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    466

    BC30616: Variable 'username' hides a variable in an enclosing block.

    How do I resolve this error in my code, please (not even sure what it means):

    Code:
    If Not IsPostBack Then
     
    Dim username As Object = Nothing
    username.Text = String.Format("{0}", Request.QueryString("Name"))
    The error refers to this line:

    Code:
    Dim username As Object = Nothing
    BC30616: Variable 'username' hides a variable in an enclosing block.

    So one variable, a text field called 'username' is hiding another variable (which is not named)?

    In context, the code looks like this:

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
    
                Dim username As Object = Nothing
    
                username.Text = String.Format("{0}", Request.QueryString("Name"))
    
            End If
    
            Dim LblDate As Object = Nothing
            LblDate.Text = ReturnDate()
    
            Dim username As Object = Nothing
            username.Focus()
    
        End Sub
    
    End Class
    Thanks!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,905

    Re: BC30616: Variable 'username' hides a variable in an enclosing block.

    That code has two lines that declare a variable called username (one inside the If block, and one outside it), and that isn't allowed. You should generally either use different names, or only one variable.

    However, in this case neither of those variables (nor LblDate) make sense. In all three cases you set them to Nothing, and then immediately attempt to use their properties/methods - but you obviously wont be able to, because the variable doesn't refer to anything.


    What are you actually trying to do, and are you sure that you even need to be creating any variables?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    466

    Re: BC30616: Variable 'username' hides a variable in an enclosing block.

    Thanks si_the-geek for explaining about the block.

    The code aims to personalise a 'thank you' message to the user who has submitted a form. Because we don't know the name of the user, the form field, which looks like this in my aspx page

    Code:
     <asp:TextBox ID="username" runat="server"></asp:TextBox></p>
    is supposed to be a variable.

    On each of those 3 lines you mention (2 x username and the LblDate), the light-bulb flashed up to indicated I needed to declare it locally.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2014
    Posts
    466

    Re: BC30616: Variable 'username' hides a variable in an enclosing block.

    I have just copied and pasted an older version of the above (taking no notice of the bulb):

    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            If Not IsPostBack Then
    
                username.Text = String.Format("{0}", Request.QueryString("Name"))
    
            End If
    
            LblDate.Text = ReturnDate()
    
            username.Focus()
    
        End Sub
    and I see no errors. Restarted VS to make sure and no errors. The mind boggles.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width