1

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();
}
1
  • Can you tell us how exactly ColumnItemValueAccessor and General_ItemValueAccessor are defined? Commented Aug 31, 2012 at 3:57

3 Answers 3

1

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

Don't use dynamic.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain? I'm a bit new to this functionality. Perhaps, change the syntax of the line?
Perhaps you're using "dynamic" keyword somewhere in you program.
I used a code converter and didn't realize that "string colName = DataCol.ColumnName;" was previously defined as "dynamic colName = DataCol.ColumnName;." I thought the statements that I posted were using "dynamic typing." Until you mentioned that dynamic was a keyword, I searched for it and -- bingo. Thanks.
0

Just be sure to have the right using

using System.Linq;

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.