0

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

如果有人有一个简单的共享函数示例(该函数位于 App_Code 文件夹中,而不是在同一页面上)或者可以指出错误,我会非常感激。

页数 - 3

1 - 默认.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 - 默认.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
4

2 回答 2

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

于 2013-06-03T10:36:37.887 回答
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
于 2013-06-03T10:39:46.520 回答