Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Newbie question on C#
Message
 
 
À
09/01/2012 11:18:09
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
Divers
Thread ID:
01532477
Message ID:
01532501
Vues:
28
>>>>>>>>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.
"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
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform