onsdag 2 december 2009

Anti-Consumerism

Having read the chapter "consuming pleasures" in Bill Brysons book "Notes from a big country" I really started to feel bad about the way most people in rich countries (including myself to some extent) engage in meaningless consumption. I googled for anti-consumerism and found this page. Unable to submit my comments, I wrote this blog entry.

I believe that the whole environmental debate is way too much focused on CO2 emissions, it seems like people believe that if we simply could stop producing greenhouse gases, everything would be fine!? Of course, this is not true, we' would still be "refining" our natural resources into hazardous products that cant be recycled back to nature. Think about how many different kinds of materials an iPod consists of, metals, minerals, plastics. How are you suppose to recycle a product like that back to nature? You cant! The natural resources spent producing that product is gone forever! To quote Annnie Leonard (from the excellent movie www.storyofstuff.com): It's a linear system from production to disposal run on a planet with finite resources. Running a system like that is like spending the money from your savings account without getting any or only a small income. Slowly, you're going bankrupt. Think about what bankrupt really means in this sense, no more eco-services, no more clean air or water.

Some people seem to think we can live "without the environment". "I live in the city where there are no trees, nature is a luxury we'll have to do without in the future". Of course, the whole society and our survival is dependent on the survival of nature whether we fail to realize it or not.

Here are some tips on how to consume less and be happier:

  1. Consume experiences rather than products, it's better for the environment and you won't get the hassle of owning another product that needs to be worried about/taken care of.
  2. When feeling like buying a product, get into the habit of waiting a couple of days before actually buying it. In the meantime, ask yourself: "What REAL (if any) value will this product bring to my life?" Most of the times, you will find that you don't really need it at all.
  3. A friend of mine told me that when he felt the urge to buy something, simply going on the internets reading forums, looking at pictures and accessories would alleviate the need to actually buy the product.
  4. If you've decided that you do indeed need something, go for quality (this is surprisingly a lot harder than it sounds, simply buying expensive stuff is unfortunately no guarantee for a long lasting, good quality product). Go on the internet, read reviews about the particular product, pay special attention to the negative comments. Don't become a "fanboy" simply looking for positive comments that will simply reinforce what you already think you know or love about the product.
  5. Read a book on something with eastern philosophy, eg. "the power of now" by Eckhart Tolle (does it qualify?). It might help you realize a few things about yourself, what makes you happy etc.
  6. Spread the message!

onsdag 18 februari 2009

Generic FindControl extension method

The other day I needed a way to find controls of a certain type in the control hierarchy, preferably in a recursive fashion. The regular Control.FindControl(string controlID) didn't quite cut it. I created two simple extension methods using Jeff Atwood's recursive version as a starting point. Here's an example of why you might need it and how you can use it.

Lets say eg. that you are dynamically adding custom user controls in a repeater and that you want to retrieve all of them without knowing their exact position in the control tree.

   1:  <asp:Repeater runat="server" DataSource="<%# MyDataSource %>" OnLoad="DataBind">
   2:     <ItemTemplate>
   3:        <My:Customer runat="server" Customer="<%# Container.DataItem as My.Customer %>" />
   4:     </ItemTemplate>
   5:  </asp:Repeater>

I came up with a simple extension method that recursively finds controls of a certain type:

   1:  List<CustomerControl> list = myControl.FindControls<CustomerControl>();

By adding an argument (in the form of a predicate) to the extension method you could easily find controls that has a certain feature enabled, eg. where a checkbox is checked.

This could be useful if you are making some kind of web based adminstration tool that dynamically generates edit controls. When the user hits the save button you could easily find all controls that has a certain feature

   1:  List<CustomerControl> list = myControl.FindControl<CustomerControl>(c =>
   2:     c.CustomerIsEnabled);

Finally, here are the extension methods:

   1:          /// <summary>
   2:          /// Recursively finds controls of a certain type anywhere in the 
   3:          /// controls "sub tree".
   4:          /// </summary>
   5:          public static List<T> FindControls<T>(this Control control) where T : class
   6:          {
   7:              return FindControls<T>(control, null);
   8:          }
   9:          /// <summary>
  10:          /// Recursively finds controls of a certain type that matches the specified predicate
  11:          /// anywhere in the controls "sub tree".
  12:          /// </summary>
  13:          public static List<T> FindControls<T>(this Control control, Predicate<T> predicate) where T : class
  14:          {
  15:              List<T> result = new List<T>();
  16:              foreach (Control childControl in control.Controls)
  17:              {
  18:                  if (childControl is T)
  19:                  {
  20:                      T tmp = childControl as T;
  21:                      if(predicate==null || predicate.Invoke(tmp))
  22:                          result.Add(tmp);
  23:                  }
  24:                  result.AddRange(FindControls<T>(childControl, predicate));
  25:              }
  26:              return result;
  27:          }

Hope you find this useful.

Bloggintresserade