|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
July 22nd, 2003, 01:39 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
global variable
hi there:
I need to store my ConnectionString in a variable witch should be accessible from all windows!?! I know there is a way with adding class & some methods & ... but I dont know how to manage it!(its only a idea) please help me to do this!!
Any help appreciated.
Always:),
Hovik Melkomian.
__________________
Always,
Hovik Melkomian.
|
July 22nd, 2003, 08:50 AM
|
Authorized User
|
|
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I usually create a class named Constants in my projects, and stick all the "globals" in there as public static fields.
Thus, for instance
namespace myProject
{
public class Constants
{
public static string CONNSTRING = "myConnection";
}//Closes Constants class
}//Closes myProject namespace
Then, in another clas sin the myProject namespace, you can get that
string as Constants.CONNSTRING.
Just an idea,
but I bet there are better ways.
HTH,
Olorin
|
July 23rd, 2003, 12:08 AM
|
Registered User
|
|
Join Date: Jul 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Put ConnectionString inside the app.config file and load from there.
|
July 23rd, 2003, 12:52 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
no dear it isnt a good idea since my connection is dynamic & I genarate it?! tnx again for reply.
Always:),
Hovik Melkomian.
|
July 24th, 2003, 02:12 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
yes! what I need is something like that but it gives me run time error!! how do u see to add a class to project & ...
can u help me?!
Any help appreciate;)
Always:),
Hovik Melkomian.
|
July 26th, 2003, 01:05 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Hi Hovik
It could be a class like this one.
public class Globals
{
private string _ConnectionString;
public Globals()
{
}
public string ConnectionString
{
get
{
return _ConnectionString;
}
set
{
_ConnectionString = value;
}
}
}
Make sure that this class resides in the same namespace
where all the other classes that are gonna use this
class are.
You can add the set/get pairs for all the other global variables
that you wanna use in your project.
You can use this class this way.
private Globals globs = null;
.
.
.
globs = new Globals();
globs.ConnectionString =
"this is the connection string string set by me.";
.
.
.
MessageBox.Show(globs.ConnectionString);
Write back if there is anything else you wanna ask.
Ankur Verma
.Net and C++ Specialist
Wiley Tech Support
|
July 26th, 2003, 06:21 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
its ok till now!
But should I new it in every form that I need it?!
my problem is here:)
Always:),
Hovik Melkomian.
|
July 26th, 2003, 07:13 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
my project name is Dabir so my created class is like this:
using System;
namespace Dabir
{
/// <summary>
/// Summary description for Const.
/// </summary>
public class Const
{
private string ConnDabir;
public Const()
{
//
// TODO: Add constructor logic here
//
}
public string Conn
{
set
{
ConnDabir = value;
}
get
{
return ConnDabir;
}
}
}
}
is it still correct,
so now tell me how to use it in other form?! if I new it it'll make all valiables new & null for my Connection string also!
Tell me what to do?!
Always:),
Hovik Melkomian.
|
March 29th, 2004, 04:33 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
|
|
Hi again:
I see an sample in net witch ut was using Component to use only one connection in whole App.
now I've created component & insered Connection obj. there, but cant succeed to access its connection & ...
Can any1 help, any help appreciated & tnx in advance.
Always:),
Hovik Melkomian.
|
March 29th, 2004, 04:45 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Hovik,
Can you define "but cant succeed to access its connection"? What is not working? Do you get an error? If so, what's the error message?
How did you set up your project? Where does the component live and with what kind of application are you trying to access it?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|
|