473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to make a variable accessible to the whole application?

I'm developing a Windows application and I'm having some problems in
making variable accessible to the whole application. I use VS.NET 2005
and I create a database connection object in the method private void
connect_Click(o bject sender, EventArgs e). That object I create seems
to be accessible only within that method, how do I make it accessible
to the whole application, just like I have access to mytextbox.text
throughout my tabbed form? Where do you keep your connection object
usually?

Thank you.

Nov 7 '06 #1
4 10900
C# does not support global variables. You can declare variables outside of
functions. These are called member variables.

class Fake
{
public int MyInt;
}

Anything accessing Fake.MyInt can read it. If you have a function in the
class, it can also read MyInt.

"ad*********@gm ail.com" wrote:
I'm developing a Windows application and I'm having some problems in
making variable accessible to the whole application. I use VS.NET 2005
and I create a database connection object in the method private void
connect_Click(o bject sender, EventArgs e). That object I create seems
to be accessible only within that method, how do I make it accessible
to the whole application, just like I have access to mytextbox.text
throughout my tabbed form? Where do you keep your connection object
usually?

Thank you.

Nov 7 '06 #2

<ad*********@gm ail.comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
I'm developing a Windows application and I'm having some problems in
making variable accessible to the whole application. I use VS.NET 2005
and I create a database connection object in the method private void
connect_Click(o bject sender, EventArgs e). That object I create seems
to be accessible only within that method, how do I make it accessible
to the whole application, just like I have access to mytextbox.text
throughout my tabbed form? Where do you keep your connection object
usually?
You would do that with a class member variable.

You are not ready for database access if you are unfamiliar with basic
programming concepts such as scope. I suggest that you complete a tutorial
that teaches structured programming and object-oriented programming in C#
before continuing work on your application.
>
Thank you.

Nov 7 '06 #3
I'm one of those guys who don't like public variables. My solution is this:
Create a database class in which you store database members such as
connectionstrin g property/variable, Open() methods etc. In the starting form
I create an instance of this database class. When another form needs access
to the database, I parse the instance using the tag property tag of the
form.

NewForm.Tag = (object) myDatabase;

Within the new form you can use
.... = (Database) this.tag.

hope you can use it

DD
<ad*********@gm ail.comschreef in bericht
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
I'm developing a Windows application and I'm having some problems in
making variable accessible to the whole application. I use VS.NET 2005
and I create a database connection object in the method private void
connect_Click(o bject sender, EventArgs e). That object I create seems
to be accessible only within that method, how do I make it accessible
to the whole application, just like I have access to mytextbox.text
throughout my tabbed form? Where do you keep your connection object
usually?

Thank you.

Nov 7 '06 #4
Just add a separate class file to your application and add in all the
required fields as public with the "static" modifier.
Then you can refer to these from anywhere with "MyStuff.FieldN ame".
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"ad*********@gm ail.com" wrote:
I'm developing a Windows application and I'm having some problems in
making variable accessible to the whole application. I use VS.NET 2005
and I create a database connection object in the method private void
connect_Click(o bject sender, EventArgs e). That object I create seems
to be accessible only within that method, how do I make it accessible
to the whole application, just like I have access to mytextbox.text
throughout my tabbed form? Where do you keep your connection object
usually?

Thank you.

Nov 7 '06 #5

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

Similar topics

0
3821
by: Chris | last post by:
Hi all, I am unable to make a functional com+ application proxy from a .net com application. I made the .net com+ application using code from the "Using Com+ Services in .Net" article in the MSDN library. The proxy MSI file can be created using Component Services however when I try to install it on client machines by installing the MSI file directly I get a message which reads "Error registering Com+ application". If I try to install the...
7
2554
by: tedqn | last post by:
I keep getting the error of not shared member or something. Basically within a class, I have a constructor that takes in an sql connection string and init a connection object to be accessible by all members instead of init several connections locally for each member function . Help! public class CommonUtilObj { public SqlConnection g_objConnection; //global connection variable // constructor takes in a connection string and open the...
3
1093
by: Daniel Friend | last post by:
I have an application and use custom user controls as plugins. Is there any way to communicate back and forth from control to app. NOTE: The control is not refrenced in the app, it will act as a plugin and will be dynamically loaded to the app. Example: 1. My application as a public variable 2. The control must receieve the value of the public variable 3. The control will do some math and encryption of the variable
11
7792
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can access the constant values? Thanks Bill
4
1235
by: Omega | last post by:
I just recently recieved my full copy of Visual Studio 2005 (excited!!!), and have gone head first into writing my first application. Sadly, I've hit a design barrier. >From what I can see, ASP.NET 2.0 has no facility that is instantiated upon deploying an application - similar to servlets. I don't really need the functionality of a servlet, but what I am interested in getting is an "always on" class that exists between different...
1
3203
by: TheSteph | last post by:
Hi ! - Using C# .NET 2.0 - How can I change the Cursor of THE WHOLE APPLICATION ?
13
1722
by: michel | last post by:
Hi, i created a class 'test' with a method 'descrlimit()' (no matter). That method is used in a lot of pages in the application, so i need to put this code a lot of time: "Dim odescr As New test" In order to avoid that, i wonder whether it would be possible to put that line in a central place, like global.asax. I tried this:
3
3490
by: Peted | last post by:
I have an application with mdi parent form, and mutliple child forms. In the mdi parent form i want to instantiate a device class for comunicating to a peice of hardware via serial port. its constructor would look something like Device kvm = new Device(Serialport);
12
3128
by: mylixes | last post by:
I have a Windows Application. I just don't understand why is it that when i click the Close button, instead of closing the form, it minimizes the whole application. I don't experience this on my previous runs. It's weird that I am now experiencing it. It's awkward also that I should restore the whole application to begin another transaction. Below is my code on Close button event. private void btnClose_Click(object sender, EventArgs e)...
0
9715
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
9595
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
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10353
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10356
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9176
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
5536
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...
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.