Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Newbie question on C#
Message
From
09/01/2012 11:41:30
Timothy Bryan
Sharpline Consultants
Conroe, Texas, United States
 
General information
Forum:
ASP.NET
Category:
Other
Environment versions
Environment:
C# 2.0
Miscellaneous
Thread ID:
01532477
Message ID:
01532518
Views:
42
Likes (1)
>>>>>>>>>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 Bryan
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform