473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Question from a transforming VB guy on Global Variable

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
Nov 17 '05 #1
5 1532
"Scott Durrett" <kc****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
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
Nov 17 '05 #2
so If I create a static on my Main MDI form then I'm okay?

"Michael C" <mc*****@NOSPAMoptushome.com.au> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Scott Durrett" <kc****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #3
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" <kc****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #4
"Scott Durrett" <kc****@hotmail.com> wrote in message
news:OO**************@TK2MSFTNGP09.phx.gbl...
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
Nov 17 '05 #5
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.

"Steve McLellan" wrote:
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" <kc****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP09.phx.gbl...
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


Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Tom Moroow | last post by:
Hi, I'm pretty new to javascript and was wondering how you would piece together a variable name and then assign it a value. I want to create a hidden field and assign it a value based on the value...
4
by: Cathie | last post by:
Hi All, I am trying to get my style sheet to work. It works fine in IE but I can't get it to work in .net. Below is the function I use for transforming, where advancedOptionsFile is the path...
23
by: Jonathan Wood | last post by:
I was wondering if some of you could advise me on the following issue. I ported my code that generates shareware licenses to a C# class. I'd like to get to the point where this is done online...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.