473,808 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Oh No a Global Variable


This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????

Nov 17 '05 #1
17 4459
You can't have variables outside of a class. Try it and you'll get a compile
error (I just tried). I would suggest having a config file that stores your
connection string, and maybe a singleton settings class that is responsible
for retrieving all of your settings. This way you are sure your entire app
is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????

Nov 17 '05 #2
"Mike L" <Ca***@nospam.n ospam> a écrit dans le message de news:
13************* *************** **...icrosof t.com...
Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If so, what is the code to make a global string variable, and where do I put it, (before the namespace, of just under it)?????


The usual solultion for this is to have a static class of static methods or
properties that return the necessary "global" values.

static class Settings
{
public static string MyValue
{
get
{
return "MyValue";
}
}
}

Then you can use it like this :

{
string user = Settings.MyValu e;
...
}

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #3
I added a config file but it looks like xml code. Will this work for my win
form application?

"Wayne" wrote:
You can't have variables outside of a class. Try it and you'll get a compile
error (I just tried). I would suggest having a config file that stores your
connection string, and maybe a singleton settings class that is responsible
for retrieving all of your settings. This way you are sure your entire app
is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????


Nov 17 '05 #4
Mike,

You are probably best adding a application configuration file to your
application and storing the value in there and then reading out the value as
required at runtime.

check out for asp.net sample:
http://msdn.microsoft.com/msdntv/epi...H/manifest.xml

Check out for winform sample:
Search for the text 'Using XML Application Configuration Files'
http://msdn.microsoft.com/library/de.../html/daag.asp

HTH

Ollie Riches
"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?
If
so, what is the code to make a global string variable, and where do I put
it,
(before the namespace, of just under it)?????

Nov 17 '05 #5
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.Configur ationSettings.A ppSettings to retrieve the value, ex:
string sDsn = ConfigurationSe ttings.AppSetti ngs["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation? If so, what is the code to make a global string variable, and where do I put it, (before the namespace, of just under it)?????

Nov 17 '05 #6
Yes. Like so:
<?xml version="1.0" encoding="utf-8" ?>

<configuratio n>

<appSettings>

<add key="dsn" value="Server=( local);Database =XXXX;User
ID=XXXX;Passwor d=XXXXXXXX" />

</appSettings>

</configuration>

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:D7******** *************** ***********@mic rosoft.com...
I added a config file but it looks like xml code. Will this work for my win form application?

"Wayne" wrote:
You can't have variables outside of a class. Try it and you'll get a compile error (I just tried). I would suggest having a config file that stores your connection string, and maybe a singleton settings class that is responsible for retrieving all of your settings. This way you are sure your entire app is using the same values, and you only retrieve them in one place.

--
Thanks
Wayne Sepega
Jacksonville, Fl

Enterprise Library Configuration Console Module Generator
http://workspaces.gotdotnet.com/elccmg

"When a man sits with a pretty girl for an hour, it seems like a minute. But let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out my application. Is a global variable the best choice for this situation? If
so, what is the code to make a global string variable, and where do I put it,
(before the namespace, of just under it)?????


Nov 17 '05 #7
I this line of code: string sConnString =
ConfigurationSe ttings.AppSetti ngs["dsn"];

I get the error: The type or namespace name 'ConfigurationS ettings' could
not be found (are you missing a using directive or an assembly reference?)
I also tried: string sConnString = configuration.a ppSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>

<appSetting>

<add key="dsn" value="Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= REMOVED" />

</appSetting>

</configuration>

"Mark White" wrote:
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.Configur ationSettings.A ppSettings to retrieve the value, ex:
string sDsn = ConfigurationSe ttings.AppSetti ngs["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out
my application. Is a global variable the best choice for this situation?

If
so, what is the code to make a global string variable, and where do I put

it,
(before the namespace, of just under it)?????


Nov 17 '05 #8
Mike

It would be: System.Configur ation.Configura tionSettings.Ap pSettings["dsn"]
-OR-
using System.Configur ation;
then in the code you can use: ConfigurationSe ttings.AppSetti ngs["dsn"];

"Mike L" <Ca***@nospam.n ospam> wrote in message
news:06******** *************** ***********@mic rosoft.com...
I this line of code: string sConnString =
ConfigurationSe ttings.AppSetti ngs["dsn"];

I get the error: The type or namespace name 'ConfigurationS ettings' could
not be found (are you missing a using directive or an assembly reference?)
I also tried: string sConnString = configuration.a ppSetting["dsn"];
I get the error: The type or namespace name 'configuration' could not be
found (are you missing a using directive or an assembly reference?)

Here is the App.config code, I removed the password for security.

<?xml version="1.0" encoding="utf-8" ?>
<configuratio n>

<appSetting>

<add key="dsn" value="Data Source=db;Datab ase=License;Int egrated
Security=False; User ID=sa;password= REMOVED" />

</appSetting>

</configuration>

"Mark White" wrote:
I would go with a App.config file. That is exactly it's purpose, it's
secure and already accessible. No need to reinvent the wheel.

Add a key (XML format) to the App.config file, then use
System.Configur ationSettings.A ppSettings to retrieve the value, ex:
string sDsn = ConfigurationSe ttings.AppSetti ngs["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.n ospam> wrote in message
news:13******** *************** ***********@mic rosoft.com...

This is for a Win form.

Currently I have several connection strings that are identical through out my application. Is a global variable the best choice for this
situation? If
so, what is the code to make a global string variable, and where do I
put it,
(before the namespace, of just under it)?????


Nov 17 '05 #9
Mike L <Ca***@nospam.n ospam> wrote:
I this line of code: string sConnString =
ConfigurationSe ttings.AppSetti ngs["dsn"];

I get the error: The type or namespace name 'ConfigurationS ettings' could
not be found (are you missing a using directive or an assembly reference?)


That suggests you're missing the using directive:

using System.Configur ation;

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #10

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

Similar topics

8
102760
by: David Hitillambeau | last post by:
Hi guys, As I am new to Python, i was wondering how to declare and use global variables. Suppose i have the following structure in the same module (same file): def foo: <instructions> <instructions> def bar: <instructions>
4
24189
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
4
7157
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
2
8836
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book { public: Book()
8
2563
by: lawrence | last post by:
I'm learning Javascript. I downloaded a script for study. Please tell me how the variable "loop" can have scope in the first function when it is altered in the second function? It is not defined in global space, therefore it is not a global variable, yes? Even if it was global, how would it get from one function to another? In PHP variables are copied by value. Are they copied by reference in Javascript? <SCRIPT LANGUAGE="JavaScript">
17
5637
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
10
2666
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
3039
by: Ed Jensen | last post by:
I'm having a vexing problem with global variables in Python. Please consider the following Python code: #! /usr/bin/env python def tiny(): bar = for tmp in foo: bar.append(tmp) foo = bar
1
29393
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
112
5499
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
0
9721
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10113
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9195
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5547
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.