Solved

static classes in ASP.NET

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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