Solved

static classes in ASP.NET

Posted on 2009-05-19
2
697 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

749 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