Looking for VB Equivalent

A

AMDRIT

I am working with ObservableCollection and looking to implement sorting. I
ran across code from Paul Stovell and he has:

In the collection class the derives from ObservableCollection
public void Sort(Comparison<T> comparrison)

In the logic when initializing the collection class

....
_transactions.Sort(
delegate(Transaction lhs, Transaction rhs) {
return lhs.Date.CompareTo(rhs.Date);
});
....

My question is, how to I represent this same logic? I am unclear on how to
create a delegate function on the fly like that.

I assume that first of all, VB has a white space issue and does not allow me
to create a function pointer on the fly.

So here is what I have done:

In the object class<Transaction> that belongs to the collection (Perhaps, I
should place this in the collection wrapper instead)
Public Shared Function CompareTo(ByVal lhs As Transaction, ByVal rhs
As Transaction) As Integer
Return lhs.Date.CompareTo(rhs.Date)
End Function

I then replaced the sort statement with:
pobjTransactions.Sort(AddressOf Transaction.CompareTo)

Am I missing something here, or is this what is required?


Thanks in advance
 
N

Nicholas Paldino [.NET/C# MVP]

AMDRIT,

Anonymous delegates do not exist in VB, as far as I know. However, you
can create lambda expressions if you are using .NET 3.5. The form for what
you want would be:

Function(ByVal lha As Transaction, ByVal rhs As Transaction)
lhs.Date.CompareTo(rhs.Date)

As opposed to:

delegate(Transaction lhs, Transaction rhs) { return
lhs.Date.CompareTo(rhs.Date) }

Before .NET 3.5, you have to create a method and use a delegate that
points to that method.
 
H

Herfried K. Wagner [MVP]

AMDRIT said:
My question is, how to I represent this same logic? I am unclear on how
to create a delegate function on the fly like that.

VB currently does not support anonymous delegates.
I assume that first of all, VB has a white space issue and does not allow
me to create a function pointer on the fly.

So here is what I have done:

In the object class<Transaction> that belongs to the collection (Perhaps,
I should place this in the collection wrapper instead)
Public Shared Function CompareTo(ByVal lhs As Transaction, ByVal
rhs As Transaction) As Integer
Return lhs.Date.CompareTo(rhs.Date)
End Function

I then replaced the sort statement with:
pobjTransactions.Sort(AddressOf Transaction.CompareTo)

Am I missing something here, or is this what is required?

You could utilize the technique shown in the following sample to pass
parameters to the method:

<URL:http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/84f655d805ba3e38>
 
A

AMDRIT

Thanks Nicholas and Herfried.

Not only did I learn a new term "Anonymous Delegate", but you have given me
a better understanding of the code surrounding it. I appreciate your help.
 
J

Jon Skeet [C# MVP]

Not only did I learn a new term "Anonymous Delegate", but you have given me
a better understanding of the code surrounding it. I appreciate your help.

Just to be picky, I don't believe that "anonymous delegate" is
standard terminology. There's:

1) Anonymous method - what's being talked about here
2) Lambda expression - the C# 3 feature which is like anonymous
methods but better
3) Anonymous function - term for "anonymous method or lambda
expression"

I only bring it up because I've seen the mistaken term "anonymous
delegate" used a few times recently.

Jon
 
H

Herfried K. Wagner [MVP]

Jon,

Jon Skeet said:
Just to be picky, I don't believe that "anonymous delegate" is
standard terminology. There's:

1) Anonymous method - what's being talked about here
2) Lambda expression - the C# 3 feature which is like anonymous
methods but better
3) Anonymous function - term for "anonymous method or lambda
expression"

I only bring it up because I've seen the mistaken term "anonymous
delegate" used a few times recently.

I agree with the terms you have listed above. Normally I'd use the term
"anonymous method" in this case too and I do not know why I have chosen
another term.
 

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