Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Global Functions?

Global Functions?

Scheduled Pinned Locked Moved ASP.NET
questioncsharpasp-netwinformscom
9 Posts 5 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dazinith
    wrote on last edited by
    #1

    I have looked through 3 different asp.net books and tried to find a topic on cp, but i cant seem to figure out how to do a global function.. basicly i have a few functions which return information about the user logged in.. like: public string GetCurUserFirstName(); public string GetCurUserMiddleInitial(); public string GetCurUserUsername(); etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. so far all ive seen is .aspx pages and .ascx user controls which inherit classes from .cs files.. but it seems my classes have to be dirived from either Page or UserControl.. what if i want to just call a static function that returns a value and such? i want to be able to call it from any page, any user control, etc.. sorry to ask such a dull question but my books seem to skip this from what i can see.. i dont want to inherit a class, i want to import one that i can call static functions from.. thanks! still a newb.. cut me some slack :P -dz www.dzgraphics.com

    J P K 3 Replies Last reply
    0
    • D dazinith

      I have looked through 3 different asp.net books and tried to find a topic on cp, but i cant seem to figure out how to do a global function.. basicly i have a few functions which return information about the user logged in.. like: public string GetCurUserFirstName(); public string GetCurUserMiddleInitial(); public string GetCurUserUsername(); etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. so far all ive seen is .aspx pages and .ascx user controls which inherit classes from .cs files.. but it seems my classes have to be dirived from either Page or UserControl.. what if i want to just call a static function that returns a value and such? i want to be able to call it from any page, any user control, etc.. sorry to ask such a dull question but my books seem to skip this from what i can see.. i dont want to inherit a class, i want to import one that i can call static functions from.. thanks! still a newb.. cut me some slack :P -dz www.dzgraphics.com

      J Offline
      J Offline
      Jeremy Oldham
      wrote on last edited by
      #2

      Not sure if this would work as I have not tried it. The other day I wanted a constanct available to the entire application. I found that you could add the following to the Global.asax.vb file. Public Shared myString as String Then throughout the appplication, you can use Global.myString to obtain this value. My guess is that you might be able to add you functions to the Global.asax.vb(cs) file using the Shared keyword and it would be available without having to inherit the class. Let me know if this works or not. Jeremy Oldham

      1 Reply Last reply
      0
      • D dazinith

        I have looked through 3 different asp.net books and tried to find a topic on cp, but i cant seem to figure out how to do a global function.. basicly i have a few functions which return information about the user logged in.. like: public string GetCurUserFirstName(); public string GetCurUserMiddleInitial(); public string GetCurUserUsername(); etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. so far all ive seen is .aspx pages and .ascx user controls which inherit classes from .cs files.. but it seems my classes have to be dirived from either Page or UserControl.. what if i want to just call a static function that returns a value and such? i want to be able to call it from any page, any user control, etc.. sorry to ask such a dull question but my books seem to skip this from what i can see.. i dont want to inherit a class, i want to import one that i can call static functions from.. thanks! still a newb.. cut me some slack :P -dz www.dzgraphics.com

        P Offline
        P Offline
        Paul Riley
        wrote on last edited by
        #3

        dazinith wrote: i want to import one that i can call static functions from. Yes, that's exactly what you want to do. Add a class to the project and add some static methods to it.

        class MyClass
        {
        	public static string GetCurUserFirstName()
        	{
        		// do something here
        	}
        
        	public static string GetCurUserMiddleInitial()
        	{
        		// do something else here
        	}
        }
        

        and then call the methods as

        MyClass.GetCurUserFirstName
        

        Of course, it probably makes more sense to call the class CurrentUser and the methods FirstName(), MiddleInitial(), Username(). CurrentUser.FirstName() is more readable than SomeArbitraryName.GetCurUserFirstName() You might also want to look at properties, rather than methods. Paul We all will feed the worms and trees
        So don't be shy
        - Queens of the Stone Age, Mosquito Song

        D 1 Reply Last reply
        0
        • P Paul Riley

          dazinith wrote: i want to import one that i can call static functions from. Yes, that's exactly what you want to do. Add a class to the project and add some static methods to it.

          class MyClass
          {
          	public static string GetCurUserFirstName()
          	{
          		// do something here
          	}
          
          	public static string GetCurUserMiddleInitial()
          	{
          		// do something else here
          	}
          }
          

          and then call the methods as

          MyClass.GetCurUserFirstName
          

          Of course, it probably makes more sense to call the class CurrentUser and the methods FirstName(), MiddleInitial(), Username(). CurrentUser.FirstName() is more readable than SomeArbitraryName.GetCurUserFirstName() You might also want to look at properties, rather than methods. Paul We all will feed the worms and trees
          So don't be shy
          - Queens of the Stone Age, Mosquito Song

          D Offline
          D Offline
          dazinith
          wrote on last edited by
          #4

          I am not using visual studio .net, so im not sure how i would include the file.. if i wrote a class like you mentioned in one file how would i access its functions from another? how do i 'include' it? thanks for the help! still a newb.. cut me some slack :P -dz

          P 1 Reply Last reply
          0
          • D dazinith

            I am not using visual studio .net, so im not sure how i would include the file.. if i wrote a class like you mentioned in one file how would i access its functions from another? how do i 'include' it? thanks for the help! still a newb.. cut me some slack :P -dz

            P Offline
            P Offline
            Paul Riley
            wrote on last edited by
            #5

            dazinith wrote: I am not using visual studio .net Ahh... in that case, I don't know. Sorry. I keep meaning to figure out how to do it the "hard way", but it hasn't happened yet. :-O Paul We all will feed the worms and trees
            So don't be shy
            - Queens of the Stone Age, Mosquito Song

            D 1 Reply Last reply
            0
            • P Paul Riley

              dazinith wrote: I am not using visual studio .net Ahh... in that case, I don't know. Sorry. I keep meaning to figure out how to do it the "hard way", but it hasn't happened yet. :-O Paul We all will feed the worms and trees
              So don't be shy
              - Queens of the Stone Age, Mosquito Song

              D Offline
              D Offline
              dazinith
              wrote on last edited by
              #6

              got the static functions to work by making a .dll and putting it in the /bin directory.. guess that is how they want u to do it.. at least according to this book i got.. still a newb.. cut me some slack :P -dz

              P 1 Reply Last reply
              0
              • D dazinith

                got the static functions to work by making a .dll and putting it in the /bin directory.. guess that is how they want u to do it.. at least according to this book i got.. still a newb.. cut me some slack :P -dz

                P Offline
                P Offline
                Paul Riley
                wrote on last edited by
                #7

                dazinith wrote: got the static functions to work by making a .dll and putting it in the /bin directory Duh! Should have thought of that. I already keep all my Common functionality in a DLL anyway, so I can move it across applications. :rolleyes: Paul We all will feed the worms and trees
                So don't be shy
                - Queens of the Stone Age, Mosquito Song

                1 Reply Last reply
                0
                • D dazinith

                  I have looked through 3 different asp.net books and tried to find a topic on cp, but i cant seem to figure out how to do a global function.. basicly i have a few functions which return information about the user logged in.. like: public string GetCurUserFirstName(); public string GetCurUserMiddleInitial(); public string GetCurUserUsername(); etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. so far all ive seen is .aspx pages and .ascx user controls which inherit classes from .cs files.. but it seems my classes have to be dirived from either Page or UserControl.. what if i want to just call a static function that returns a value and such? i want to be able to call it from any page, any user control, etc.. sorry to ask such a dull question but my books seem to skip this from what i can see.. i dont want to inherit a class, i want to import one that i can call static functions from.. thanks! still a newb.. cut me some slack :P -dz www.dzgraphics.com

                  K Offline
                  K Offline
                  Kastellanos Nikos
                  wrote on last edited by
                  #8

                  dazinith wrote: etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. The easiest way is to put a function in the Global.asax file like this: public String myfunction(string initoutput) { string result; //do some usefull stuff return result; } the you can call that function from any page using: ApplicationInstance.myfunction(""); - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

                  J 1 Reply Last reply
                  0
                  • K Kastellanos Nikos

                    dazinith wrote: etc.. i want to be able to call these functions from multiple pages and user controls (like side menus and panels).. The easiest way is to put a function in the Global.asax file like this: public String myfunction(string initoutput) { string result; //do some usefull stuff return result; } the you can call that function from any page using: ApplicationInstance.myfunction(""); - - - - - - - - - - - - - - - - - - Memory leaks is the price we pay \0 01234567890123456789012345678901234

                    J Offline
                    J Offline
                    John Whish
                    wrote on last edited by
                    #9

                    Nice one! Been trying to figure out how to do this for ages and then stumbled across your post. Thanks :)

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups