0

I'm having difficulty translating a C# statement into VB. I don't know if this is a lambda, which I do use but I'm not that expert with, or a separator, which I understand even less. According to Microsoft, the C# '=>' symbol can be used in those 2 different ways.

The line of C# code I'm having trouble with is:

RecurringJob.AddOrUpdate(() => TextBuffer.WriteLine("text"), Cron.Minutely);

It is from the Startup.cs file in the sample hangfire.mvcApplication project. This is the full Class:

public void Configuration(IAppBuilder app)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("DefaultConnection");
    app.UseHangfireDashboard();
    app.UseHangfireServer();
    RecurringJob.AddOrUpdate(
        () => TextBuffer.WriteLine("Recurring Job completed successfully!"), 
        Cron.Minutely);
}

the TextBuffer.WriteLine reference is:

public static void WriteLine(string value)
{
    lock (Buffer)
    {
        Buffer.AppendLine(String.Format("{0} {1}", DateTime.Now, value));
    }
}

This is what I tried, which is not really accurate; I tried a lot of variations:

RecurringJob.AddOrUpdate(Function() TextBuffer.WriteLine("text"), Cron.Minutely)

And the syntax error I get in Visual Studio is:

Expression does not produce a value

I went to the definitions for AddOrUpdate and saw the following overloads, but I'm not sure which is the proper selection to use nor how the syntax would look once I pick the proper overload.

Public Shared Sub AddOrUpdate(methodCall As Expression(Of Action), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Action), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Func(Of Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(methodCall As Expression(Of Func(Of Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Action), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Action), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Func(Of Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(recurringJobId As String, methodCall As Expression(Of Func(Of Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Action(Of T)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Action(Of T)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Func(Of T, Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(methodCall As Expression(Of Func(Of T, Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Action(Of T)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Action(Of T)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Func(Of T, Task)), cronExpression As Func(Of String), Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")
Public Shared Sub AddOrUpdate(Of T)(recurringJobId As String, methodCall As Expression(Of Func(Of T, Task)), cronExpression As String, Optional timeZone As TimeZoneInfo = Nothing, Optional queue As String = "default")

I just don't understand what the method is looking for or how to properly write it.

2
  • 1
    In C#, every method is a function with those that don't return a value having a type of void. In VB, methods that don't return a value are Subs, not Functions. The same goes for Lambdas, which are just anonymous methods. Just note that, when LINQ was originally introduced, VB did not support action Lambdas, i.e. Subs, but only value Lambdas, i.e. Functions. If you're using an older version of VB that doesn't support action Lambdas then you'll need to use a named method and create a delegate for that. Feb 14, 2019 at 2:03
  • Nice explanation, thanks. I am using VS2017, so I suppose the older version caution doesn't apply.
    – Alan
    Feb 14, 2019 at 2:39

1 Answer 1

3

VB.NET has two different lambda expressions: Function, which returns a value, and Sub, which does not.

In this case, you want to use Sub, as your expression does not return a value.

RecurringJob.AddOrUpdate(Sub() TextBuffer.WriteLine("text"), Cron.Minutely)
1
  • Well, I don't feel so bad, because I actually tried what you wrote above, but I must have had some minor syntax issue - using your line above passes the checker. Thanks! I think I had 2 closing parenthesis, and so figured my whole line was wrong, but I had been quite close. Your answer with @jmcillhinney's comment above clears it all up for me. Learned something new tonight. Nice link for me to study on the VB lambda's, too.
    – Alan
    Feb 14, 2019 at 2:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.