2013-06-03 75 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