473,378 Members | 1,493 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,378 software developers and data experts.

Global Objects

Hi,

I'm a PHP programmer who has been hired by a company to do web
applications, but they want me to use ASP .NET. For the most part, the
transition has been relatively smooth. However, I have a question:

In PHP, I would create a class that I called DBContol that would be the
intermediary for all communications between my web application and
Database. I would execute a command something like this:

<?
// This
$GLOBALS["mainDB"] = new DBControl("DatabaseName");

// or I would store it in the session
$_SESSION["mainDB"] = new DBControl("DatabaseName);

// Then I would be able to execute commands something like this:

$_SESSION["mainDB"]->query($queryString);
?>

Now, in ASP .NET I know there is a Global.asax file that you create
that is the "root" of the application and is the very first executed,
depending on what EventHandler you use. You also use an <objecttag to
declare objects used across the application.

The problem is, I can't really get this to work. So here is my
question:

How can I create an instance of a class in my application that can
access that object where ever needed in the code, how do I access it
and run it's methods, and make sure that its a sound and secure way of
doing this. I've looked for tutorials, and they all talk about storing
variables globally, but I need to access this instance of a class no
matter where in the code.

Any suggestions? Thanks in advance for any replies.

Jan 16 '07 #1
2 1110
Also, some of the errors I experiences while trying this.

I would set the <objecttag in the Global.asax, and later in the
program I tried to do this in the Global.asax's Application_Start():

void Application_Start(object sender, EventArgs e)
{
mainDB = new DBControl("mainDB");
}

and it told me that mainDB was read only.

I'm still going to play with it and keep checking back and posting
progress.

Jan 16 '07 #2
To answer your question, in Application_Start you would;

Application["myDB"] = new DBControl("mainDB");

Then when you need to use it on a page;

DBControl mainDB = (DBControl) Application["myDB"];
mainDB.SomeMethod();

However you are better making sure the methods on your DBControl class are
all static and don't persists any data in the class itself across method
calls. That way you can simply use this code;

DBControl.SomeMethod();

anywhere in your code without the need to store anything globally.

You can combine the two techniques using the "singleton pattern" (google
it).

However do look to using static methods on your class and get away ftom the
whole "global variable" methodology.

"JustinCarmony" <ca*****@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Hi,

I'm a PHP programmer who has been hired by a company to do web
applications, but they want me to use ASP .NET. For the most part, the
transition has been relatively smooth. However, I have a question:

In PHP, I would create a class that I called DBContol that would be the
intermediary for all communications between my web application and
Database. I would execute a command something like this:

<?
// This
$GLOBALS["mainDB"] = new DBControl("DatabaseName");

// or I would store it in the session
$_SESSION["mainDB"] = new DBControl("DatabaseName);

// Then I would be able to execute commands something like this:

$_SESSION["mainDB"]->query($queryString);
?>

Now, in ASP .NET I know there is a Global.asax file that you create
that is the "root" of the application and is the very first executed,
depending on what EventHandler you use. You also use an <objecttag to
declare objects used across the application.

The problem is, I can't really get this to work. So here is my
question:

How can I create an instance of a class in my application that can
access that object where ever needed in the code, how do I access it
and run it's methods, and make sure that its a sound and secure way of
doing this. I've looked for tutorials, and they all talk about storing
variables globally, but I need to access this instance of a class no
matter where in the code.

Any suggestions? Thanks in advance for any replies.

Jan 16 '07 #3

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

Similar topics

1
by: Oystein Haare | last post by:
I'm thinking about sort of a factory-system where the factories get instanciated on program start (through a global object in the .cpp file), and registers themselves with a singelton. Is it a...
1
by: Oystein Haare | last post by:
Note: This might be a bit off topic.. I want to create some global objects that register themselves with another "manager"-class upon creation: class SomeClass { ... }; // cpp:
2
by: Gianguz | last post by:
I'd like to discuss about the opportunity to have a global objects creator that introduces into a general framework (suited for multithreading) a controlled semantic to manage globals variables...
8
by: john townsley | last post by:
whats the best way to declare Global objects (holding say 10 member variables and functions) of an unknown size in c++, memory efficient as possible thanks
6
by: MechSoft | last post by:
Hi, I am new to C++ from C, I am a bit confused about using global objects in C++. I have a program that need to share some data(held in classes) between files, and of course I thought about using...
0
by: MechSoft | last post by:
Hi, there, I have an issue about globally access data. My multi-threaded application has some resources that need to be accessed by multiple threads and have the lifetime of the application,...
5
by: Chris Hughes | last post by:
I have an environment with many thousands of client machines uploading data files several times each day to a web server via HTTP PUT. To avoid disk I/O (for performance), I am implementing a...
4
by: Shadowboxer | last post by:
What happened to global objects? I want to make the main form accessible throughout all the classes as a single instance, right now I have to make a cludgy pass back through the class structure...
11
by: Sanjay | last post by:
Probably a newcomer question, but I could not find a solution. I am trying to have some singleton global objects like "database connection" or "session" shared application wide. Trying hard, I...
2
by: Ben Collver | last post by:
What is the JavaScript equivalent of the forth words word, or the TCL command? I am using OSSP SpiderMonkey as my JavaScript host and I would like to list the global objects such as Number,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.