C# Object Initializer Trick (Update1)

by rsutton 19. October 2009 08:29

UPDATE: Nevermind this doesn’t work.  It passes the compiler, but gives a runtime error.  Sorry.

 

Today I found an interesting trick you can do with object initializers that I hadn’t noticed before.

    1 
    2     class MyClass
    3     {
    4         public int Id { get; set; }
    5         public string Description { get; set; }
    6         public List<string> FirstList { get; set; }
    7         public List<string> SecondList { get; set; }
    8     }
    9 
   10     class Program
   11     {
   12         static void Main(string[] args)
   13         {
   14             var mc = new MyClass
   15             {
   16                 Id = 1,
   17                 Description = "One",
   18                 FirstList = new List<string> { "123", "456" },
   19                 SecondList = { "123", "456" }
   20             };
   21 
   22             // works
   23             var l1 = new List<string> { "123", "456" };
   24 
   25             // does not work
   26             List<string> l2 = { "123", "456" };
   27         }
   28     }
   29 

If you noticed when the list of part of class that uses a setter you can use a short cut and leave out the new List<string> portion.  There interesting part is that it doesn’t work by normal assignment as shown on line 26.

The other interesting part is that this works with dictionaries.

    1 
    2     class MyClass
    3     {
    4         public int Id { get; set; }
    5         public string Description { get; set; }
    6         public Dictionary<string, int> FirstDictionary { get; set; }
    7         public Dictionary<string, int> SecondDictionary { get; set; }
    8     }
    9 
   10     class Program
   11     {
   12         static void Main(string[] args)
   13         {
   14             var mc = new MyClass
   15             {
   16                 Id = 1,
   17                 Description = "One",
   18                 FirstDictionary = new Dictionary<string, int> { { "123", 4 }, { "456", 5 } },
   19                 SecondDictionary = { { "123", 4 }, { "456", 7 } }
   20             };
   21 
   22             // works
   23             var d1 = new Dictionary<string,int> { {"123",4}, {"456",7} };
   24 
   25             // does not work
   26             Dictionary<string,int> d2 = { {"123",4}, {"456",7} };
   27         }
   28     }
   29 

Personally I find this useful while writing unit tests.  Quite often I setup objects in a particular state, so any shortcut to make this faster and cleaner is better.

Tags:

Development

JavaScript Dictionary

by rsutton 19. October 2009 08:04

Here is a JavaScript dictionary I created.  It is nice because you can get all the keys, values, and even access keys by using notation like d.key1.

    1 Dictionary = function(test) {

    2     function isKey(k) {

    3         var ret = true;

    4         if(k == 'set' || k == 'get' || k == 'exists' || k == 'remove' || k == 'keys' || k == 'values' || k == 'kvps')

    5             ret = false;

    6         return ret;

    7     }

    8 

    9     var iStore = {};

   10 

   11     return {

   12         set: function(key,value) {

   13             if(isKey(key))

   14                 this[key] = function() { return value; }();

   15             else

   16                 iStore[key] = function() { return value; }();

   17         },

   18         get: function(key) {

   19             if(isKey(key))

   20                 return this[key];

   21             return iStore[key];   

   22         },

   23         exists: function(key) {

   24             if(isKey(key))

   25                 return this[key] ? true : false;

   26             return iStore[key] ? true : false;

   27         },

   28         remove: function(key) {

   29             if(isKey(key))

   30                 delete this[key];

   31             else

   32                 delete iStore[key];

   33         },

   34         keys: function() {

   35             var ret = [];

   36             for (var i in this) {

   37                 if(isKey(i))

   38                     ret[ret.length] = i;

   39             }

   40             for(var i in iStore)

   41                 ret[ret.length] = i;

   42             return ret;

   43         },

   44         values: function() {

   45             var ret = [];

   46             for (var i in this) {

   47                 if(isKey(i))

   48                     ret[ret.length] = this[i];

   49             }

   50             for (var i in iStore) {

   51                 ret[ret.length] = iStore[i];

   52             }

   53             return ret;

   54         },

   55         kvps: function() {

   56             var ret = {};

   57             for (var i in this) {

   58                 if(isKey(i))

   59                     ret[i] = this[i];

   60             }

   61             for(var i in iStore)

   62                 ret[i] = iStore[i];

   63             return ret;

   64         }

   65     };

   66 };

Tags:

Development

JavaScript Event Hub

by rsutton 7. October 2009 09:30

I’m am currently creating a UI to do a bulk add operation and one of the main features that this form must have is speed.  It must be quick to do maneuver around the form and get at various data.  So in order to do this I have decided to go the no postbacks, ajax and JavaScript route.  The problem is trying to deal with all the interactions and maintaining the state of the form, because I will be constantly entering data and making ajax request.  In my effort simplify things I created what I call an EventHub.  The idea is really simple.  Here is the code.

    1 

    2 cw.EventHub = function() {

    3     var messages = [];

    4 

    5     return {

    6         subscribe: function(message,key,subscriber) {

    7             // setup the message subscriber list of this is a new message

    8             if (!messages[message])

    9                 messages[message] = [];

   10 

   11             // add the subscriber

   12             messages[message][key] = subscriber;

   13         },

   14         unsubscribe: function(message,key) {

   15             delete messages[message][key];

   16         },

   17         dispatch: function(message,data) {

   18             for (var key in messages[message])

   19                 messages[message][key](data);

   20         }

   21     };

   22 } ();

You subscribe to events like this.

 

EventHub.subscribe("OrderDateUpdated", "myKey", function(data) { alert('Order ' + data.id + ' Updated'); });

 

I added the key during the subscribe so that you can unsubscribe messages if needed like this.

 

EventHub.unsubscribe("OrderDateUpdated", "myKey");

 

Then you send messages like this.

 

EventHub.send("OrderDateUpdated", { id: '1234' });

 

Like I said there is nothing too complex here and it has made my life a lot easier in dealing with UI interactions.  Adding additional behavior to a form is simply a function of subscribing to the published event and then doing some work.

Tags:

Development

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

Profile

Member of the Church of Jesus Christ of Latter-Day Saints, Developer and IT Professional