Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on C#
Message
 
 
À
09/01/2012 11:41:30
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01532477
Message ID:
01532522
Vues:
31
>>>>>>>>>>Hi,
>>>>>>>>>>
>>>>>>>>>>I am getting back to my ASP.NET application and relearning C#.
>>>>>>>>>>
>>>>>>>>>>I have a class defined as PUBLIC SEALED CLASS (I think it is called static class, right?). In this class I have bunch of methods where some of them get value(s) from WEB.CONFIG using the following syntax:
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>string MyString = @ConfiguartionManager.AppSettings["MySetting"];
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>My question is, would I be better off (app runing faster) if I create a property (or a field?) in this class (correct syntax?) and store the value there (when they are retrieved the first time). And then, instead of "getting" the value from WEB.CONFIG, the value would be always available in the property or a field.
>>>>>>>>>>
>>>>>>>>>>Does the above make sense? TIA.
>>>>>>>>>
>>>>>>>>>Sure. Create the connectionstring as the public property of the class for easy access.
>>>>>>>>
>>>>>>>>Thank you.
>>>>>>>
>>>>>>>Dmitry,
>>>>>>>
>>>>>>>(1) A sealed class means you can not inherit from it http://msdn.microsoft.com/en-us/library/88c54tsw.aspx
>>>>>>>
>>>>>>>(2) If the property does not change, add it as a public static readonly property
>>>>>>>
>>>>>>>
>>>>>>>public static readonly string MyString = ConfiguartionManager.AppSettings["MySetting"];
>>>>>>>
>>>>>>
>>>>>>Gregory,
>>>>>>
>>>>>>1. Thank you for explaining to me what I used <g>.
>>>>>>
>>>>>>2. Do I put this line public static readonly.. right below the declaration of the class? e.g.
>>>>>>
>>>>>>
>>>>>>public sealed class MySealedClass
>>>>>>{
>>>>>>       public static readonly string MyString = ConfiguartionManager.AppSettings["MySetting"];
>>>>>>       
>>>>>>       public MySealedClass()
>>>>>>       {
>>>>>>                // contructor logic
>>>>>>       }
>>>>>>
>>>>>>       // ... other methods to follow
>>>>>>
>>>>>>}
>>>>>>
>>>>>>
>>>>>>Is above correct syntax?
>>>>>
>>>>>
>>>>>Think so. You do not have to make it static, but I think static is best. It will only be initialized once and not on every instantiation of MySealedClass
>>>>
>>>>I believe I used the sealed class in the very beginning so that I don't have to instantiate the class. That is, I just call the methods of the class and it works. I will test your approach and see how it works. Thank you.
>>>
>>>
>>>Uhm - a sealed class cannot be inherited from
>>>
>>>A static property/method means that you do not have to instantiate the class before using it
>>
>>Thank you.
>
>
>Dimitry, Sealed and static are two different things. Any method or property that is static belongs to the class and not an instance of the class. If you want or have everything in the class as static, you can make the class static also. Sealed is a different thing and as was said prevents it from being inherited. I use static properties and methods all the time when I only want a single value or don't need to instantiate a class. Such as Utility class that has methods to return untility type values. No need to instantiate a utility class if the methods are static.

Timothy,

Thank you for the explanation. Indeed all of the methods in my sealed class are declared Static. So I suppose I really didn't need to make the class Sealed. But I am not going to change it now (if it is not broken, I don't want to fix it <g>).
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform