Site-wide method implementation

M

Mike

Moving from classic ASP to dotnet (C#) - I have a function (method) that
searches a string and replaces instances of urls and email addresses with
hyperlinks. The method works as I want it to. Currently it resides in the
code-behind for the pages I want it to do its thing on. Note, it is used on
more than one page.

In classic ASP, I would have placed this method as a VBScript function in an
include file and called it for each page I wanted to use it, thus
restricting me to one copy of the function (promoting easier maintenance).

This is my first exposure to OOP, so I'm a bit hazy on the best way to
achieve something similar in dotnet. Do I create a class with just the one
method and stick it in the App_Code folder? How do I then get it to work on
a page?

Also, I'm keen to understand exactly what it is in its current context. Am
I right in believing that I have, by virtue of adding a page called
jobDetails.aspx, created a class called jobDetails, which is based on the
Page class? And if this is correct, have I simply added a new method
(ReplaceLinks) to this class?

Here's the code_behind (omitting the "using" statements):

public partial class jobDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string ReplaceLinks(string InputText)
{
Regex urlregex = new Regex(@"(^|[\n ])(?<url>(www|ftp)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = urlregex.Replace(InputText, "<a href=\"http://${url}\"
target=\"_blank\">${url}</a>");
Regex httpurlregex = new Regex(@"(^|[\n ])(?<url>(http://www)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = httpurlregex.Replace(InputText, "<a href=\"${url}\"
target=\"_blank\">${url}</a>");
Regex emailregex = new
Regex(@"(?<url>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = emailregex.Replace(InputText, "<a
href=\"mailto:${url}\">${url}</a>");
return InputText.Replace("\r", "<br />");
}
}

Thanks

Mike
 
S

Steve B.

When I need such "utility" methods, I use to create a class using this
pattern :

public sealed class MyUtilityClass // sealed ensure your utility class won't
be inherited
{
private MyUtilityClass() {} // Ensure you cannot instanciate an object of
this type

public static string DoTheJob(string arg)
{
return arg.ToLower();
}
}

Then, place this class in app_code and anywher in your web application you
will be able to call MyUtilityClass.DoTheJob(...).

Hope that helps
Steve
 
M

Masudur

Hi Mike,

You are write . You have to create a separate class and then add the
function in that class.
These classes are helper class. and yes you can put it in app_code
folder.
Let Me give you a short example
lets say we named the class myLinkHelper.cs
and we want to add the method in it.
it is better to add a static method so we add the function ReplaceLinks
in that class

namespace MyWebSite.App_Code
{
public class MyHelperClass
{
public static string ReplaceLinks(string Text)
{
// code logic will go here
}
}
}


and from the page where you want to use it ... just call the method.

MyWebSite.App_Code.MyHelperClass.ReplaceLinks(TextBox1.Text);

where Textbox1.Text will be your input value... or you can pass any
string object.

Thanks.

Md. Masudur Rahman
Kaz Software Ltd.
www.kaz.com.bd

Moving from classic ASP to dotnet (C#) - I have a function (method) that
searches a string and replaces instances of urls and email addresses with
hyperlinks. The method works as I want it to. Currently it resides in the
code-behind for the pages I want it to do its thing on. Note, it is used on
more than one page.

In classic ASP, I would have placed this method as a VBScript function in an
include file and called it for each page I wanted to use it, thus
restricting me to one copy of the function (promoting easier maintenance).

This is my first exposure to OOP, so I'm a bit hazy on the best way to
achieve something similar in dotnet. Do I create a class with just the one
method and stick it in the App_Code folder? How do I then get it to work on
a page?

Also, I'm keen to understand exactly what it is in its current context. Am
I right in believing that I have, by virtue of adding a page called
jobDetails.aspx, created a class called jobDetails, which is based on the
Page class? And if this is correct, have I simply added a new method
(ReplaceLinks) to this class?

Here's the code_behind (omitting the "using" statements):

public partial class jobDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string ReplaceLinks(string InputText)
{
Regex urlregex = new Regex(@"(^|[\n ])(?<url>(www|ftp)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = urlregex.Replace(InputText, "<a href=\"http://${url}\"
target=\"_blank\">${url}</a>");
Regex httpurlregex = new Regex(@"(^|[\n ])(?<url>(http://www)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = httpurlregex.Replace(InputText, "<a href=\"${url}\"
target=\"_blank\">${url}</a>");
Regex emailregex = new
Regex(@"(?<url>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = emailregex.Replace(InputText, "<a
href=\"mailto:${url}\">${url}</a>");
return InputText.Replace("\r", "<br />");
}
}

Thanks

Mike
 
M

Mike

That certainly helps, Steve. It works, and more importantly, it makes sense
to me. Thanks to Masudur too.

Mike

Steve B. said:
When I need such "utility" methods, I use to create a class using this
pattern :

public sealed class MyUtilityClass // sealed ensure your utility class
won't be inherited
{
private MyUtilityClass() {} // Ensure you cannot instanciate an object of
this type

public static string DoTheJob(string arg)
{
return arg.ToLower();
}
}

Then, place this class in app_code and anywher in your web application you
will be able to call MyUtilityClass.DoTheJob(...).

Hope that helps
Steve

Mike said:
Moving from classic ASP to dotnet (C#) - I have a function (method) that
searches a string and replaces instances of urls and email addresses with
hyperlinks. The method works as I want it to. Currently it resides in
the code-behind for the pages I want it to do its thing on. Note, it is
used on more than one page.

In classic ASP, I would have placed this method as a VBScript function in
an include file and called it for each page I wanted to use it, thus
restricting me to one copy of the function (promoting easier
maintenance).

This is my first exposure to OOP, so I'm a bit hazy on the best way to
achieve something similar in dotnet. Do I create a class with just the
one method and stick it in the App_Code folder? How do I then get it to
work on a page?

Also, I'm keen to understand exactly what it is in its current context.
Am I right in believing that I have, by virtue of adding a page called
jobDetails.aspx, created a class called jobDetails, which is based on the
Page class? And if this is correct, have I simply added a new method
(ReplaceLinks) to this class?

Here's the code_behind (omitting the "using" statements):

public partial class jobDetails : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string ReplaceLinks(string InputText)
{
Regex urlregex = new Regex(@"(^|[\n ])(?<url>(www|ftp)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = urlregex.Replace(InputText, "<a href=\"http://${url}\"
target=\"_blank\">${url}</a>");
Regex httpurlregex = new Regex(@"(^|[\n ])(?<url>(http://www)\.[^
,""\s<]*)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = httpurlregex.Replace(InputText, "<a href=\"${url}\"
target=\"_blank\">${url}</a>");
Regex emailregex = new
Regex(@"(?<url>[a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
InputText = emailregex.Replace(InputText, "<a
href=\"mailto:${url}\">${url}</a>");
return InputText.Replace("\r", "<br />");
}
}

Thanks

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top