473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Delegates, not anonymous methods

I can do this in VB:

For Each fld As Field In _
fieldList.FindAll(AddressOf SearchSelOrKeyNotID)
'do stuff here
Next

where the function is defined:

Private Shared Function _
SearchSelOrKeyNotID(ByVal f As Field) As Boolean
Return Not f.IsIdentity _
AndAlso (f.IsSelected OrElse f.IsKey)
End Function

And fieldList is defined:

Dim fieldList As List(Of Field)

In C#, I can do this with an anonymous method like this (which is cool):

foreach (Field fld in fieldList.FindAll(
delegate(Field f)
{
return (Not f.IsIdentity && (f.IsSelected || f.IsKey));
}))
{
//do stuff
}

where fieldList is defined as:

List<FieldfieldList;

However, I want to use this repeatedly, so I want to know how to use a
function, the way I do in VB. I would appreciate any help you can give me.
(And I hope I got all those pesky curly braces and semi-colons right.)

Thanks in advance,
Robin S.
Mar 7 '07 #1
4 1939
Note that there are conversion tools around - or you can simply
compile it (from VB) and look in "reflector" to see how the same code
is written in VB.Net and C#. But it should be something like below.

Marc

// field declaration
List<FieldfieldList;
// enumeration
void Test() {
foreach(Field fld in fieldList.FindAll(SearchSelOrKeyNotID)) {
// do stuff here
}
}
// predicate
private static bool SearchSelOrKeyNotID(Field f) {
return !f.IsIdentity && (f.IsSelected || f.IsKey);
}

Mar 7 '07 #2
Cool. That worked like a charm. It never occured to me to just stick the
function name in as the FindAll parameter.

Robin S.
-------------------------------------------
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@q40g2000cwq.googlegr oups.com...
Note that there are conversion tools around - or you can simply
compile it (from VB) and look in "reflector" to see how the same code
is written in VB.Net and C#. But it should be something like below.

Marc

// field declaration
List<FieldfieldList;
// enumeration
void Test() {
foreach(Field fld in fieldList.FindAll(SearchSelOrKeyNotID)) {
// do stuff here
}
}
// predicate
private static bool SearchSelOrKeyNotID(Field f) {
return !f.IsIdentity && (f.IsSelected || f.IsKey);
}

Mar 7 '07 #3
That is a 2.0 nicety. In 1.1 syntax (give or take the generics) you
would have had to use "new Predicate<Field>(SearchSelOrKeyNotID)".
Both forms are common, especially since the VS designer (bless it)
uses the long version, as does the VS editor when using [Tab] to auto-
create a handler following "+=". Presumably this is to pre-emptively
disambiguate, but for the latter I usually trim it afterwards... I
wouldn't touch the designer code, though (it can be fussy...).

Marc

Mar 9 '07 #4
I'm glad I'm using .Net 2.0. But it's always good to know some 1.1 in case
it comes up somewhere.

I wouldn't touch the designer code. I'd never remember to go change it
again after I regenerated it!

Thanks again,
Robin S.
-----------------------
"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@h3g2000cwc.googlegro ups.com...
That is a 2.0 nicety. In 1.1 syntax (give or take the generics) you
would have had to use "new Predicate<Field>(SearchSelOrKeyNotID)".
Both forms are common, especially since the VS designer (bless it)
uses the long version, as does the VS editor when using [Tab] to auto-
create a handler following "+=". Presumably this is to pre-emptively
disambiguate, but for the latter I usually trim it afterwards... I
wouldn't touch the designer code, though (it can be fussy...).

Marc

Mar 9 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Sam | last post by:
I’m just starting to learn delegates. I’m at the very beginning. If I understand correctly, delegates are for when you want to pass a function as a parameter. For example the client provides a...
2
by: Marcos Stefanakopolus | last post by:
In C# is there any way to use the concept of delegates to have multiple implementations of a particular block of code, so that you can choose between them without the overhead of possibly expensive...
8
by: Brian Hampson | last post by:
I'm new to this delegate thing, and I need to use it to do UI updates from a backgroundworker thread. I have a method with overloaded signatures of: void MyMethod(string MyString); void...
15
by: Matt | last post by:
Hi There, Can anyone explain me the real advantages of (other than syntax) lambda expressions over anonymous delegates? advantage for one over the other. delegate int F(int a); F fLambda = a...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
5
by: raylopez99 | last post by:
I understand delegates (static and non-static) and I agree they are very useful, and that the "Forms" used in the Windows .NET API could not work without them. That said, I'm curious as to how...
4
by: Frankie | last post by:
I have just gotten up to speed on what anonymous methods are (syntax, capabilities, etc), and how they can be used with /called via delegates. What I am wondering is... 1. Are they only/mostly...
10
by: tshad | last post by:
I don't think there are delegates for VB as there is for C#. Why is that and are they planning to add this? This seems to be the only thing that VB doesn't have that C# does. Thanks, Tom
69
by: raylopez99 | last post by:
They usually don't teach you in most textbooks I've seen that delegates can be used to call class methods from classes that are 'unaware' of the delegate, so long as the class has the same...
5
by: puzzlecracker | last post by:
thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.