Global Functions?
-
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 -
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.comNot 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 -
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.comdazinith 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 -
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 SongI 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
-
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
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 -
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 -
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
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 -
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.comdazinith 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
-
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
Nice one! Been trying to figure out how to do this for ages and then stumbled across your post. Thanks :)