兩個月之前,新換一個工作后,由於新公司使用VB,而且做web開發,於是不得不暫時丟下用了許久的C#和Winform的開發了。其實本身語言沒有多大的限制的,主要的是做Web,需要改變很多,然又畢竟大部份的都是做Web,所以就學吧,誰叫咱是做這一行的。只有不停的學..........
也是因為這些相關的種種原因,在博客園中發現了webabcd的SmartGridView,在征得原作者的同意之后,也為了配合我那有VB癖好的經理,便將其轉換成了VB的源碼。轉換過程當中,目前只是簡單的語法轉變,並沒有改變原作者的思路。稍后等有時間了再將VB的一些新特性加入進去吧。暫時先用上再說。呵呵....
轉換過程當中,讓我記憶很深的是轉換自定義事件時的煩惱,舉一兩例,如下的C#代碼
C#源碼
private static readonly object rowDataBoundDataRowEventKey = new object();
/**//// <summary>
/// RowDataBound事件中的DataControlRowType.DataRow部分
/// </summary>
[Category("擴展")]
public event RowDataBoundDataRowHandler RowDataBoundDataRow
{
add { Events.AddHandler(rowDataBoundDataRowEventKey, value); }
remove { Events.RemoveHandler(rowDataBoundDataRowEventKey, value); }
}
/**//// <summary>
/// 觸發RowDataBoundDataRow事件
/// </summary>
/// <param name="e"></param>
protected virtual void OnRowDataBoundDataRow(GridViewRowEventArgs e)
{
RowDataBoundDataRowHandler handler = Events[rowDataBoundDataRowEventKey] as RowDataBoundDataRowHandler;
if (handler != null)
{
handler(this, e);
}
}
如果要轉成VB的話,必須如下轉換:
VB源碼
Private Shared ReadOnly rowDataBoundDataRowEventKey As New Object()
''' <summary>
''' RowDataBound事件中的DataControlRowType.DataRow部分
''' </summary>
<Category("擴展")> _
Public Custom Event RowDataBoundDataRow As RowDataBoundDataRowHandler
AddHandler(ByVal value As RowDataBoundDataRowHandler)
Events.AddHandler(rowDataBoundDataRowEventKey, value)
End AddHandler
RemoveHandler(ByVal value As RowDataBoundDataRowHandler)
Events.RemoveHandler(rowDataBoundDataRowEventKey, value)
End RemoveHandler
RaiseEvent(ByVal obj As Object, ByVal e As GridViewRowEventArgs)
Dim handler As RowDataBoundDataRowHandler = DirectCast(Events(rowDataBoundDataRowEventKey), RowDataBoundDataRowHandler)
If (handler IsNot Nothing) Then
handler.Invoke(Me, e)
End If
End RaiseEvent
End Event
''' <summary>
''' 觸發RowDataBoundDataRow事件
''' </summary>
''' <param name="e"></param>
Protected Overridable Sub OnRowDataBoundDataRow()Sub OnRowDataBoundDataRow(ByVal e As GridViewRowEventArgs)
Dim handler As RowDataBoundDataRowHandler = DirectCast(Events(rowDataBoundDataRowEventKey), RowDataBoundDataRowHandler)
If (handler IsNot Nothing) Then
handler.Invoke(Me, e)
End If
End Sub
一般我們轉換的時候並不會加入
RaiseEvent
RaiseEvent(ByVal obj As Object, ByVal e As GridViewRowEventArgs)
Dim handler As RowDataBoundDataRowHandler = DirectCast(Events(rowDataBoundDataRowEventKey), RowDataBoundDataRowHandler)
If (handler IsNot Nothing) Then
handler.Invoke(Me, e)
End If
End RaiseEvent
但VB如果不加,是會編譯出錯的。2.0是這樣子,不知其它版本如何。當然如果你后面寫了觸發的事件,這個RaiseEvent中的代碼是可以刪掉不好,也不會出錯的。只是形式必須有而憶。
其次就是轉下面這一句的時候
int sortOrderIndex = Array.FindIndex(sortColumns, delegate(string s) { return s.IndexOf(columnName) != -1; });
VB必須要分開寫delegate的實現
VB
Dim sortOrderIndex As Integer = Array.FindIndex(sortColumns, AddressOf FindDelegate)
Private Function FindDelegate()Function FindDelegate(ByVal s As String) As Boolean
If (s.IndexOf(columnName) = 0) Then
Return True
Else
Return False
End If
End Function
看來.C#,VB要互通有無還有得東西要學......
先記下這兩點印象深刻點的貼上來。說不定哪天讓你也能用上,那此篇也就達到了我今天的目的了。.....
另外有點弱弱的問題是當我在翻譯導出的這段代碼時,在實際運行階段總是出現不可認識的<eof>標簽,忒郁悶半天之后,將For i As Integer = 0 To headers.Length - 1設為For i As Integer = 0 To headers.Length - 2,搞不明白為什么,我是因為將兩個xsl保存下來之后,才發現VB產生的多了一行,所以就少循環一次就可以了。