473,325 Members | 2,785 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,325 software developers and data experts.

Wanted: Help using predicate? How to set new font?

Can someone help me convert this to the latest C# syntax using
predicate/delegate? You can see my attempt in the comments.

Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I
want to change its font.
//q_ul.FontFamily.FamilyTypefaces.All<System.Collect ions.Generic.KeyValuePair<System.Windows.Markup.Xm lLanguage,string>>(System.Collections.Generic.KeyV aluePair<System.Windows.Markup.XmlLanguage,string>
ftf, bool x=>MessageBox.Show(ftf); );
foreach
(System.Collections.Generic.KeyValuePair<System.Wi ndows.Markup.XmlLanguage,
stringfft in q_ll.FontFamily.FamilyNames)
{
var key = fft.Key.ToString();
var val = fft.Value.ToString();
MessageBox.Show("key = \"" + key + "\" value= \"" + val+"\"");
}

Aug 3 '08 #1
5 1975
Can someone help me convert this to the latest C# syntax using
predicate/delegate? You can see my attempt in the comments.
I doubt you mean predicate (a predicate is a boolean test on
something, such as a filter). Re using the latest C# syntax,
personally I wouldn't here... I'd stick with regular C# unless there
is a good reason to complicate things. There is nothing wrong with
regular "foreach".

Perhaps it might help if you clarified what you are actually trying to
do?

Marc
Aug 4 '08 #2
On Aug 4, 3:38*am, Siegfried Heintze
<SiegfriedHein...@discussions.microsoft.comwrote :
Can someone help me convert this to the latest C# syntax using
predicate/delegate? You can see my attempt in the comments.

Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I
want to change its font.

//q_ul.FontFamily.FamilyTypefaces.All<System.Collect ions.Generic.KeyValuePa*ir<System.Windows.Markup.X mlLanguage,string>>(System.Collections.Generic.Ke* yValuePair<System.Windows.Markup.XmlLanguage,strin g>
ftf, bool x=>MessageBox.Show(ftf); );
* * * * * * foreach
(System.Collections.Generic.KeyValuePair<System.Wi ndows.Markup.XmlLanguage,
stringfft in q_ll.FontFamily.FamilyNames)
* * * * * * {
* * * * * * * * var key = fft.Key.ToString();
* * * * * * * * var val = fft.Value.ToString();
* * * * * * * * MessageBox.Show("key = \"" + key + "\" value= \"" + val+"\"");
* * * * * * }
There is nothing analogous to foreach (i.e., doing some imperative
action for every element in a sequence) in LINQ - probably because
foreach is already as good as it gets. Abusing All() (or Select(), or
whatever) to emulate foreach does not serve a point, and may be
harmful in the long run if you ever decide to move to something like
PLINQ.

In your case, the only real advantage from using C# 3.0 would be type
inference for the foreach variable:

* * * * * * foreach (var fft in q_ll.FontFamily.FamilyNames)
* * * * * * {
* * * * * * * * var key = fft.Key.ToString();
* * * * * * * * var val = fft.Value.ToString();
* * * * * * * * MessageBox.Show("key = \"" + key + "\" value= \"" +
val+"\"");
* * * * * * }
Aug 4 '08 #3
On 4 Aug, 07:06, Pavel Minaev <int...@gmail.comwrote:
On Aug 4, 3:38*am, Siegfried Heintze

<SiegfriedHein...@discussions.microsoft.comwrote :
Can someone help me convert this to the latest C# syntax using
predicate/delegate? You can see my attempt in the comments.
Also: how would I set a new font for q_ul? q_ul is a WPF TextBlock and I
want to change its font.
//q_ul.FontFamily.FamilyTypefaces.All<System.Collect ions.Generic.KeyValuePa**ir<System.Windows.Markup. XmlLanguage,string>>(System.Collections.Generic.K* e*yValuePair<System.Windows.Markup.XmlLanguage,str ing>
ftf, bool x=>MessageBox.Show(ftf); );
* * * * * * foreach
(System.Collections.Generic.KeyValuePair<System.Wi ndows.Markup.XmlLanguage,
stringfft in q_ll.FontFamily.FamilyNames)
* * * * * * {
* * * * * * * * var key = fft.Key.ToString();
* * * * * * * * var val = fft.Value.ToString();
* * * * * * * * MessageBox.Show("key = \"" + key + "\" value= \"" + val+"\"");
* * * * * * }

There is nothing analogous to foreach (i.e., doing some imperative
action for every element in a sequence) in LINQ - probably because
foreach is already as good as it gets. Abusing All() (or Select(), or
whatever) to emulate foreach does not serve a point, and may be
harmful in the long run if you ever decide to move to something like
PLINQ.

In your case, the only real advantage from using C# 3.0 would be type
inference for the foreach variable:

** * * * * * foreach (var fft in q_ll.FontFamily.FamilyNames)
** * * * * * {
** * * * * * * * var key = fft.Key.ToString();
** * * * * * * * var val = fft.Value.ToString();
** * * * * * * * MessageBox.Show("key = \"" + key + "\" value= \"" +
val+"\"");
** * * * * * }- Hide quoted text -

- Show quoted text -
Aug 4 '08 #4
OK, you have convinced me to abandon the new LINQ lambda functions for
production code.

But I'm curious: could I make them work? The intellisense shows All and I
tried to make them work and I could not.

Incidently, I solved my font problem. I had to create a whole new font
object instead of just replacing the font-family object the current font
object.

Thanks,
Siegfried
Aug 5 '08 #5
On Aug 5, 11:27*am, "Siegfried Heintze" <siegfr...@heintze.comwrote:
OK, you have convinced me to abandon the new LINQ lambda functions for
production code.
I do apogize - it was not my intention, and you will probably want to
reconsider this. LINQ is immensely useful in production - when used
where it is supposed to be used.
But I'm curious: could I make them work? The intellisense shows All and I
tried to make them work and I could not.
Of course you can make them work! The point was that the way you tried
to use All() was not what it was designed to solve. If you look in
MSDN, here's the description of All():

"Determines whether all elements of a sequence satisfy a condition."

For instance, if you wanted to check whether all font families in your
collection are some variations of Courier, you could do:

if (families.All(family =family.Name.Contains("Courier")) { ... }

Which is more terse then using foreach to iterate and check it
manually, and at the same time makes the intent clearer.

However, the example you've given - which simply enumerated the
families and displayed their names - is precisely what plain foreach
is intended to cover; therefore, there's no LINQ method to do it (why
duplicate?). List<Thas ForEach() (since 2.0, even before LINQ),
which is somewhat LINQish in appearance, but I never really understood
its point - it's not shorter, it's not faster, and it does the same
thing, so why bother? Apparently, LINQ designers thought the same.

In short, use LINQ methods when you need to query or transform
collections - projection, slicing, sorting, grouping etc. Use foreach
when you need to apply some action to each element of the collection
(such as printing it, saving it to a file, drawing it etc).
Aug 5 '08 #6

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

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
4
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
2
by: sianan | last post by:
I am having a problem doing the following in generics. I have two list of a custom item type. I need to iterate through the first list and match each item against another list to see if there is...
10
by: William Krick | last post by:
I am writing an XSL transform that converts XML data about vehicles into XML data that will fill printed forms. The default form can handle up to 5 vehicles which I handle using subscripts... ...
2
by: Srini | last post by:
Does anyone know if we can do SELECTIVE unload/load data from/to a Tablespace using Where condition on z/OS with IBM DB2 UNLOAD / LOAD utility? Thanks, Srini
0
by: adebaene | last post by:
Hello all, Has everyone tried to use the functions taking a Predicate in Generics container in C++/CLI? Say I have a List<MyClass^>^ my_array, and I want to call RemoveAll on it. How would...
2
by: =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= | last post by:
Hello guys, I was wondering if it was possible to reference a boolean predicate in a variable. Basically I want to do with the boolean predicate what you would do with any other variable; I want...
8
by: thomas | last post by:
priority_queue usually uses the greater<intpredicate function. But as you know, we don't always use priority_queue<int>. Actually we may need the "priority_queue<pair<int,int>,...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.