Solved

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

19 Experts available now in Live!

Get 1:1 Help Now