Solved

static classes in ASP.NET

Posted on 2009-05-19
2
694 Views
Last Modified: 2012-08-14
I have a static class Utils that defines some extension methods (eg. on String).

How can I use this in my ASP.NET page?   I would like to be calling things like Label1.Text = helloWorld.ToFrobbedString()

I am a newbie at C#, VS, and ASP.NET, so I need instructions about how to make the utility class somehow available to my own customized page class.   How are methods in the Utils class accessed by my ASP.NET codebehind (instantiated) classes?  What are the namespace (Using?) and Visual Studio things I need to do to link?

Utils.cs
========================================================================
namespace Utils
{
  public static class Basics
  {
    public static string ToFrobbedString(this String str)
    {
      return str + "Frobbed";
    }
  }
}
 
 
WebForm11.aspx.cs
========================================================================
namespace Mine
{
  public partial class WebForm11 : System.Web.UI.Page
  {
    protected global::System.Web.UI.WebControls.Label Msg1;
 
    public void Button_Click(Object sender, EventArgs e)
    {
      Msg1.Text = Msg1.Text.ToFrobbedString();
    }
  }
}

Open in new window

0
Comment
Question by:TabbedOut
2 Comments
 
LVL 15

Accepted Solution

by:
OMC2000 earned 500 total points
ID: 24423734
if your static class is a part of the same namespace as your class of ASP.NET page you can call it directly by class name and method name. Otherwise you should either put its qualified name, which includes namespace or declare its namespace by "using" directive. No special actions in VS are necessary in this case

If that static class is created in another project - assembly, you should add reference to that project/assembly in the properties of your ASP.NET project

WebForm11.aspx.cs
========================================================================
namespace Mine
{
  public partial class WebForm11 : System.Web.UI.Page
  {
    protected global::System.Web.UI.WebControls.Label Msg1;
 
    public void Button_Click(Object sender, EventArgs e)
    {
      Msg1.Text = Utils.Basics.ToFrobbedString(Msg1.Text.);
    }
  }
}

or

WebForm11.aspx.cs
========================================================================
using Utils

namespace Mine
{
  public partial class WebForm11 : System.Web.UI.Page
  {
    protected global::System.Web.UI.WebControls.Label Msg1;
 
    public void Button_Click(Object sender, EventArgs e)
    {
      Msg1.Text = Basics.ToFrobbedString(Msg1.Text.);
    }
  }
}
0
 
LVL 9

Expert Comment

by:VikramMullick
ID: 24423778
use
Msg1.Text = Utils.Basic.ToFrobbedString("Test data");
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

827 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