Namespaces and shared functions

Last post 07-31-2009 10:31 PM by renasis. 7 replies.

Sort Posts:

  • Namespaces and shared functions

    07-31-2009, 7:41 PM
    • Member
      52 point Member
    • renasis
    • Member since 09-27-2006, 8:33 PM
    • Posts 120

     Hello,

    I am working with VB.Net and am kinda new. I was wondering, if my asp.net project has a lot of shared functions, can I put them all into one Namespace? Do I have to put them into a class?

    Thanks,

    -renasis

  • Re: Namespaces and shared functions

    07-31-2009, 8:11 PM
    • All-Star
      98,100 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    You could place them in either a Class or a Module - and yes you could put them all in a single namespace if that's what you want.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Namespaces and shared functions

    07-31-2009, 8:31 PM
    • Member
      52 point Member
    • renasis
    • Member since 09-27-2006, 8:33 PM
    • Posts 120

    Do I have to place them into a class or module? Could I just put them into a namespace?

  • Re: Namespaces and shared functions

    07-31-2009, 8:37 PM
    • All-Star
      98,100 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    You cannot place methods directly into a namespace.  They must be in either a class or a module.

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Namespaces and shared functions

    07-31-2009, 9:21 PM
    • Member
      52 point Member
    • renasis
    • Member since 09-27-2006, 8:33 PM
    • Posts 120

    OK. Is there a way where I don't have to write the class each time I call the method? I am planning on placing my shared functions inside a class, which resides in a namespace with the same name. Like so,

    Namespace Utility
    Public Class Utility
    shared function getsomething()
    end function
    end class
    end namespace

    Then I want to be able to use the getsomething function in another class. Based on an example on the web(http://www.sitecrafting.com/blog/aspnet-20-tips-shared-functions/), it indicates that I can import the namespace I just created an access the function by its name alone, but I can't seem to get it to work. This is what I am trying to do.

    imports Utility

    public class x

    dim x as string = getsomething 'VS needs Utility. before getsomething

    end class

     

  • Re: Namespaces and shared functions

    07-31-2009, 9:23 PM
    Answer
    • Star
      10,558 point Star
    • Danny117
    • Member since 12-16-2008, 2:30 PM
    • Royal Oak Michigan USA
    • Posts 1,837

    Quick simple pseudo code.  Have fun without namespaces you can never do implements and really design code that others write for you.

     

    First you need some code in a file seperate from the form or web page in the app_code folder.

    Just type namespace at the top of your code I recomend starting with two levels then the class name is the third level.

    namespace whatever.whatever

    class dosomething.

    end class

    end namespace

     

    'then your webpage or form

     

    imports whatever.whatever 'Using whatever.whatever;

     

    'then you can just access the class in your code.

    dosomething(visitorid)';

    'or write it out

    whatever.whatever.dosomething(visitorid)';

     

     

    Good Luck



  • Re: Namespaces and shared functions

    07-31-2009, 10:29 PM
    • All-Star
      98,100 point All-Star
    • mbanavige
    • Member since 11-06-2003, 1:29 PM
    • New England, USA
    • Posts 10,347
    • Moderator
      TrustedFriends-MVPs

    renasis:
    access the function by its name alone
     

     

    in vb.net, you can place the function in a module instead of a class

    Mike Banavige
    ~~~~~~~~~~~~
    Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
  • Re: Namespaces and shared functions

    07-31-2009, 10:31 PM
    • Member
      52 point Member
    • renasis
    • Member since 09-27-2006, 8:33 PM
    • Posts 120

     What if I am using the function in another code file in the app_code folder? Does same code apply?

     

Page 1 of 1 (8 items)