Solved

Where to declare read only static global class (ASP.NET 2.0 / C#)?

Posted on 2007-11-14
5
1,285 Views
Last Modified: 2013-11-07
I want to make a global static class available to my aspx.cs pages. Should I declare this class in a separate .cs file in the App_Code folder? How do I call it from the aspx.cs page if I don't initialize the class? I want to make several different global classes and tell different aspx.cs pages which one to choose and use. It's really for my class below. Can someone help?

I want the aspx.cs to either pick:
(ConfigXML)WebConfigurationManager.GetSection("Test_id1");

Or:
(ConfigXML)WebConfigurationManager.GetSection("Test_id2");

Or:
(ConfigXML)WebConfigurationManager.GetSection("Test_id3");

I want to code my aspx.cs to tell it which one to do.
0
Comment
Question by:bemara57
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 33

Expert Comment

by:raterus
ID: 20282622
All classes must first be instantiated before they can be used.  However you can create static methods in those classes, and use them without instantiating the class.  Is that what you mean?
0
 

Author Comment

by:bemara57
ID: 20282636
Yes, because these values will never change unless the web app is restarted. They are global settings for the website for any user to use.
0
 
LVL 7

Expert Comment

by:NauticalNonsense
ID: 20282652
You mean something like this?

App_Code/MyClass.cs
===============================================
using (whatevers)

namespace MyProject
{
public class MyClass
{
pPublic static void MyFunction(string sSection)
{
(ConfigXML)WebConfigurationManager.GetSection(sSection);
}
}
}


Default.asp.cs
===============================================
Page_Load

MyProject.MyClass.MyFunction("Test_id1");
0
 
LVL 33

Accepted Solution

by:
raterus earned 250 total points
ID: 20282676
In that case, I would make a normal class (not static), and in Application_Start in Global.asax instantiate this class with the values you want, then store it in the Application where any request can access it.

I don't think you need a static class for this.

Not sure about exactly what you are storing, but you might also be able to use appSettings in web.config
0
 
LVL 7

Assisted Solution

by:NauticalNonsense
NauticalNonsense earned 250 total points
ID: 20282744
I'm with raterus, the specifics of what you are trying to do are a little unknown, .. using the global.asax is definitely a way to go.

The other thing that you can do is have your pages inherit from a base class.

App_Code/BaseClass.cs
(usings)
namespace MyProject
{
public class Page : System.Web.UI.Page //inherit page class
{
private string _sSomething= "";
public string sSomething
{ get { return _sSomething; } set { _sSomething= value; } }
private void Page_Load(object sender, System.EventArgs e)
{
// set whatever you want.
}
}}

and then in your default.aspx.cs (or any pages)

(usings)
namespace MyProject
{
public partial class Default: MyProject.Page // inherit from base page class
{
protected void Page_Load(object sender, System.EventArgs e)
{
this.sSomething = "foo";
}
}
}

Which I've used to give me great flexibility in setting up my pages consistenly, and allowing each page to access global items that do change inbetween app restarts.

If this isn't what you're trying to do, disregard :)
0

Featured Post

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question