473,396 Members | 1,599 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.

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 4427
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.nospam> wrote in message
news:13**********************************@microsof t.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.nospam> a écrit dans le message de news:
13**********************************@microsoft.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.MyValue;
...
}

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.nospam> wrote in message
news:13**********************************@microsof t.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.nospam> wrote in message
news:13**********************************@microsof t.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.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.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" ?>

<configuration>

<appSettings>

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

</appSettings>

</configuration>

"Mike L" <Ca***@nospam.nospam> wrote in message
news:D7**********************************@microsof t.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.nospam> wrote in message
news:13**********************************@microsof t.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 =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)
I also tried: string sConnString = configuration.appSetting["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" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
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.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.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.Configuration.ConfigurationSettings.AppSett ings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

"Mike L" <Ca***@nospam.nospam> wrote in message
news:06**********************************@microsof t.com...
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)
I also tried: string sConnString = configuration.appSetting["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" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
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.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.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.nospam> wrote:
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

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


That suggests you're missing the using directive:

using System.Configuration;

--
Jon Skeet - <sk***@pobox.com>
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
Hi Joanna.
What is a static class in C#? Please tell us... <vbg>

Anyways, I think would simply do this:

public class DataAccess
{
public static readonly string ConnectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
}

Or better, provide a simple factory for connections.

public class DataAccess
{
private DataAccess() { }
private static readonly string connectionString =
ConfigurationSettings.AppSettings["ConnectionString"];
public static SqlConnection Create()
{
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
return conn;
}
}

Happy Coding
- Michael S


"Joanna Carter (TeamB)" <jo*****@nospamforme.com> wrote in message
news:eR*************@tk2msftngp13.phx.gbl...
"Mike L" <Ca***@nospam.nospam> a écrit dans le message de news:
13**********************************@microsoft.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.MyValue;
...
}

Joanna

--
Joanna Carter
Consultant Software Engineer

Nov 17 '05 #11
Now I get this error:

An unhandled exception of type 'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting


Here is my code, password as be removed for security.

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

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; is on Load of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config

"Mark White" wrote:
Mike

It would be: System.Configuration.ConfigurationSettings.AppSett ings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

"Mike L" <Ca***@nospam.nospam> wrote in message
news:06**********************************@microsof t.com...
I this line of code: string sConnString =
ConfigurationSettings.AppSettings["dsn"];

I get the error: The type or namespace name 'ConfigurationSettings' could
not be found (are you missing a using directive or an assembly reference?)
I also tried: string sConnString = configuration.appSetting["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" ?>
<configuration>

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
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.ConfigurationSettings.AppSettings to retrieve the value, ex:
string sDsn = ConfigurationSettings.AppSettings["dsn"];

Regards,
Mark
"Mike L" <Ca***@nospam.nospam> wrote in message
news:13**********************************@microsof t.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 #12
it is called <appSettings> NOT <appsetting>

HTH

Ollie Riches

"Mike L" <Ca***@nospam.nospam> wrote in message
news:C1**********************************@microsof t.com...
Now I get this error:

An unhandled exception of type
'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting


Here is my code, password as be removed for security.

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

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; is on Load
of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config

"Mark White" wrote:
Mike

It would be:
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

"Mike L" <Ca***@nospam.nospam> wrote in message
news:06**********************************@microsof t.com...
> I this line of code: string sConnString =
> ConfigurationSettings.AppSettings["dsn"];
>
> I get the error: The type or namespace name 'ConfigurationSettings'
> could
> not be found (are you missing a using directive or an assembly
> reference?)
>
>
> I also tried: string sConnString = configuration.appSetting["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" ?>
> <configuration>
>
> <appSetting>
>
> <add key="dsn" value="Data Source=db;Database=License;Integrated
> 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.ConfigurationSettings.AppSettings to retrieve the value, ex:
> > string sDsn = ConfigurationSettings.AppSettings["dsn"];
> >
> > Regards,
> > Mark
> >
> >
> > "Mike L" <Ca***@nospam.nospam> wrote in message
> > news:13**********************************@microsof t.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 #13
Michael S <a@b.c> wrote:
What is a static class in C#? Please tell us... <vbg>


In C# 2.0, it's a class which is declared as being static. It is
implicitly sealed, has *no* constructors (private or public), and all
its members have to be static.

--
Jon Skeet - <sk***@pobox.com>
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 #14
Once I corrected my App.config file to appSettings, it worked!!!

Thank you everyone!!

"Ollie Riches" wrote:
it is called <appSettings> NOT <appsetting>

HTH

Ollie Riches

"Mike L" <Ca***@nospam.nospam> wrote in message
news:C1**********************************@microsof t.com...
Now I get this error:

An unhandled exception of type
'System.Configuration.ConfigurationException'
occurred in system.dll

Additional information: Unrecognized configuration section appSetting


Here is my code, password as be removed for security.

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

<appSetting>

<add key="dsn" value="Data Source=db;Database=License;Integrated
Security=False;User ID=sa;password=REMOVED" />

</appSetting>

</configuration>

I have tried both, string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; and string
sConnString = ConfigurationSettings.AppSettings["dsn"];
I get the error on both.

Added info, if this helps. The call to string sConnString =
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]; is on Load
of
my Win Form named frmDataEntry, and the file name for storing the value is
named App.config

"Mark White" wrote:
Mike

It would be:
System.Configuration.ConfigurationSettings.AppSett ings["dsn"]
-OR-
using System.Configuration;
then in the code you can use: ConfigurationSettings.AppSettings["dsn"];

"Mike L" <Ca***@nospam.nospam> wrote in message
news:06**********************************@microsof t.com...
> I this line of code: string sConnString =
> ConfigurationSettings.AppSettings["dsn"];
>
> I get the error: The type or namespace name 'ConfigurationSettings'
> could
> not be found (are you missing a using directive or an assembly
> reference?)
>
>
> I also tried: string sConnString = configuration.appSetting["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" ?>
> <configuration>
>
> <appSetting>
>
> <add key="dsn" value="Data Source=db;Database=License;Integrated
> 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.ConfigurationSettings.AppSettings to retrieve the value, ex:
> > string sDsn = ConfigurationSettings.AppSettings["dsn"];
> >
> > Regards,
> > Mark
> >
> >
> > "Mike L" <Ca***@nospam.nospam> wrote in message
> > news:13**********************************@microsof t.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 #15
Oh.

I and was sitting hare having a laugh at Joanna (as she is seldom wrong).
Sorry Joanna and thanks Jon.

Happy Static
- Michael S

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Michael S <a@b.c> wrote:
What is a static class in C#? Please tell us... <vbg>


In C# 2.0, it's a class which is declared as being static. It is
implicitly sealed, has *no* constructors (private or public), and all
its members have to be static.

--
Jon Skeet - <sk***@pobox.com>
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 #16


hi

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #17
hi

"nikita parekh" <ni***********@indiatimes.com> wrote in message
news:er**************@TK2MSFTNGP10.phx.gbl...


hi

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #18

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

Similar topics

8
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>...
4
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(); ...
4
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
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 {...
8
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...
17
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...
10
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...
9
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
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...
112
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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.