昭峰's profile怪頭的私人頻道PhotosBlogLists Tools Help

怪頭的私人頻道

有事別來、沒事快滾

昭峰 陳

Occupation
Interests
不算聰明、也不算笨、更不算傻...
不算英俊、也不算醜、更不算呆... 寫程式就像算數學一樣簡單~~~
不要想的太複雜~~~
No list items have been added yet.
Photo 1 of 21

今日星情與天氣

Loading...
1/1/2006

06年台北101跨年煙火秀

今年台北101跨年煙火秀真是炫呀!!
有興趣的朋友請到我的Xuite Blog 跟相本看...
在我的網站連結區裡有我的Xuite Blog的連結...
就是"怪頭的技術不落國"
12/28/2005

My Xuite Blog

最近申請了在HiNet的Xuite[sweet]免費整合性網路服務...
 
這個服務包含了「網路硬碟」、「電子郵件」、「網路相簿」還有「網路日誌」...
全部空間共有500MB,而每個服務所使用的空間可以一自己的需求作調配...
 
雖然看過這類型的服務不多,但我覺得很好用,就光以相簿跟日誌來說,就比MSN Space好用,所以我以後的資料更新會著重在Xuite上,各位假如有興趣,歡迎瀏覽
http://blog.xuite.net/jaofeng.chen/
12/19/2005

[.NET 2.0]System.Collections.Generic.List<T>

在.NET Framework 2.0中的System.Collections namespace多了一個1.1所沒有的成員 - System.Collections.Generic
這個namespace最常用的應該是System.Collections.Generic.List<T>這個class(因為我常用:P)
而這個class能做什麼呢?! 其實你把他想成是個物件(或類別)的集合,可能就會比較容易理解
以前在VS6時代,要做物件集合的類別時,常常會使用以下方式來做(以vb6為例)

==== [VB6 Code Start] ====
' Filename: myClass.cls
Private String m_myValue = ""
Public Property Let myValue(ByVal vData As String)
    m_myValue = vData
End Property
Public Property Get myValue() As String
    myValue= m_myValue
End Property

' Filename: myClasses
Private mCol As New Collection
Public Sub Add(cls As myClass)
    mCol.Add cls
End Sub
Public Function Insert(ByVal AfterIndexPos As Long, cls As myClass) As Long
    If AfterIndexPos > mCol.Count Then Exit Function
    If AfterIndexPos = 0 Then
        mCol.Add cls, , 1
        Insert = 1
    Else
        mCol.Add cls, , , AfterIndexPos
        Insert = AfterIndexPos
    End If
End Function
Public Property Get Item(ByVal Index As Long) As myClass
  Set Item = mCol(Index)
End Property
Public Property Get Count() As Long
    Count = mCol.Count
End Property
Public Sub Remove(ByVal Index As Long)
    mCol.Remove Index
End Sub
Public Sub RemoveAll()
    Dim i As Long
    For i = 1 To mCol.Count
        mCol.Remove 1
    Next i
End Sub
Private Sub Class_Terminate()
    Set mCol = Nothing
End Sub
==== [VB6 Code End] ====

看吧!光一個集合, 就必須寫這麼一大串, 而假如程式內有一堆物件(或類別)集合時, 類似myClasses的程式就要寫好幾個
在.NET Framework 2.0中就不用這麼累了, 只要使用System.Collections.Generic.List<T>這個class, 只需一行程式, 因為諸如Add, Remove, Count這些Properties & methods, .Net Framework 2.0都已經幫我們做好了
以下就開始來說明System.Collections.Generic.List<T>的用法(以下將用C#說明)
==== [C# Code Start] ====
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            class2 myText = new class2();
            // 使用List<t>.Add()方法來新增集合內容
            myText.Values.Add(new class1("123"));
            myText.Values.Add(new class1("456"));
            myText.Values.Add(new class1("ABC"));
            myText.Values.Add(new class1("DEF"));
            myText.Values.Add(new class1("Jaofeng"));
            myText.Values.Add(new class1("James"));
            Predicate<class1> FindJaofeng = delegate(class1 obj) {
                return obj.Value == "Jaofeng";
            };
            Console.WriteLine(string.Format("Jaofeng位於第 {0} 個", myText.Values.FindIndex(FindJaofeng)));
            Console.WriteLine("=======================");
            Console.WriteLine("將所有資料列出");
            int idx = 0;
            Action<class1> ListAll = delegate(class1 obj) {
                Console.WriteLine(string.Format("第 {0} 個的Value值為 {1}", idx, obj.Value));
                idx++;
            };
            myText.Values.ForEach(ListAll);
        }
    }
    class class1 {
        private string _value = string.Empty;
        public string Value { get { return _value; } }
        public class1(string value) {
            _value = value;
        }
    }
    class class2 {
        private List<class1> _values = new List<class1>();
        public List<class1> Values { get { return _values; } }
        public class2() { }
    }
}
==== [C# Code End] ====

執行的結果如下:
Jaofeng位於第 4 個
=======================
將所有資料列出
第 0 個的Value值為 123
第 1 個的Value值為 456
第 2 個的Value值為 ABC
第 3 個的Value值為 DEF
第 4 個的Value值為 Jaofeng
第 5 個的Value值為 James
當需要依條件來尋找集合內的某個類別時, 可用List<T>Find(), List<T>FindLast()來搜尋, 回傳搜尋到的類別
當需要依條件來尋找集合內的某些類別時, 可用List<T>FindAll()來搜尋, 將回傳一個新的List<T>物件集合
當需要依條件來尋找集合內的某個類別的索引值時, 可用List<T>FindIndex(), List<T>FindLastIndex()
List<T>Find(), List<T>FindLast()的不同是, List<T>Find()由Index=0開始尋找, 而List<T>FindLast()由Index = List<T>.Count - 1開始尋找
同理, List<T>FindIndex(), List<T>FindLastIndex()也是一樣, 不同的是, 這兩個回傳的是索引值
當使用List<T>Find()相關函示時, 必須delegate.這個Predicate<T>
其內容就是搜尋的判斷式, 如:
==== [C# Code Start] ====
Predicate<class1> FindJaofeng = delegate(class1 obj) {
    return obj.Value == "Jaofeng";
};
==== [C# Code End] ====

return type為boolean值
而上面也有介紹一個List<T>.ForEach(), 這個Method只是將原本我們用foreach()的方式, 簡化而已
譬如原本的習慣寫法:
==== [C# Code Start] ====
foreach (class1 cls in myText.Values) {
    // ... Do something
}
// 現在變成
Action<class1> ActionName = delegate(class1 obj) {
    // ... Do something
};
myText.Values.ForEach(ActionName);
==== [C# Code End] ====

程式碼雖然看起來好像沒比較簡化, 但是執行效率卻是比習慣寫法的效率高喔...

.Net Framework 2.0 Beta 2 upgrade to 2.0

相信有很多朋友從VS2005 Beta1就開始玩了...
現在VS2005正式版已經在MSDN Subscriptions可以下載了, 不過是英文版的...
至於繁體中文版的, 據說是在明天三月就會上市...
堅持要用中文版的朋友們就再等一下吧...

這次要跟各位報告的是...
當你從.NET Framework 2.0 Beta 2更新版本至 2.0正式版時, 需要注意的地方...

  1. 所有 DataSource WebControl 的 OldValuesParameterFormatString 屬性
    在 Beta 2 時, 這個屬性預設值是 original_{0}, 但在正式版時, 這個屬性預設值卻變成 {0}, 少了"original_"
  2. aspnet_regsql.exe在資料庫中所設定的 Database "aspnetDb", 也有些許的變更
    有幾個 Stored Procedure 的輸入欄位有變動, 譬如 aspnet_UsersInRoles_AddUsersToRoles 的輸入欄位中有個@TimeZoneAdjustment 定義為 INT 的欄位;在正式版中已改為 @CurrentTimeUtc 定義為 datetime 的欄位
    變更的有:
  • aspnet_UsersInRoles_AddUsersToRoles
  • aspnet_Membership_GetPasswordWithFormat
  • aspnet_Membership_CreateUser
  • aspnet_Membership_GetNumberOfUsersOnline
  • aspnet_Membership_GetPassword
  • ........ 等等
    其他的請各位查一下, 只要是 @TimeZoneAdjustment 定義為 INT 的欄位, 全部都改為@CurrentTimeUtc 定義為 datetime 的欄位了

除了以上兩個常碰到的問題之外, 相信各位先前使用Beta 2所開發的原始碼, "應該"都可已完全更新到.NET Framework 2.0正式版上使用

[.Net 1.1 & 2.0] IsInputKey & IsInputChar

不知各位有沒有自己實做過類似TextBox或是需要完全接收鍵盤按建的自訂元件...
假如有, 應該都會碰到一個問題... 就是接收不到方向鍵... 這不知道是微軟的好意還是故意的(我想應該是好意的 ^^)...
當你將你寫好的UserControl放到WinForm後執行他...
你會發覺就算你去override OnKeyDown也接收不到那四個該死的方向鍵...
這是因為你沒有另外去override另一個Method... 那就是IsInputKey...
只要把回傳值改為true... 這樣你就可以攔截到鍵盤上所有的按鍵值了...
而IsInputChar與IsInputKey的意思是一樣的...
差別是在一個攔截按鍵(IsInputKey), 而另一個是攔截字元(IsInputChar)...
各位有空的話去查一下這兩個method...
大致上的用意是... 要不要讓接收到的按鍵(或字元)傳回容器去... 另一個解釋就是要不要攔截這個按鍵(或字元)
true: 攔截; false: 不攔截, 傳到容器去

==== [C# Code Start] ====
        protected override bool IsInputKey(Keys keyData) {
            /* 這裡需回傳true, 否則無法接收到方向鍵
             * MSDN:
             * Call the IsInputKey method to determine whether the key specified by the keyData
             * parameter is an input key that the control wants.
             * This method is called during window message preprocessing to determine whether
             * the specified input key should be preprocessed or sent directly to the control.
             * If IsInputKey returns true, the specified key is sent directly to the control.
             * If IsInputKey returns false, the specified key is preprocessed and only sent to
             * the control if it is not consumed by the preprocessing phase.
             * Keys that are preprocessed include the TAB, RETURN, ESCAPE, and the UP ARROW,
             * DOWN ARROW, LEFT ARROW, and RIGHT ARROW keys. */
            return true;
        }
==== [C# Code End] ====