p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   static Class in ASP (http://p2p.wrox.com/showthread.php?t=36976)

chafor January 9th, 2006 02:50 PM

static Class in ASP
 
Hi, I want to make a static class for my special function like MD5String. I'm pretty sure of my syntaxe but the compilator says on then line (public static class CMD5) : "c:\inetpub\wwwroot\HelloWorld\CMD5.cs(8): Le modificateur 'static' n'est pas valide pour cet élément" or in english : "The modificator 'static' is not valide for this element"

    public static class CMD5
    {
        public static string MD5String(string password)
        {
            MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvi der();
            byte[] bs = Encoding.UTF8.GetBytes(password);
            bs = x.ComputeHash(bs);
            StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            password = s.ToString();
            return password;
        }
    }


chafor January 9th, 2006 04:27 PM

The answer is simple I needed to use
public sealed class CMD5
instead of
public static class CMD5


planoie January 11th, 2006 01:30 PM

Note that the change you made wasn't really a 'fix' per se.

You can not use the modifier "static" on a class. You could say that technically, all classes are static, because they are type definitions. You can (as you did) make a METHOD static so you can reference by type without the need for an object instance.

The "sealed" modifier means that your class can not be inherited.

-Peter

chafor January 11th, 2006 04:26 PM

I have taken this exemple directly from MSDN:

public static class TemperatureConverter
{
    public static double CelsiusToFahrenheit(string temperatureCelsius)
    {
        // Convert argument to double for calculations.
        double celsius = System.Double.Parse(temperatureCelsius);

        // Convert Celsius to Fahrenheit.
        double fahrenheit = (celsius * 9 / 5) + 32;

        return fahrenheit;
    }

    public static double FahrenheitToCelsius(string temperatureFahrenheit)
    {
        // Convert argument to double for calculations.
        double fahrenheit = System.Double.Parse(temperatureFahrenheit);

        // Convert Fahrenheit to Celsius.
        double celsius = (fahrenheit - 32) * 5 / 9;

        return celsius;
    }
}

Why can they maked the class static? Is it because it's in ASP.NET2.0?

And I fogot;) Thanks

planoie January 13th, 2006 10:58 AM

Here's the current documentation on the "class" keyword:

http://msdn.microsoft.com/library/en...eclasstype.asp

From the docs:
"modifiers (Optional)
    The allowed modifiers are new, abstract, sealed, and the four access modifiers."

I can't find anything regarding using the 'static' keyword but admittedly, I can't seem to get at the 2.0 msdn docs. I must not be awake yet.

-Peter


All times are GMT -4. The time now is 04:21 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.