公共类和App_Code

时间:2013-06-03 10:32:58

标签: asp.net vb.net

我可以在一个页面上运行我的函数但是当我想从App_Code文件夹调用该函数时,我在“CreateSentence”部分得到一个错误,说是被非共享成员引用。

如果有人有一个简单的共享功能示例(其中该功能位于App_Code文件夹中而不在同一页面上),或者可以指出错误我会很高兴。

Pages - 3

1 - Default.aspx

 <asp:Button ID="Button1" runat="server" Text="Create Sentence" />
    <asp:TextBox ID="word" runat="server"></asp:TextBox>  
    <asp:Label ID="sentence" runat="server" Text="Label"></asp:Label>

2 - Default.aspx.vb

Imports Functions

Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click

        sentence.Text = CreateSentence(word.Text)
    End Sub
End Class

3 - App_code / Class1.vb

Imports Microsoft.VisualBasic

Public Class Functions

    Function CreateSentence(ByVal word As String) As String

        Dim Sentence As String = "The world you have is: " & word & "."

        Return Sentence

    End Function

End Class

2 个答案:

答案 0 :(得分:1)

只需编辑您要共享的功能:

Imports Microsoft.VisualBasic

Public Class Functions

    Public Shared Function CreateSentence(ByVal word As String) As String

        Dim Sentence As String = "The world you have is: " & word & "."

        Return Sentence

    End Function

End Class

也许阅读这篇文章有助于您理解未来:http://msdn.microsoft.com/en-us/library/zc2b427x.aspx

答案 1 :(得分:0)

应该共享您的功能:

Public Shared Function CreateSentence(ByVal word As String) As String
   Dim Sentence As String = "The world you have is: " & word & "."
   Return Sentence
End Function

然后,您可以在页面Functions.CreateSentence中调用该功能:

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
   sentence.Text = Functions.CreateSentence(word.Text)
End Sub
相关问题