473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Site-wide method implementation

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
Nov 27 '06 #1
3 1113
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" <ne********@microsoft.coma écrit dans le message de news:
uF**************@TK2MSFTNGP06.phx.gbl...
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

Nov 27 '06 #2
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(Text Box1.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
Mike wrote:
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
Nov 27 '06 #3
That certainly helps, Steve. It works, and more importantly, it makes sense
to me. Thanks to Masudur too.

Mike

"Steve B." <st**********@com.msn_swap_msn_and_comwrote in message
news:uI**************@TK2MSFTNGP06.phx.gbl...
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" <ne********@microsoft.coma écrit dans le message de news:
uF**************@TK2MSFTNGP06.phx.gbl...
>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


Nov 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
0
by: HackingPSP | last post by:
I saw a lot of requests for a program like this, so I wrote it. Yeah, my site has "PSP software by Auri" but in this case it means "Pretty Sweet Programming" :) There's both a VS2005 add-in and a...
1
by: HackingPSP | last post by:
I saw a lot of requests for a program like this, so I wrote it. Yeah, my site has "PSP software by Auri" but in this case it means "Pretty Sweet Programming" :) There's both a VS2005 add-in and a...
3
by: DBLWizard | last post by:
Howdy All, Is it possible to have Visual Studio 2005 create a project form an existing hosted website? In other words I want to be able connect via ftp to my website structure and have it pull...
16
by: Ben Sehara | last post by:
Is there any way I can limit the access to my website? I have a site "A" and I want to allow access to it only from site "B" login user. If someone try to access site "A" directory, I want it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.