对非共享成员的引用需要对象引用“

时间:2013-12-03 17:47:47

标签: asp.net vb.net

我正在研究餐馆系统。当客户打开网站时,主页面将显示时间,是否是早餐午餐或晚餐,具体取决于时间。但它继续在vb代码中的welcomelabel.text下给我这个消息.'welcomeLabel'没有声明。由于其保护级别,它可能无法访问。

vb代码:

Partial Class _Default
    Inherits System.Web.UI.Page

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

        Dim time As Date
        Dim CurrHour As Double

        time = DateTime.Now.ToString("hh:mm:ss")
        CurrHour = time.ToString("hh")


        If CurrHour = 10 Or CurrHour = 11 Then
            Welcomeabel.Text = time
           WelcomeLabel.ForeColor = Drawing.Color.White
           WelcomeLabel.Font.Size = "20"
            WelcomeLabel.BackColor = Drawing.Color.Black

        ElseIf CurrHour = 12 Or CurrHour = 1 Or CurrHour = 2 Or CurrHour = 3 Or CurrHour = 4 Or CurrHour = 5 Then
            label.Text = "Welcome dears you can enjoy ordering our meals!!It is your lUNCH Now " + time
            WelcomeLabel.ForeColor = Drawing.Color.White
           WelcomeLabel.Font.Size = "20"
            WelcomeLabel.BackColor = Drawing.Color.Black

        ElseIf CurrHour = 6 Or CurrHour = 7 Then
            label.Text = "Welcome dears you can enjoy ordering our meals!!come on and have a quick meal " + time
            WelcomeLabel.ForeColor = Drawing.Color.White
            WelcomeLabel.Font.Size = "20"
            WelcomeLabel.BackColor = Drawing.Color.Black

        ElseIf CurrHour = 8 Or CurrHour = 9 Then
            label.Text = "Welcome dears you can enjoy ordering our meals!!It is your Dinner Now " + time
           WelcomeLabel.ForeColor = Drawing.Color.White
           WelcomeLabel.Font.Size = "20"
            WelcomeLabel.BackColor = Drawing.Color.Black


        End If
    End Sub

End Class

asp代码:标签id是welcomeLabel,所以代码出错了吗?

<div style="margin-left: 16px"> 
            <asp:Label ID="welcomelabel" runat="server" BackColor="Black" 
          EnableViewState="False" Font-Size="XX-Large"            
        ForeColor="Yellow"></asp:Label>   <br /><br /><br />
  </div>                      

2 个答案:

答案 0 :(得分:2)

您似乎有命名冲突。 VB.Net不是区分大小写的语言,将id重命名为label以外的其他内容,然后通过ID访问它,或者你可以指定Me.label.Text来安抚编译器

答案 1 :(得分:1)

VB不区分大小写。您已经调用了标签“标签”,这会导致冲突。这条线

 label.Text = "Some Text"
例如,

被解释为引用类Label而不是页面上的实例label。解决方案很简单 - 不要为变量使用类名。将您的标签称为其他内容(即:“welcomeLabel”等)。