469,336 Members | 5,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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 1835
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
10 posts views Thread by William Krick | last post: by
2 posts views Thread by Srini | last post: by
reply views Thread by adebaene | last post: by
2 posts views Thread by =?iso-8859-1?q?Jean-Fran=E7ois_Michaud?= | last post: by
8 posts views Thread by thomas | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.