Search code examples
asp.net.netvb.netuser-controlsaspx-user-control

Why is my UserControl's markup telling me "variable is not defined", despite variable being defined and public in code?


I have the following user control in my project:

Markup (SlidingScaleTableReadonly.ascx):

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="SlidingScaleTableReadonly.ascx.vb" Inherits="Client.Project.SlidingScaleTableReadonly" %>

<table class="table">
    <thead>
        <tr>
            <th scope="col">Résultat glycémique</th>
            <th scope="col">Unité à administrer</th>
        </tr>
    </thead>
    <tbody>
        <% For Each scaleEntry In ScaleEntries %>
            <tr>
                <td><%= scaleEntry.LowerValue %> - <%= scaleEntry.HigherValue %></td>
                <td><%= scaleEntry.UnitToDeliver %></td>
            </tr>
        <% Next %>
    </tbody>
</table>

Code (SlidingScaleTableReadonly.ascx.vb):

Public Class SlidingScaleTableReadonly
    Inherits UserControl

    Public ScaleEntries As List(Of ScaleEntry)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

And the following error at line 11 in SlidingScaleTableReadonly.ascx:

BC30451 'scaleEntries' is not declared. It may be inaccessible due to its protection level.

I'd call the compiler wrong on this one; scaleEntries is defined and public. What's wrong then?


Solution

  • Exit vs, then re-load. Now do a re-build all for the project - that should make those errors go away. Often sometimes they will STILL show, but if you build (or run) the project, they go away. I can't say when/why this occurs, but VS OFTEN will struggle to resolve those imbedded server side expressions in the HTML markup

    In most cases a re-build all will fix this, but I have found that sometimes you have to exit VS, re-launch. Do a re-build all, and the error goes away.