2013-06-03 51 views
0

我可以让我的功能在一个页面上工作,但是当我想从App_Code文件夹中调用该函数时,我在“CreateSentence”部分发现一个错误,指向一个非共享成员。公共课和App_Code

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

页 - 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 

回答

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

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