indofraiser
indofraiser

Reputation: 1024

Public Class and App_Code

I can get my function working on one page but when I want to call the function from the App_Code folder I get an error on the "CreateSentence" part saying is is referenced to a non-shared member.

If someone has a simple shared function example (where the function is in the App_Code folder and not on the same page) or can point out the error I would be greatful.

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

Upvotes: 0

Views: 1476

Answers (2)

Kevin Brechb&#252;hl
Kevin Brechb&#252;hl

Reputation: 4727

Your function should be shared:

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

Then you can call the function within your page Functions.CreateSentence:

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

Upvotes: 0

Sean Airey
Sean Airey

Reputation: 6372

Simply edit your function to be shared:

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

Maybe giving this article a read would help in your understanding for the future: http://msdn.microsoft.com/en-us/library/zc2b427x.aspx

Upvotes: 1

Related Questions