By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

C# Website - Global Functions

P: n/a
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references
to non-static objects" and whatnot. I realize what that means and all,
but what I'm wondering is what's the best way around it? Say, for
example, I want a function that takes a username and a password and
returns true or false if it's a successful login, and I want any page or
usercontrol in the website to be able to call that function, where
should I put it and what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references to
non-static objects" and whatnot. I realize what that means and all, but
what I'm wondering is what's the best way around it? Say, for example, I
want a function that takes a username and a password and returns true or
false if it's a successful login, and I want any page or usercontrol in
the website to be able to call that function, where should I put it and
what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #2

P: n/a
Either of the methods you suggest definitely work. However, what if
some of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references to
non-static objects" and whatnot. I realize what that means and all, but
what I'm wondering is what's the best way around it? Say, for example, I
want a function that takes a username and a password and returns true or
false if it's a successful login, and I want any page or usercontrol in
the website to be able to call that function, where should I put it and
what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu


Nov 16 '05 #3

P: n/a
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a couple
key things to remember:

First, add the session to the collection in from the Global.Session_Start()
method and then make sure you remove the session from your collection from
the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references
to non-static objects" and whatnot. I realize what that means and all,
but what I'm wondering is what's the best way around it? Say, for
example, I want a function that takes a username and a password and
returns true or false if it's a successful login, and I want any page or
usercontrol in the website to be able to call that function, where should
I put it and what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu



Nov 16 '05 #4

P: n/a
Ok, I can get the Session ID from the page, pass it to the Global
function as a string. Now how do I use it to specify a running session?

DalePres wrote:
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a couple
key things to remember:

First, add the session to the collection in from the Global.Session_Start()
method and then make sure you remove the session from your collection from
the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and put
the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references
to non-static objects" and whatnot. I realize what that means and all,
but what I'm wondering is what's the best way around it? Say, for
example, I want a function that takes a username and a password and
returns true or false if it's a successful login, and I want any page or
usercontrol in the website to be able to call that function, where should
I put it and what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #5

P: n/a
It depends on what you are really wanting to accomplish.

For instance, if you want to write to the Response object associated with a
specific Session then your global collection should hold Context objects.
Use a foreach loop to iterate through the collection, testing for a matching
Context.Session.SessionID value. When you have identified the correct
context, you can Context.Response.Write() to that context.

Can you give a description of what it is you're trying to accomplish?

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Ok, I can get the Session ID from the page, pass it to the Global function
as a string. Now how do I use it to specify a running session?

DalePres wrote:
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a
couple key things to remember:

First, add the session to the collection in from the
Global.Session_Start() method and then make sure you remove the session
from your collection from the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:

You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and
put the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl.. .
>When I wrote websites in VB .NET, I would often put functions in Global
>for all the pages to call. Now, in C#, doing so results in "references
>to non-static objects" and whatnot. I realize what that means and all,
>but what I'm wondering is what's the best way around it? Say, for
>example, I want a function that takes a username and a password and
>returns true or false if it's a successful login, and I want any page
>or usercontrol in the website to be able to call that function, where
>should I put it and what syntax do I need?
>
>
>Regards,
>David P. Donahue
>dd******@ccs.neu.edu

Nov 16 '05 #6

P: n/a
I looked for the code where I had done this in the past and don't have it
any longer so I did some playing around.

I think the answer to your original question, about creating global objects,
create the object in the Global.asax.cs as a static method or object such
as:

private static ArrayList allContexts = new ArrayList();

public static ArrayList AllContexts

{

get { return allContexts; }

set { allContexts = value; }

}

public static void SendWebMessage(string sessionid)

{

ArrayList contexts = AllContexts;

for (int count = 0; count < AllContexts.Count; count++)

{

HttpContext context = (HttpContext)AllContexts[count];

if ((context != null)

&& (string.Compare(context.Session.SessionID, sessionid, false) == 0))

{

context.Response.Write("<BR>This is a message from the server!<BR>");

}

else

{

Global.AllContexts.RemoveAt(count);

}

}

}

///// Then, modify the following instance methods in your Global.asax.cs to
include the code below:

protected void Session_Start(Object sender, EventArgs e)

{

lock(Global.AllContexts)

{

Global.AllContexts.Add(Context);

}

}

protected void Session_End(Object sender, EventArgs e)

{

lock(Global.AllContexts)

{

for (int count = 0; count < Global.AllContexts.Count; count++)

{

HttpContext context = (HttpContext)Global.AllContexts[count];

if ((context != null)

&& (string.Compare(context.Session.SessionID, Session.SessionID, false) ==
0))

{

Global.AllContexts.RemoveAt(count);

}

else

{

Global.AllContexts.RemoveAt(count);

}

}

}

}

//// Then, lastly, in your web page classes, you would send a message like
this:

Global.SendWebMessage(Session.SessionID);

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Ok, I can get the Session ID from the page, pass it to the Global function
as a string. Now how do I use it to specify a running session?

DalePres wrote:
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a
couple key things to remember:

First, add the session to the collection in from the
Global.Session_Start() method and then make sure you remove the session
from your collection from the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:

You can add your method to Global.asax such as

public static bool Login()

{

private bool success = false;

// do some login stuff here and set success

return success;

}

and call it like

bool loggedIn = Global.Login();

But a better solution, I think, would be to create a helper class and
put the methods that you want globally available in that class.

HTH

DalePres
MCAD, MCDBA, MCSE
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl.. .
>When I wrote websites in VB .NET, I would often put functions in Global
>for all the pages to call. Now, in C#, doing so results in "references
>to non-static objects" and whatnot. I realize what that means and all,
>but what I'm wondering is what's the best way around it? Say, for
>example, I want a function that takes a username and a password and
>returns true or false if it's a successful login, and I want any page
>or usercontrol in the website to be able to call that function, where
>should I put it and what syntax do I need?
>
>
>Regards,
>David P. Donahue
>dd******@ccs.neu.edu

Nov 16 '05 #7

P: n/a
The vast majority of my interaction with the Session object is
reading/writing session variables:

Session[stringName] = stringValue;
stringTemp = Session[stringName];

That sort of thing. Am I going to have to loop through the collection?
Or is there a way to jus specify a Session and access it?

DalePres wrote:
It depends on what you are really wanting to accomplish.

For instance, if you want to write to the Response object associated with a
specific Session then your global collection should hold Context objects.
Use a foreach loop to iterate through the collection, testing for a matching
Context.Session.SessionID value. When you have identified the correct
context, you can Context.Response.Write() to that context.

Can you give a description of what it is you're trying to accomplish?

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Ok, I can get the Session ID from the page, pass it to the Global function
as a string. Now how do I use it to specify a running session?

DalePres wrote:
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a
couple key things to remember:

First, add the session to the collection in from the
Global.Session_Start() method and then make sure you remove the session
from your collection from the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl. ..
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
>You can add your method to Global.asax such as
>
>public static bool Login()
>
>{
>
> private bool success = false;
>
> // do some login stuff here and set success
>
> return success;
>
>}
>
>and call it like
>
>bool loggedIn = Global.Login();
>
>But a better solution, I think, would be to create a helper class and
>put the methods that you want globally available in that class.
>
>HTH
>
>DalePres
>MCAD, MCDBA, MCSE
>
>
>"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
>news:ON**************@TK2MSFTNGP12.phx.gbl. ..
>
>
>
>>When I wrote websites in VB .NET, I would often put functions in Global
>>for all the pages to call. Now, in C#, doing so results in "references
>>to non-static objects" and whatnot. I realize what that means and all,
>>but what I'm wondering is what's the best way around it? Say, for
>>example, I want a function that takes a username and a password and
>>returns true or false if it's a successful login, and I want any page
>>or usercontrol in the website to be able to call that function, where
>>should I put it and what syntax do I need?
>>
>>
>>Regards,
>>David P. Donahue
>>dd******@ccs.neu.edu
>
>


Nov 16 '05 #8

P: n/a
You can access the current session (or request, or response, or
whatever) by using HttpContext.Current.

<code>
public static string GetSessionID()
{
//You can also access the current Request, Response, or Server.
return HttpContext.Current.Session.SessionID;
}

//From somewhere else:
Response.Write(Global.GetSessionID());
</code>

HTH,
~d

DalePres wrote:
It depends on what you are really wanting to accomplish.

For instance, if you want to write to the Response object associated with a
specific Session then your global collection should hold Context objects.
Use a foreach loop to iterate through the collection, testing for a matching
Context.Session.SessionID value. When you have identified the correct
context, you can Context.Response.Write() to that context.

Can you give a description of what it is you're trying to accomplish?

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Ok, I can get the Session ID from the page, pass it to the Global function
as a string. Now how do I use it to specify a running session?

DalePres wrote:
Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections of
session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are a
couple key things to remember:

First, add the session to the collection in from the
Global.Session_Start() method and then make sure you remove the session
from your collection from the Global.Session_End() method.

Second, be aware of the security risks involved in granting one session
access to code that is aware of, and able to communicate with, another
session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl. ..
Either of the methods you suggest definitely work. However, what if some
of this global code refers to things like the Session object? Same
problem of non-staticness. How would I be able to refer to the correct
Session?
Regards,
David P. Donahue
dd******@ccs.neu.edu

DalePres wrote:
>You can add your method to Global.asax such as
>
>public static bool Login()
>
>{
>
> private bool success = false;
>
> // do some login stuff here and set success
>
> return success;
>
>}
>
>and call it like
>
>bool loggedIn = Global.Login();
>
>But a better solution, I think, would be to create a helper class and
>put the methods that you want globally available in that class.
>
>HTH
>
>DalePres
>MCAD, MCDBA, MCSE
>
>
>"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
>news:ON**************@TK2MSFTNGP12.phx.gbl. ..
>
>
>
>>When I wrote websites in VB .NET, I would often put functions in Global
>>for all the pages to call. Now, in C#, doing so results in "references
>>to non-static objects" and whatnot. I realize what that means and all,
>>but what I'm wondering is what's the best way around it? Say, for
>>example, I want a function that takes a username and a password and
>>returns true or false if it's a successful login, and I want any page
>>or usercontrol in the website to be able to call that function, where
>>should I put it and what syntax do I need?
>>
>>
>>Regards,
>>David P. Donahue
>>dd******@ccs.neu.edu
>
>


Nov 16 '05 #9

P: n/a
That's _exactly_ what I needed! Thanks!
Regards,
David P. Donahue
dd******@ccs.neu.edu
D.W. Warlock wrote:
You can access the current session (or request, or response, or
whatever) by using HttpContext.Current.

<code>
public static string GetSessionID()
{
//You can also access the current Request, Response, or Server.
return HttpContext.Current.Session.SessionID;
}

//From somewhere else:
Response.Write(Global.GetSessionID());
</code>

HTH,
~d

DalePres wrote:
It depends on what you are really wanting to accomplish.

For instance, if you want to write to the Response object associated
with a specific Session then your global collection should hold
Context objects. Use a foreach loop to iterate through the collection,
testing for a matching Context.Session.SessionID value. When you have
identified the correct context, you can Context.Response.Write() to
that context.

Can you give a description of what it is you're trying to accomplish?

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:eH**************@TK2MSFTNGP10.phx.gbl...
Ok, I can get the Session ID from the page, pass it to the Global
function as a string. Now how do I use it to specify a running session?

DalePres wrote:

Pass the SessionID as a parameter to your global method.

I have had to, in some cases, create Application scope collections
of session objects or SessionID strings so that global functions can
communicate across sessions. If you have to go that far, there are
a couple key things to remember:

First, add the session to the collection in from the
Global.Session_Start() method and then make sure you remove the
session from your collection from the Global.Session_End() method.

Second, be aware of the security risks involved in granting one
session access to code that is aware of, and able to communicate
with, another session.

HTH

DalePres
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:e1****************@TK2MSFTNGP15.phx.gbl...
> Either of the methods you suggest definitely work. However, what
> if some of this global code refers to things like the Session
> object? Same problem of non-staticness. How would I be able to
> refer to the correct Session?
>
>
> Regards,
> David P. Donahue
> dd******@ccs.neu.edu
>
>
>
> DalePres wrote:
>
>
>> You can add your method to Global.asax such as
>>
>> public static bool Login()
>>
>> {
>>
>> private bool success = false;
>>
>> // do some login stuff here and set success
>>
>> return success;
>>
>> }
>>
>> and call it like
>>
>> bool loggedIn = Global.Login();
>>
>> But a better solution, I think, would be to create a helper class
>> and put the methods that you want globally available in that class.
>>
>> HTH
>>
>> DalePres
>> MCAD, MCDBA, MCSE
>>
>>
>> "David P. Donahue" <dd******@ccs.neu.edu> wrote in message
>> news:ON**************@TK2MSFTNGP12.phx.gbl...
>>
>>
>>
>>> When I wrote websites in VB .NET, I would often put functions in
>>> Global for all the pages to call. Now, in C#, doing so results
>>> in "references to non-static objects" and whatnot. I realize
>>> what that means and all, but what I'm wondering is what's the
>>> best way around it? Say, for example, I want a function that
>>> takes a username and a password and returns true or false if it's
>>> a successful login, and I want any page or usercontrol in the
>>> website to be able to call that function, where should I put it
>>> and what syntax do I need?
>>>
>>>
>>> Regards,
>>> David P. Donahue
>>> dd******@ccs.neu.edu
>>
>>
>>


Nov 16 '05 #10

P: n/a
Assuming you are using Visual Studio and are writing your app using
code-behinds, then simply create a class in any *.cs file in the project, or
create a new *.cs file in the project. Any page can simply create the
object and call the method, or, if you declare the method as "static" then
any code can simply call the method without creating an object first.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"David P. Donahue" <dd******@ccs.neu.edu> wrote in message
news:ON**************@TK2MSFTNGP12.phx.gbl...
When I wrote websites in VB .NET, I would often put functions in Global
for all the pages to call. Now, in C#, doing so results in "references to
non-static objects" and whatnot. I realize what that means and all, but
what I'm wondering is what's the best way around it? Say, for example, I
want a function that takes a username and a password and returns true or
false if it's a successful login, and I want any page or usercontrol in
the website to be able to call that function, where should I put it and
what syntax do I need?
Regards,
David P. Donahue
dd******@ccs.neu.edu

Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.