Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

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!
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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