Question

I downgraded my app from version 4 of the framework to version 4 and now I want to implement this VB.NET lambda function statement (which works on 3.5)

Dim colLambda As ColumnItemValueAccessor = Function(rowItem As Object) General_ItemValueAccessor(rowItem, colName)

and rewrite it in C#. This was my attempt:

ColumnItemValueAccessor colLambda = (object rowItem) => General_ItemValueAccessor(rowItem, colName);

When I did this, I get the following error:

Error   14  One or more types required to compile a dynamic expression cannot be found. Are you missing references to Microsoft.CSharp.dll and System.Core.dll? C:\Source\DotNet\SqlSmoke\SqlSmoke\UserControls\ScriptUserControl.cs    84  73  SqlSmoke

However, when I downgraded the app from version 4.0 of the framework to 3.5 (because our users only hae 3.5 and don't have rights to install 4.0). when I did this, the reference to "Microsoft.CSharp" was broken.

Can I rewrite the VB.NET command in C# using syntax that is valid in C# 3.5 as I was able to in VB.NET? What would the syntax be?

I'm thinking that if I want to stay on 3.5, which is a must, then I have to write this code in VB.NET because it looks like C# got this functionality after VB.

namespace BinaryComponents.SuperList
{
    public delegate object ColumnItemValueAccessor(object rowItem);
}

private object General_ItemValueAccessor(DataRow rowItem, object colName)
{
    DataRow rowPerson = (DataRow)rowItem;
    return rowPerson[colName.ToString()].ToString();
}
Was it helpful?

Solution

Dynamic typing in C# is new to .Net 4.0.

Don't use dynamic.

OTHER TIPS

Just be sure to have the right using

using System.Linq;

The issue seems to be that one of the types don't match. Change the delegate parameter type from object to DataRow:

public delegate object ColumnItemValueAccessor(DataRow rowItem);

As AVD noted, you also need to fix the syntax on the lambda expression (the type object for rowItem is implied, it shouldn't be specified there):

ColumnItemValueAccessor colLambda = (rowItem) => General_ItemValueAccessor(rowItem, colName);

The key point is that when you write a lambda expression for the delegate type ColumnItemValueAccessor, the parameters and the return type of the lambda must match the delegate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top