Question from a transforming VB guy on Global Variable

  • Thread starter Thread starter Scott Durrett
  • Start date Start date
S

Scott Durrett

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
 
Scott Durrett said:
I know and have been told that global variables are "bad". I must say that
I agree but what's the alternative to this?

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
 
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
 
Scott Durrett said:
so If I create a static on my Main MDI form then I'm okay?

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
 
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.
 
Back
Top