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

  • Thread starter Thread starter Siegfried Heintze
  • Start date Start date
S

Siegfried Heintze

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.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,string>>(System.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,string>
ftf, bool x=>MessageBox.Show(ftf); );
foreach
(System.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,
string> fft in q_ll.FontFamily.FamilyNames)
{
var key = fft.Key.ToString();
var val = fft.Value.ToString();
MessageBox.Show("key = \"" + key + "\" value= \"" + val+"\"");
}
 
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
 
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.Collections.Generic.KeyValuePa­ir<System.Windows.Markup.XmlLanguage,string>>(System.Collections.Generic.Ke­yValuePair<System.Windows.Markup.XmlLanguage,string>
ftf, bool x=>MessageBox.Show(ftf); );
            foreach
(System.Collections.Generic.KeyValuePair<System.Windows.Markup.XmlLanguage,
string> fft 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+"\"");
            }
 
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 -
 
In your case, the only real advantage from using C# 3.0 would be type
inference for the foreach variable:

Agreed; although actually I'd probably use "string" for key/val (I
realise they were "var" in the OP). Other than that, *perhaps*
string.Format might be more readable than the naked concatenation (but
slightly slower, too):

foreach (var fft in q_ll.FontFamily.FamilyNames)
{
MessageBox.Show(string.Format("key = \"{0}\" value=
\"{1}\"", fft.Key, fft.Value);
}

Marc
 
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
 
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<T> has 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).
 

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

Back
Top