?
Solved

static classes in ASP.NET

Posted on 2009-05-19
2
Medium Priority
?
708 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 1500 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

807 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