Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
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
 
Old July 22nd, 2003, 01:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default 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.
 
Old July 22nd, 2003, 08:50 AM
Authorized User
 
Join Date: Jun 2003
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old July 23rd, 2003, 12:08 AM
Registered User
 
Join Date: Jul 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Put ConnectionString inside the app.config file and load from there.


 
Old July 23rd, 2003, 12:52 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

no dear it isnt a good idea since my connection is dynamic & I genarate it?! tnx again for reply.

Always:),
Hovik Melkomian.
 
Old July 24th, 2003, 02:12 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.
 
Old July 26th, 2003, 01:05 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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
 
Old July 26th, 2003, 06:21 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

its ok till now!
But should I new it in every form that I need it?!
my problem is here:)

Always:),
Hovik Melkomian.
 
Old July 26th, 2003, 07:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.
 
Old March 29th, 2004, 04:33 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

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.
 
Old March 29th, 2004, 04:45 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
comapring global variable value to local variable amhicraig XSLT 6 December 5th, 2007 12:16 PM
Global Variable techfreak123 XSLT 0 August 3rd, 2006 07:45 AM
global variable? milk_vanilla Classic ASP Basics 3 January 8th, 2005 06:50 PM
global variable in c# kobystud C# 3 April 22nd, 2004 05:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.