locked
Public function on default page or somewhere else? RRS feed

  • Question

  • User-1242214802 posted

     Hi All: I have a public function declared in my code-beside on default.aspx.vb. I needed to add a second page to my project and copied and pasted that function into the second page, but when I test the page I get an error saying there's a conflict with the same public function on the default page.

    If I remove the function from the second page, i don't get the error, but will it still be accessible if called on that page, even though it's on the default page? Or do I need to place it somewhere else so that it's accessible to both pages?

    Sunday, August 30, 2009 9:53 AM

All replies

  • User-1242214802 posted

     Sorry...let me rephrase the question.

    If I create a public class and place it in my app_code folder, is it automagically accessible to every page in the project, or do I need to add a namespace?

    Or, if there are no namespaces in the project, is everything automagically organized under the project's default namespace? I'm new to the concept of namespaces and inheritance <g>

    Sunday, August 30, 2009 10:35 AM
  • User-952121411 posted

     The best method for your case being newer to .NET would be not to get into a more complex layered approach determining where that logic should exist, but rather keep the code at the UI level until you grasp some more OOP concepts in the future.

    As you mentioned, a good way to solve your problem is to create a public class (in your App_Code folder is fine; default location for code in a 'Web Site' template project) that is accessable to your (2) pages. By making it 'Public' you will just need to create an instance of it so it can be accessed by any of your pages.  With this centeralized placing of your re-usable code, you do not need to copy and paste the method into each page; just create a new instance of the class in any page needed, and call the classes methods.

    Below I have created a simple example to show you how to use this:

    (First the class with the methods you want to allow your pages to access)

    'This class exists in the 'App_Code' folder
    Imports Microsoft.VisualBasic
    
    Public Class MyCustomClass
    
        Public Function MyMethod1() As String
    
            Return "Hello from Mymethod1!"
    
        End Function
    
        Public Function MyMethod2() As String
    
            Return "Hello from Mymethod2!"
    
        End Function
    
    End Class


    (Second, here would be an example calling code from the UI Page_Load event:)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
            'Instantiate an instance of the class named 'MyCustomClass' that resides 
            'in the 'App_Code' folder so we can call its methods
            Dim MyCustCls As New MyCustomClass
            'Set Label1 .Text property to return value from MyMethod1()
            Me.Label1.Text = MyCustCls.MyMethod1()
            'Set Label2 .Text property to return value from MyMethod2()
            Me.Label2.Text = MyCustCls.MyMethod2()
    
    End Sub

    Hope this helps! Smile

    Monday, September 21, 2009 1:57 PM