Groups | Blog | Home
all groups > c# > april 2005 >

c# : Question from a transforming VB guy on Global Variable


Scott Durrett
4/26/2005 11:50:14 PM
I know and have been told that global variables are "bad". I must say that
I agree but what's the alternative to this?

1. I have an application that logs into a website. The site returns me a
sessionID and I must use this sessionID throughout the remainder of my
application.

I'm just not sure where is the proper place to store this kind of info in
C#.

Thanks in advance.
Doug

Michael C
4/27/2005 12:00:00 AM
[quoted text, click to view]

You have to be careful about taking things too far. Yes globals are bad when
not appropriate but they are perfectly ok when appropriate. It's actually
bad to try too hard to avoid them. I'd just keep it as a static variable in
a class somewhere, statics are basically global variables.

Michael

Steve McLellan
4/27/2005 12:00:00 AM
Create a class that logs into the website and gets the session ID. You can
make the class a singleton if there's only going to be one - for this kind
of thing, it would seem more appropriate than a simple static variable.

Steve

[quoted text, click to view]

Michael C
4/27/2005 12:00:00 AM
[quoted text, click to view]

Yes, although Steve's idea is probably a better way to go. One day you might
want to log into several pages and so having it in a class would be a good
idea. Basically the variable is in a class and you store a global instance
to the class.

Michael

Scott Durrett
4/27/2005 12:04:59 AM
so If I create a static on my Main MDI form then I'm okay?



[quoted text, click to view]

Catennacio
5/9/2005 9:10:06 PM
I think for his specific issue I would store the "sessionID" or "session key"
in the HttpSession provided by ASP.NET. Session is the kind of global
variable for web applications, and it's simple.

[quoted text, click to view]
AddThis Social Bookmark Button