Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Once more about macros in C#

, 11 May 2010 Apache
Rate this:
Please Sign up or sign in to vote.
I am writing a WPF app. In WPF they like very much all kinds of notifications about property changes. Thus, I’ve got a class with a bunch of properties similar to this:    class Person    {        void OnPropertyChanged(string propertyName)        {        …        }         public str

I am writing a WPF app. In WPF they like very much all kinds of notifications about property changes. Thus, I’ve got a class with a bunch of properties similar to this:

    class Person
    {
        void OnPropertyChanged(string propertyName)
        {
        …
        }
 
        public string LastName
        {
            get { return _lastName; }
 
            set
            {
                if (_lastName != value)
                {
                    _lastName = value;
                    OnPropertyChanged("LastName");
                }
            }
        }
        string _lastName;
 
        public int Age
        {
            get { return _age; }
 
            set
            {
                if (_age != value)
                {
                    _age = value;
                    OnPropertyChanged("Age");
                }
            }
        }
        int _age;
    }

This is long, boring, and bloated. Of course, I created a code snipped for generating the properties, but it works only once, at the time of writing, the code is still bloated, and all the changes must be done by hand. Besides, it is unable to change case automatically, so that Age becomes _age.

I would like to see something like:

    class MyClass
    {
        public Observable<string> LastName;
        public Observable<int> Age;
    }
 
    property Observable<T>
    { 
        T _value;
        get { return _value; }
        set
        {
            if (_value != value)
            {
                _value = value;
                OnPropertyChanged(#PropertyName);
            }
        }
    }

Of course, the syntax is approximate. Unfortunately, nothing of the sort exists. Generics are of no help here, and there are no macros in C#.

License

This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0

Share

About the Author

Ivan Krivyakov
Technical Lead Thomson Reuters
United States United States
Ivan is a hands-on software architect/technical lead working for Thomson Reuters in the New York City area. At present I am mostly building complex multi-threaded WPF application for the financial sector, but I am also interested in cloud computing, web development, mobile development, etc.
 
Please visit my web site: www.ikriv.com.

Comments and Discussions

 
GeneralAn implementation PinmemberLeung Yat Chun12-May-10 20:08 
GeneralRe: An implementation PinmemberIvan Krivyakov13-May-10 8:25 
GeneralRe: An implementation PinmemberLeung Yat Chun13-May-10 21:13 
GeneralRe: An implementation PinmemberIvan Krivyakov14-May-10 9:11 
AnswerRe: An implementation PinmemberLeung Yat Chun5-Jun-10 9:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.1 | Last Updated 11 May 2010
Article Copyright 2010 by Ivan Krivyakov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid