2012-03-23 151 views
1

在查看Fuzzy Date Time Picker Control in C# .NET? Piotr Czaapla對該問題的回答正是我所需要的。將C#lambda表達式轉換爲VB

不幸的是,我是一個VB.NET傢伙,我不熟悉lambda表達式,所以我嘗試轉換代碼導致了幾個小時的錯誤的括號,並用磚塊敲我的頭。

任何機會一些雙語嚮導可以將C#代碼轉換爲VB.net給我嗎?

下面的代碼有問題:

class FuzzyDateTime 
{ 

static List<string> dayList = new List<string>() { "sun", "mon", "tue", "wed", "thu", "fri", "sat" }; 
static List<IDateTimePattern> parsers = new List<IDateTimePattern>() 
{ 
    new RegexDateTimePattern (
     @"next +([2-9]\d*) +months", 
     delegate (Match m) { 
      var val = int.Parse(m.Groups[1].Value); 
      return DateTime.Now.AddMonths(val); 
     } 
    ), 
    new RegexDateTimePattern (
     @"next +month", 
     delegate (Match m) { 
      return DateTime.Now.AddMonths(1); 
     } 
    ),   
    new RegexDateTimePattern (
     @"next +([2-9]\d*) +days", 
     delegate (Match m) { 
      var val = int.Parse(m.Groups[1].Value); 
      return DateTime.Now.AddDays(val); 
     } 
    ), 

    new RegexDateTimePattern (
     @"([2-9]\d*) +months +ago", 
     delegate (Match m) { 
      var val = int.Parse(m.Groups[1].Value); 
      return DateTime.Now.AddMonths(-val); 
     } 
    ), 
    new RegexDateTimePattern (
     @"([2-9]\d*) days +ago", 
     delegate (Match m) { 
      var val = int.Parse(m.Groups[1].Value); 
      return DateTime.Now.AddDays(-val); 
     } 
    ), 
    new RegexDateTimePattern (
     @"([2-9]\d*) *h(ours)? +ago", 
     delegate (Match m) { 
      var val = int.Parse(m.Groups[1].Value); 
      return DateTime.Now.AddMonths(-val); 
     } 
    ), 
    new RegexDateTimePattern (
     @"tomorrow", 
     delegate (Match m) { 
      return DateTime.Now.AddDays(1); 
     } 
    ), 
    new RegexDateTimePattern (
     @"today", 
     delegate (Match m) { 
      return DateTime.Now; 
     } 
    ), 
    new RegexDateTimePattern (
     @"yesterday", 
     delegate (Match m) { 
      return DateTime.Now.AddDays(-1); 
     } 
    ), 
    new RegexDateTimePattern (
     @"(last|next) *(year|month)", 
     delegate (Match m) { 
      int direction = (m.Groups[1].Value == "last")? -1 :1; 
      switch(m.Groups[2].Value) 
      { 
       case "year": 
        return new DateTime(DateTime.Now.Year+direction, 1,1); 
       case "month": 
        return new DateTime(DateTime.Now.Year, DateTime.Now.Month+direction, 1); 
      } 
      return DateTime.MinValue; 
     } 
    ), 
    new RegexDateTimePattern (
     String.Format(@"(last|next) *({0}).*", String.Join("|", dayList.ToArray())), //handle weekdays 
     delegate (Match m) { 
      var val = m.Groups[2].Value; 
      var direction = (m.Groups[1].Value == "last")? -1 :1; 
      var dayOfWeek = dayList.IndexOf(val.Substring(0,3)); 
      if (dayOfWeek >= 0) { 
       var diff = direction*(dayOfWeek - (int)DateTime.Today.DayOfWeek); 
       if (diff <= 0) { 
        diff = 7 + diff; 
       } 
       return DateTime.Today.AddDays(direction * diff); 
      } 
      return DateTime.MinValue; 
     } 
    ), 

    new RegexDateTimePattern (
     @"(last|next) *(.+)", // to parse months using DateTime.TryParse 
     delegate (Match m) { 
      DateTime dt; 
      int direction = (m.Groups[1].Value == "last")? -1 :1; 
      var s = String.Format("{0} {1}",m.Groups[2].Value, DateTime.Now.Year + direction); 
      if (DateTime.TryParse(s, out dt)) { 
       return dt; 
      } else { 
       return DateTime.MinValue; 
      } 
     } 
    ), 
    new RegexDateTimePattern (
     @".*", //as final resort parse using DateTime.TryParse 
     delegate (Match m) { 
      DateTime dt; 
      var s = m.Groups[0].Value; 
      if (DateTime.TryParse(s, out dt)) { 
       return dt; 
      } else { 
       return DateTime.MinValue; 
      } 
     } 
    ), 
}; 

public static DateTime Parse(string text) 
{ 
    text = text.Trim().ToLower(); 
    var dt = DateTime.Now; 
    foreach (var parser in parsers) 
    { 
     dt = parser.Parse(text); 
     if (dt != DateTime.MinValue) 
      break; 
    } 
    return dt; 
} 
} 

interface IDateTimePattern 
{ 
    DateTime Parse(string text); 
} 

class RegexDateTimePattern : IDateTimePattern 
{ 
    public delegate DateTime Interpreter(Match m); 
    protected Regex regEx; 
    protected Interpreter inter; 
    public RegexDateTimePattern(string re, Interpreter inter) 
    { 
     this.regEx = new Regex(re); 
     this.inter = inter; 
    } 
    public DateTime Parse(string text) 
    { 
     var m = regEx.Match(text); 

     if (m.Success) 
     { 
      return inter(m); 
     } 
     return DateTime.MinValue; 
    } 
} 
+0

你有沒有嘗試過在線c#到vb.net轉換工具,如http://converter.telerik.com/? – 2012-03-23 13:18:50

+2

你爲什麼試圖將它轉換爲vb?編譯它在C#中,並引用程序集在您的VB項目。 – 2012-03-23 13:18:57

+0

@Bala - 我曾嘗試了幾個C#轉換爲vb.net,但lambda表達式被弄壞了。 – 2012-03-23 13:21:11

回答

3

OK,有一串代碼在那裏,我就不翻譯了這一切,但例如(假設VS2010或更高版本)。 。 。

new RegexDateTimePattern (
    @"next +([2-9]\d*) +months", 
    delegate (Match m) { 
     var val = int.Parse(m.Groups[1].Value); 
     return DateTime.Now.AddMonths(val); 
    } 
) 

Dim p2 = New RegexDateTimePattern(
    "next +([2-9]\d*) +months", 
    Function(m) 
     Dim val = Int.Parse(m.Groups(1).Value) 
     Return DateTime.Now.AddMonths(val) 
    End Function 
    ) 

new RegexDateTimePattern (
    String.Format(@"(last|next) *({0}).*", String.Join("|", dayList.ToArray())), 
    delegate (Match m) { 
     var val = m.Groups[2].Value; 
     var direction = (m.Groups[1].Value == "last")? -1 :1; 
     var dayOfWeek = dayList.IndexOf(val.Substring(0,3)); 
     if (dayOfWeek >= 0) { 
      var diff = direction*(dayOfWeek - (int)DateTime.Today.DayOfWeek); 
      if (diff <= 0) { 
       diff = 7 + diff; 
      } 
      return DateTime.Today.AddDays(direction * diff); 
     } 
     return DateTime.MinValue; 
    } 
), 

變得

Dim p = New RegexDateTimePattern(
     String.Format("(last|next) *({0}).*", String.Join("|", dayList.ToArray())), 
     Function(m) 
      Dim val = m.Groups(2).Value 
      Dim direction = If(m.Groups(1).Value = "last", -1, 1) 
      Dim dayOfWeek = dayList.IndexOf(val.Substring(0, 3)) 
      If (dayOfWeek >= 0) Then 
       Dim diff = direction * (dayOfWeek - CType(DateTime.Today.DayOfWeek, Integer)) 
       If (diff <= 0) Then 
        diff = 7 + diff 
       End If 
       Return DateTime.Today.AddDays(direction * diff) 
      End If 
      Return DateTime.MinValue 
     End Function 
     ) 

還有什麼在那裏蜿蜒的你梅隆?

如果不是VS2010(或者即使它不是你喜歡的lambda表達式),那麼你所需要做的就是獲取每個lambda表達式(VB代碼中的「in line」函數) ,爲每一個(例如RegexDateTimePattern_Helper2爲最後一個)創建明確命名的功能,並通過AddressOf RegexDateTimePattern_Helper2代替lambda表達式

Function RegexDateTimePattern_Helper2(Match m) as DateTime ''# Dreadful name 
     dim val = m.Groups[2].Value 
     dim direction = if(m.Groups(1).Value = "last", -1 ,1) 
     Dim dayOfWeek = dayList.IndexOf(Val.Substring(0, 3)) 
     If (dayOfWeek >= 0) Then 
      Dim diff = direction * (dayOfWeek - CType(DateTime.Today.DayOfWeek, Integer)) 
      If (diff <= 0) Then 
       diff = 7 + diff 
      End If 
      Return DateTime.Today.AddDays(direction * diff) 
     End If 
     Return DateTime.MinValue 
    End Function 

。 。 。

New RegexDateTimePattern (
    String.Format("(last|next) *({0}).*", String.Join("|", dayList.ToArray())), 
    AddressOf RegexDateTimePattern_Helper2) 
+0

FWIW我在你的文章中修復了vb.net的評論。現在它正確地結束了該行的註釋,而不是繼續幾行。 '''#'修復它。 – mydogisbox 2012-03-23 14:05:19

+0

@mydogisbox:塔朋友:) – 2012-03-23 14:24:08

+0

這看起來像一個偉大的開始 - 讓我摔跤幾個小時,看看我是否有其他問題。再次感謝! – 2012-03-23 15:34:27

0

轉換器here吐出這個代碼編譯塊了(除去少數「做」 S和一對夫婦的「變種」 S後):

Imports System.Text.RegularExpressions 

Class FuzzyDateTime 

Shared dayList As New List(Of String)() From { _ 
"sun", _ 
"mon", _ 
"tue", _ 
"wed", _ 
"thu", _ 
"fri", _ 
"sat" _ 
} 
Shared parsers As New List(Of IDateTimePattern)() From { _ 
New RegexDateTimePattern("next +([2-9]\d*) +months", Function(m As Match) 
                  Dim val = Integer.Parse(m.Groups(1).Value) 
                  Return DateTime.Now.AddMonths(val) 
                 End Function), _ 
New RegexDateTimePattern("next +month", Function(m As Match) DateTime.Now.AddMonths(1)), _ 
New RegexDateTimePattern("next +([2-9]\d*) +days", Function(m As Match) 
                 Dim val = Integer.Parse(m.Groups(1).Value) 
                 Return DateTime.Now.AddDays(val) 

                End Function), _ 
New RegexDateTimePattern("([2-9]\d*) +months +ago", Function(m As Match) 
                 Dim val = Integer.Parse(m.Groups(1).Value) 
                 Return DateTime.Now.AddMonths(-val) 
                End Function), _ 
New RegexDateTimePattern("([2-9]\d*) days +ago", Function(m As Match) 
                 Dim val = Integer.Parse(m.Groups(1).Value) 
                 Return DateTime.Now.AddDays(-val) 
                End Function), _ 
New RegexDateTimePattern("([2-9]\d*) *h(ours)? +ago", Function(m As Match) 
                  Dim val = Integer.Parse(m.Groups(1).Value) 
                  Return DateTime.Now.AddMonths(-val) 
                 End Function), _ 
New RegexDateTimePattern("tomorrow", Function(m As Match) DateTime.Now.AddDays(1)), _ 
New RegexDateTimePattern("today", Function(m As Match) DateTime.Now), _ 
New RegexDateTimePattern("yesterday", Function(m As Match) DateTime.Now.AddDays(-1)), _ 
New RegexDateTimePattern("(last|next) *(year|month)", Function(m As Match) 
                  Dim direction As Integer = If((m.Groups(1).Value = "last"), -1, 1) 
                  Select Case m.Groups(2).Value 
                   Case "year" 
                    Return New DateTime(DateTime.Now.Year + direction, 1, 1) 
                   Case "month" 
                    Return New DateTime(DateTime.Now.Year, DateTime.Now.Month + direction, 1) 
                  End Select 
                  Return DateTime.MinValue 
                  ''#handle weekdays 
                 End Function), _ 
New RegexDateTimePattern([String].Format("(last|next) *({0}).*", [String].Join("|", dayList.ToArray())), Function(m As Match) 
                               Dim val = m.Groups(2).Value 
                               Dim direction = If((m.Groups(1).Value = "last"), -1, 1) 
                               Dim dayOfWeek = dayList.IndexOf(val.Substring(0, 3)) 
                               If dayOfWeek >= 0 Then 
                                Dim diff = direction * (dayOfWeek - CInt(DateTime.Today.DayOfWeek)) 
                                If diff <= 0 Then 
                                 diff = 7 + diff 
                                End If 
                                Return DateTime.Today.AddDays(direction * diff) 
                               End If 
                               Return DateTime.MinValue 

                               ''# to parse months using DateTime.TryParse 
                              End Function), _ 
New RegexDateTimePattern("(last|next) *(.+)", Function(m As Match) 
                Dim dt As DateTime 
                Dim direction As Integer = If((m.Groups(1).Value = "last"), -1, 1) 
                Dim s = [String].Format("{0} {1}", m.Groups(2).Value, DateTime.Now.Year + direction) 
                If DateTime.TryParse(s, dt) Then 
                 Return dt 
                Else 
                 Return DateTime.MinValue 
                End If 
                ''#as final resort parse using DateTime.TryParse 

               End Function), _ 
New RegexDateTimePattern(".*", Function(m As Match) 
            Dim dt As DateTime 
            Dim s = m.Groups(0).Value 
            If DateTime.TryParse(s, dt) Then 
             Return dt 
            Else 
             Return DateTime.MinValue 
            End If 
           End Function) _ 
} 

    Public Shared Function Parse(text As String) As DateTime 
     text = text.Trim().ToLower() 
     Dim dt = DateTime.Now 
     For Each parser In parsers 
      dt = parser.Parse(text) 
      If dt <> DateTime.MinValue Then 
       Exit For 
      End If 
     Next 
     Return dt 
    End Function 
End Class 

Interface IDateTimePattern 
    Function Parse(text As String) As DateTime 
End Interface 

Class RegexDateTimePattern 
    Implements IDateTimePattern 
    Public Delegate Function Interpreter(m As Match) As DateTime 
    Protected regEx As Regex 
    Protected inter As Interpreter 
    Public Sub New(re As String, inter As Interpreter) 
     Me.regEx = New Regex(re) 
     Me.inter = inter 
    End Sub 
    Public Function Parse(text As String) As DateTime Implements IDateTimePattern.Parse 
     Dim m = regEx.Match(text) 

     If m.Success Then 
      Return inter(m) 
     End If 
     Return DateTime.MinValue 
    End Function 
End Class 
0

我翻譯一個模式,其餘的東西。現在,你只需要添加其他模式(這不應該是一個大問題,它在模塊1測試

Module Module1 
    Sub Main() 
     Dim res = FuzzyDateTime.Parse("next 2 months") 
    End Sub 
End Module 

Public Interface IDateTimePattern 
    Function Parse(text As String) As DateTime 
End Interface 

Public Class FuzzyDateTime 
    Shared dayList As List(Of String) = New List(Of String)() From {"sun", "mon", "tue", "wed", "thu", "fri", "sat"} 
    Shared parsers As List(Of IDateTimePattern) = New List(Of IDateTimePattern)() 
    Shared Sub New() 
     parsers.Add(New RegexDateTimePattern("next +([2-9]\d*) +months", New Func(Of Match, DateTime)(Function(x As Match) 
                              Dim val = Integer.Parse(x.Groups(1).Value) 
                              Return DateTime.Now.AddMonths(val) 
                             End Function))) 
    End Sub 
    Public Shared Function Parse(ByVal text As String) As DateTime 
     text = text.Trim().ToLower() 
     Dim dt = DateTime.Now 
     For Each parser In parsers 
      dt = parser.Parse(text) 
      If Not dt = DateTime.MinValue Then 
       Exit For 
      End If 
     Next 
     Return dt 
    End Function 
End Class 
Public Class RegexDateTimePattern : Implements IDateTimePattern 
    Protected inter As Func(Of Match, DateTime) 
    Protected regEx As Regex 
    Public Function Parse(text As String) As Date Implements IDateTimePattern.Parse 
     Dim m = regEx.Match(text) 
     If m.Success Then Return inter(m) 
     Return DateTime.MinValue 
    End Function 
    Public Sub New(ByVal re As String, ByVal inter As Func(Of Match, DateTime)) 
     Me.regEx = New Regex(re) 
     Me.inter = inter 
    End Sub 
End Class 

PS:我添加的圖案在靜態構造函數(加入他們你喜歡的地方,雖然)不能與內聯初始化 - 它只是簡單醜陋在我看來。