Wednesday, June 10, 2009

SharePoint: About Deleted Item

重要文件暫記:

c# 3.0 new features Example Demo


   1:  /////////////////////////////////////////////////////

   2:  //

   3:  // 總結所有的 New Features(language extensions)

   4:  //

   5:  /////////////////////////////////////////////////////

   6:   

   7:  using System;

   8:  using System.Collections.Generic;

   9:  using System.Linq;

  10:  using System.Text;

  11:  using System.Diagnostics;

  12:   

  13:  static class newFeaturesDemo

  14:  {

  15:      class ProcessData {

  16:          public Int32 Id { set; get; }

  17:          public Int64 Memory { set; get; }

  18:          public String Name { set; get; }

  19:      }

  20:   

  21:      public static void DisplayProcesses(Func<Process, Boolean> match) {

  22:          

  23:          // 1. implicitly typed local variables

  24:          var processes = new List<ProcessData>();

  25:   

  26:          foreach (var process in Process.GetProcesses()) {

  27:              if (match(process)) {

  28:                  

  29:                  // 2. object initialized

  30:                  processes.Add(new ProcessData

  31:                  {

  32:                      Id = process.Id,

  33:                      Name = process.ProcessName,

  34:                      Memory = process.WorkingSet64

  35:                  });

  36:              }

  37:          }

  38:   

  39:          // 3. extension methods

  40:          Console.WriteLine("Total memeory: {0} MB",

  41:                             processes.TotalMemory()/1024/1024);

  42:   

  43:          // 4. lambda expression

  44:          var top2Memory = processes

  45:                           .OrderByDescending(process => process.Memory)

  46:                           .Take(2)

  47:                           .Sum(process => process.Memory) / 1024 / 1024;

  48:   

  49:          Console.WriteLine(

  50:              "Memory consumed by the two most hungry processes: {0} MB",

  51:              top2Memory);

  52:   

  53:          // 5. anonymous type

  54:          var results = new

  55:          {

  56:              TotalMemory = processes.TotalMemory() / 1024 / 1024,

  57:              Top2Memory = top2Memory,

  58:              Processes = processes

  59:          };

  60:          

  61:          ObjectDumper.Write(results, 1);

  62:          ObjectDumper.Write(processes);

  63:   

  64:      }

  65:   

  66:      static Int64 TotalMemory(this IEnumerable<ProcessData> processes) {

  67:          Int64 result = 0;

  68:          foreach (var process in processes) {

  69:              result += process.Memory;

  70:          }

  71:   

  72:          return result;

  73:      }

  74:   

  75:    static void Main()

  76:    {

  77:      DisplayProcesses(

  78:        process => process.WorkingSet64 >= 20*1024*1024);

  79:    }     

  80:  }


Thursday, June 04, 2009

c# 3.0 new features (part II)

前一篇已經很長了,不想再拉長他的篇幅,所以分篇寫。而且這個趴吐也是相當模糊的不熟悉XD

New Feature #3: Lambda(λ) expressions
講這個之前呢,必須先知道有個delegates(委派)這東西。至於什麼是委派呢... 我也不是很清楚
= =,相當慚愧(orz) 所以才在上一篇貼了個文件XD。大概看得懂啦,不過不知道該怎麼寫。而且也還沒搞懂為什麼要這麼做(他存在的目的?)。

原本的delegates因為有了anonymous methods(匿名方法)而更簡潔。
現在要介紹的lambada expressions就是再進一步精簡他。

下面這個範例來自MSDN,裡面就包含了以具名、匿名,或這裡的重點,以lambda expression來 instantiate(實體化?) delegates:

   1:  // Declare delegate -- defines required signature:

   2:  delegate double MathAction(double num);

   3:   

   4:  class DelegateTest

   5:  {

   6:      // Regular method that matches signature:

   7:      static double Method4Delegates(double input)

   8:      {

   9:          return input * 2;

  10:      }

  11:   

  12:      static void Main()

  13:      {

  14:          // Instantiate delegate with named method(具名方法):

  15:          MathAction ma = Method4Delegates;

  16:   

  17:          // Invoke delegate ma:

  18:          double multByTwo = ma(4.5);

  19:          Console.WriteLine(multByTwo);

  20:   

  21:          // Instantiate delegate with anonymous method(匿名方法):

  22:          MathAction ma2 = delegate(double input)

  23:          {

  24:              return input * input;

  25:          };

  26:   

  27:          double square = ma2(5);

  28:          Console.WriteLine(square);

  29:   

  30:          // Instantiate delegate with lambda expression

  31:          MathAction ma3 = s => s * s * s;

  32:          double cube = ma3(4.375);

  33:   

  34:          Console.WriteLine(cube);

  35:      }

  36:  }

  37:   


New Feature #4: Extension methods
這個新功能也是個好東西,也一樣很難描述,所以一樣貼貼重點。

This new language feature makes it possible to treat existing types as if they were extended with additional methods.

In order to transform our method into an extension method, all we have to do is add the this keyword to the first parameter. The this keyword instructs the compiler to treat the method as an extension method.

小小看一下他的作用:

這裡有一小行code, 大概先把一堆processes做了些篩選,然後計算篩選出來的processes所佔用的memory, 最後做一個單位的換算。

BytesToMegaBytes(TotalMemory(FilterOutSomeProcesses(processes)));

嗯,對阿,然後呢? 然後如果把剛剛那些method變成extension method的話,就可以長成這樣:

processes
.FilterOutSomeProcesses()
.TotalMemory()
.BytesToMegaBytes();

上面那段code是為了方便閱讀才分成4行的,不是什麼特殊寫法XD。而且,看起來好像FilterOutSomeProcesses(), TotalMemory(), BytesToMegaBytes()就是processes的static method一樣,用個"."(dot)就可以叫用這些功能,他們其實是extension methods~ 酷吧~

但是!!! 以上面的code來說,如果processes其實也有一個instance method叫做TotalMemory呢? 在C#裡(VB.NET不太一樣), 如果extension method跟instance method都符合叫用條件的話,會優先呼叫該instance本身的method。

下面這個example就可以看出來:

   1:  using System;

   2:   

   3:  class Class1 {}

   4:  class Class2 {

   5:    public void Method1(string s) { Console.WriteLine("Class2.Method1");}

   6:  }

   7:  class Class3 {

   8:    public void Method1(object o) { Console.WriteLine("Class3.Method1");}

   9:  }

  10:  class Class4 {

  11:    public void Method1(int i) { Console.WriteLine("Class4.Method1");}

  12:  }

  13:  static class Extensions {

  14:    static public void Method1(this object o, int i)

  15:    {

  16:      Console.WriteLine("Extensions.Method1");

  17:    }

  18:   

  19:    static void Main()

  20:    {

  21:      new Class1().Method1(12);

  22:      new Class2().Method1(12);

  23:      new Class3().Method1(12);

  24:      new Class4().Method1(12);

  25:    }

  26:  }

  27:   

  28:  // Answer:

  29:  // Extensions.Method1

  30:  // Extensions.Method1

  31:  // Class3.Method1

  32:  // Class4.Method1


New Features #1, #2 請看c# 3.0 new features

Delegates

阿... 還不懂,收個文件。
Both delegates and interfaces enable a class designer to separate type declarations and implementation. A given interface can be inherited and implemented by any class or struct. A delegate can be created for a method on any class, as long as the method fits the method signature for the delegate.

A delegate can be instantiated by associating it either with a named or anonymous method.
Wednesday, June 03, 2009

c# 3.0 new features

前幾個禮拜一直想好好寫一下auto-implemented properties這個主題,拖了一陣子,忽然覺得他沒什麼= =,就不想寫了。發現了個更讓人感興趣的東西,所以就一起寫在這。

New Feature #1: auto-implemented properties
懶的描述,請看連結XD

New Feature #2: Object and collection initializers
這一塊是我感興趣的部分。直接看程式:

   1:  ProcessData data = new ProcessData();

   2:  data.Id = 123;

   3:  data.Name = "MyProcess";

   4:  data.Memory = 123456;

上面這段程式是很常見的片段: 宣告ProcessDate的object後,初始化一些值。
用C#3.0可以寫成這樣:

var data = new ProcessData{ Id=123, Name="MyProcess", Memory=123456};

嗯, 是認真的。

如果是有constructor的class:

   1:  var exception = new Exception("message");

   2:  exception.Source = "LINQ in Action";

   3:  throw exception;


可以變成這樣:

throw new Exception("message") { Source = "LINQ in Action" };

相當over!!! 以上介紹的是所謂的"Object Initializer", 接下來看看"collection initializer", 他其實並不陌生:

var digits = new List<int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

這看起來超眼熟的,他原本應該是這樣:

   1:  List<int> digits = new List<int>();

   2:  digits.Add(0);

   3:  digits.Add(1);

   4:  digits.Add(2);

   5:  //....

   6:  digits.Add(9);


再來一個稍微麻煩一點的:

   1:  ProcessData tmp;

   2:  var processes = new List<ProcessData>();

   3:  //先來第一組資料

   4:  tmp = new ProcessData();

   5:  tmp.Id = 123;

   6:  tmp.Name = "devenv";

   7:  processes.Add(tmp);

   8:  //再來第二組資料

   9:  tmp = new ProcessData();

  10:  tmp.Id = 456;

  11:  tmp.Name = "firefox";

  12:  processes.Add(tmp);


把ojbect initializer跟collection initializer一起用:

   1:  var processes = new List<ProcessData> {

   2:    new ProcessData {Id=123, Name="devenv"},

   3:    new ProcessData {Id=456, Name="firefox"}

   4:  }


相當簡潔明瞭。以上,要這樣做,必須有一個前提XD(這麼晚才講)

This new syntax allows us to initialize different types of collections, provided they implement System.Collections.IEnumerable and provide suitable Add methods.

(take a break~) 好像還有,讀完後補。

PowerBuilder problems

今天整個早上都在跟pb奮戰。遇到幾個詭異的問題=   =

1. function忽然壞掉
自己寫的某個function不知道發生了什麼事情,忽然就罷工了。就是程式跑到那,就直接跳過 他。就像有一支funcion會回傳boolean,然後放在if區段裡,程式跑到那,根本不會進去那支 function就跳到else去了... 相當傻眼。搞了半天,可以嘗試做一下Regenerate,搞定。

2. 資料庫欄位長度問題
這個狀況有點複雜,就是兩間公司有自己的資料庫,可是跑的是同樣的系統,同樣的程式。但是資料庫的結構又有那麼點不同,這次遇到的是欄位長度不同。所以某個data window的該欄位在P公司(欄位長度較短)跑起來是正常的,可是在O公司資料卻被截斷了。明明就連到O公司的資料庫了,程式還是顯示P公司資料庫的長度。原本以為長度被寫死了,後來知道原來可以在那個data window按右鍵,選"Edit Source"。可以發現原來column的型別與長度被定義在這裡面,就從這兒給他改掉,顯示上是沒問題,不過目前還不確定如果要操作這筆資料會不會有問題。

這樣,搞一個早上,還有其他零零碎碎的。

Tags