Inline Function/Lambda Expression (C#-->VB)

S

Scott Rymer

I stumbled upon Peter Kellner's ObjectDataSource Generation for Profiles
which generates a Membership class to use in ASP.NET. The only problem is,
it's in C# and I need it in VB to use it in my project. Can anyone help me
convert this into VB.NET? In particular, I'm trying to convert this code:

Comparison<MembershipUserWrapperForMP> comparison = null;

switch (sortDataBase)
{

case "FirstName":
comparison = new
Comparison<MembershipUserWrapperForMP>(
delegate(MembershipUserWrapperForMP lhs,
MembershipUserWrapperForMP rhs)
{
if (lhs.FirstName == null || rhs.FirstName ==
null)
{
return 1;
}
else
{
return
lhs.FirstName.CompareTo(rhs.FirstName);
}
}
);
break;
case "LastName":
comparison = new
Comparison<MembershipUserWrapperForMP>(
delegate(MembershipUserWrapperForMP lhs,
MembershipUserWrapperForMP rhs)
{
if (lhs.LastName == null || rhs.LastName ==
null)
{
return 1;
}
else
{
return lhs.LastName.CompareTo(rhs.LastName);
}
}
);
break;

I think I'm almost there with:

Case "FirstName"
Dim comparison As Comparison(Of
MembershipUserWrapperForMP)(Function(ByVal lhs As
MembershipUserWrapperForMP, ByVal rhs As MembershipUserWrapperForMP)
If (lhs.FirstName Is Nothing) OrElse (rhs.FirstName Is Nothing)
Then Return 1 Else Return lhs.FirstName.CompareTo(rhs.LastName)
Case "Last Name"
etc...

but I'm getting Expresssion Expected at the end of the Dim line and lhs and
rhs not declared in the If line...


-Scott
 
H

Herfried K. Wagner [MVP]

Scott Rymer said:
I stumbled upon Peter Kellner's ObjectDataSource Generation for Profiles
which generates a Membership class to use in ASP.NET. The only problem is,
it's in C# and I need it in VB to use it in my project. Can anyone help me
convert this into VB.NET? In particular, I'm trying to convert this code:

VB does not support anonymous methods. You can, however, extract the code
to an ordinary function and pass the function to the parameter using
'AddressOf <function name>'.
 
S

Scott Rymer

After searching and reading all afternoon, I was beginning to get the
impression that was the case...

I ended up getting to work via:

Case "FirstName" : comparison = New Comparison(Of MyClass)(Address Of
CompareFirstName)
Case "LastName" : comparison = New Comparison(Of MyClass)(Address Of
CompareLastName)
etc...

Private Function CompareFirstName(ByVal x As MyClass, ByVal y As MyClass) As
Integer
Return x.FirstName.CompareTo(y.FirstName)
End Function
etc...

I just had to create 15 functions to cover each property which is what I was
hoping to avoid...

Thanks
-Scott

etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top