Solved

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

Posted on 2007-11-14
5
1,281 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now