0

I searched all over for a way to pass ToUpper or ToLower as a parameter. I looked at actions, I looked at delegates, I looked at extension methods, and I looked at Microsoft documentation. The closest answer I got was: Passing an extension method to a method expecting a delegate. How does this work?, but that didn't really explain how to do it. I am writing this question in case someone else runs across a similar problem. For example, you can't pass string.ToLower() as a parameter.

The issue is figuring out:

  1. How to call string.toLower() as a delegate?

Example of what I want to be able to do:

orderItems.GetContatenatedModdedNames(string.ToLower());
orderItems.GetConcatenatedModdedNames(string.ToUpper());

The example idea is to be able to pass in ToLower() or ToUpper() as a parameter.

Patrick Knott
  • 1,666
  • 15
  • 15
  • Is there a reason you want to pass the `.ToUpper()` / `.ToLower()` through as a parameter? I would create an enum, pass *that* through, and then upper / lower based on the enum. – Jonathan Jun 28 '21 at 15:46
  • 2
    The point is knowing how to do it, not knowing how to pass an enum, I can do that... I've done it many times... I was trying to learn how to do this... – Patrick Knott Jun 28 '21 at 15:55
  • From your own post, you readily admit that you actually have several questions, which means the post lacks sufficient focus. The root of your question however seems to be about passing the method as a parameter, which the duplicates cover. Note that if you don't know the actual _instance_ you want to call the method on, you can't directly pass the instance's method (obviously, since you don't have an instance). Instead, you need to pass a method that knows how to call the instance's method, once you have the instance. E.g. `x => x.ToUpper()`. The parameter type would be `Func`. – Peter Duniho Jun 28 '21 at 16:07
  • When you posted a question that had multiple questions in it, you cannot expect one (or even several) duplicates to address _every_ part of the post. The duplicates do address the basic concept of passing a method. The only thing remaining is _what_ method to pass; since you don't have the `string` instance in hand, you can't pass the `ToUpper()` method itself. You have to pass another method that knows how to call that when given an instance. **Which I already even told you in my previous comment, and explained how to do.** Please just read the comment. – Peter Duniho Jun 28 '21 at 16:20
  • That said, you should note that the `ToLower()` and `ToUpper()` methods don't modify the `string` instance itself. Calling either by itself, without storing the return value in an appropriate place, will have no effect at all. But that's yet another question. You certainly haven't provided sufficient context for anyone to address that, even if it were appropriate to answer multiple questions in a single post. – Peter Duniho Jun 28 '21 at 16:21

1 Answers1

-1

Here is an example of how to do it... it involved passing an anonymous function.

public static string GetConcatenatedNames(this ICollection<OrderItem> orderItems, string separater = ",", Func<string, string> myFunc = null)
{
    if (myFunc == null)
    {
        myFunc = x => x.ToLower();
    }

    var productNames = orderItems?.Select(x => x.Product)?.Select(x => myFunc(x.ProductName));

    if (productNames == null)
    {
        return null;
    }

    return string.Join(separater, productNames);
}

To call this:

var upperCaseNames = orderItems.GetConcatenatedConcessionNames(myFunc: x => x.ToUpper());
Patrick Knott
  • 1,666
  • 15
  • 15
  • 2
    This approach is quite dangerous. Image the following method `_ => "My Constant"`. It does satisfy the `Func` delegate signature but it is useless. – Peter Csala Jun 28 '21 at 15:06
  • 1
    I would rather recommend to pass a custom enum to the function like `enum Casing { Lower, Upper }` and then map the enum value to the appropriate `ToUpper` or `ToLower` method call. If you want to I can leave post to demonstrate it. – Peter Csala Jun 28 '21 at 15:08
  • Yeah, although it works... I was sure there was a potential issue somewhere... so I figured I'd see if anyone knew a better way... besides not doing it at all. I know how to pass in an enum and do logic according to an enum, thanks but that's not really the knowledge I seek to gain. Remember though, passing an anonymous function can be done for ANY code, not just this example... so... that makes any delegate a potential trap. I'm looking for a right way to be able to pass this kind of method as a delegate. – Patrick Knott Jun 28 '21 at 16:01