Solved

static classes in ASP.NET

Posted on 2009-05-19
2
689 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net VB.net Load contents of a GridView  to Excel 2 27
Get id from json Data with NewtonSoft 3 34
Name space syntax error 12 44
Close form "before" open 3 27
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 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
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

16 Experts available now in Live!

Get 1:1 Help Now