?
Solved

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

Posted on 2007-11-14
5
Medium Priority
?
1,287 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 1000 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

765 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