Thursday, March 22, 2012

newbie namespace question

Hi,

I'd like to place a method in a namespace which I can easily call from any of my asp.net pages.
The method formats a string, and I'd like to just call it with a string without instantiating a new object ie:

string Message = "hello world";
Message = MyNameSpace.MyMethod( Message );

How can I do this? Thanks - Katie.To do this is VB you use "Shared." Example:

Public Shared Function MyMethod(ByVal Message As String) as String
'Method body
End Function

-John

"katie" <pe_date@.hotmail.com> wrote in message
news:79C7038A-5CB3-4D52-9E32-69D50C8C2663@.microsoft.com...
> Hi,
> I'd like to place a method in a namespace which I can easily call from any
of my asp.net pages.
> The method formats a string, and I'd like to just call it with a string
without instantiating a new object ie:
> string Message = "hello world";
> Message = MyNameSpace.MyMethod( Message );
> How can I do this? Thanks - Katie.
Hi Katie,

In a method is not a member of a namespace, it is a member of a class. What
you want to do is create a class and create a static member within the
class, this is often referred to as a class member as apposed to an instance
member.

public class StringMethods
{
public static string MyMethod( string message )
{
// Do your thing here...
return ...
}
}

Then invoke the method using the following syntax

string Message = "hello world";
Message = StringMethods.MyMethod( Message );

Hope this helps
--
Chris Taylor
http://dotnetjunkies.com/WebLog/chris.taylor/
"katie" <pe_date@.hotmail.com> wrote in message
news:79C7038A-5CB3-4D52-9E32-69D50C8C2663@.microsoft.com...
> Hi,
> I'd like to place a method in a namespace which I can easily call from any
of my asp.net pages.
> The method formats a string, and I'd like to just call it with a string
without instantiating a new object ie:
> string Message = "hello world";
> Message = MyNameSpace.MyMethod( Message );
> How can I do this? Thanks - Katie.

0 comments:

Post a Comment