Subject: static Class in ASP
Posted By: chafor Post Date: 1/9/2006 1:50:42 PM
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.MD5CryptoServiceProvider();
            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;
        }
    }

Reply By: chafor Reply Date: 1/9/2006 3:27:17 PM
The answer is simple I needed to use
public sealed class CMD5
instead of
public static class CMD5

Reply By: planoie Reply Date: 1/11/2006 12:30:42 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
Reply By: chafor Reply Date: 1/11/2006 3:26:11 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
Reply By: planoie Reply Date: 1/13/2006 9:58:01 AM
Here's the current documentation on the "class" keyword:

http://msdn.microsoft.com/library/en-us/csref/html/vcreftheclasstype.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

Go to topic 38668

Return to index page 396
Return to index page 395
Return to index page 394
Return to index page 393
Return to index page 392
Return to index page 391
Return to index page 390
Return to index page 389
Return to index page 388
Return to index page 387