Skip to main content

Posts

Showing posts with the label Static

Simplifying use of Static

Hope everyone must have come across static keyword while doing development, specifically using C#. Static modifier is used to declare static member, which means it belong to the type itself. Well, as part of this article, I’m not going to discuss more about static as it will increase the length of this writeup. public   class  Logger   {         public   static   int  GetLogLevel( string  logType)       {            …       }   }   Above is a sample code snippet wherein, we have a class called Logger and it has a static method called GetLogLevel . Now in order to call this GetLogLevel(…) method, first we have to add the required namespace, where this Logger class has been defined. Something like this, using  Planner.Utilities;    Well, nothing new as of now. Next, let’s have a look at, how to make a call to this static method. int  logLevel = Logger.GetLogLevel(fileLog);   Till here, nothing that bad, but we do have a room to improve our code by making it more readable and cleaner.

Propagating Property Change for Static Properties

While working on any XAML based app, first thing which comes into mind is Binding . There are lot many articles on what is Binding and how it works. Don’t worry, I am not going to repeat all that stuff again. But definitely, I would like to touch upon few things which are base of my today’s write-up. To make any property bindable or let’s say to propagate property change, we usually follow one of the below two options: Implement INotifyPropertyChanged interface or Create an event with name PropertyNameChanged Point to notice here is, both the above options will work only on instance properties. Now what if my property is Static??? INotifyPropertyChanged is not going to work for static properties. None of the above options will make x:Static extension work. What to do now ? No worries, all these hazards can easily be overcome when you will jump to .Net 4.5. Approach 1: Property specific static event for each and every static property Let’s have a look a