Steve Rousseau
Steve Rousseau

Reputation: 93

How to use Lambda Expression as a String parameter C#

Just for curiosity, I would like to know if it is possible to use a Lambda Expression as a String parameter?

Here's my snipet:

List<Int64> selectedUsers = GetSelectedUsers();
if (MessageBox.Show(this, ((String message) =>
{
    message = "Are you sure you want to delete user(s) ID";
    foreach (Int64 id in selectedUsers)
    {
        message += " " + id.ToString();
    }
    message += "?";
    return message;
}), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{ 
    //DoSomething();
}

But unfortunately, I get this error "Error 7 Cannot convert lambda expression to type 'string' because it is not a delegate type" here: (String message)

Thanks for your help, it is really appreciated!

Upvotes: 0

Views: 395

Answers (3)

Andrei
Andrei

Reputation: 341

I think this is what you should do

List<Int64> selectedUsers = GetSelectedUsers();

            if (MessageBox.Show(this, ((Func<string, string>)(message =>
            {
                message = "Are you sure you want to delete user(s) ID";
                foreach (Int64 id in selectedUsers)
                {
                    message += " " + id.ToString();
                }
                message += "?";
                return message;
            }))(""), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                //DoSomething();
            }
        }

what we have here is an IFFE. As an optimization you could get rid of the message argument, since you're not really using the initial value. This would become :

List<Int64> selectedUsers = GetSelectedUsers();

            if (MessageBox.Show(this, ((Func<string>)(() =>
            {
                var message = "Are you sure you want to delete user(s) ID";
                foreach (Int64 id in selectedUsers)
                {
                    message += " " + id.ToString();
                }
                message += "?";
                return message;
            }))(), "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                //DoSomething();
            }

I hope this helps

Upvotes: 0

davcs86
davcs86

Reputation: 3935

Build your message before

string ids = selectedUsers.Select(n=>n.ToString()).Aggregate((current, next) => current + ", " + next);
// also works string.Join
// string ids = string.Join(", ",selectedUsers);
string message = "Are you sure you want to delete user(s) ID: "+ids+"?";
if (MessageBox.Show(this, message, "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
{ 
    //DoSomething();
}

Upvotes: 3

David L
David L

Reputation: 33863

If you really wanted to use a delegate of some sort to return a string (and I'm honestly not sure why you would here), you'd need to cast the expression to Func<string> and immediately invoke it.

if (MessageBox.Show(this, 
    ((Func<string>)(() =>
        {
            var message = "Are you sure you want to delete user(s) ID";
            foreach (Int64 id in selectedUsers)
            {
                message += " " + id.ToString();
            }
            message += "?";
            return message;
       })
    )(), 
    "Confirmation Delete User", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
    { 
        //DoSomething();
    }
}

Upvotes: 1

Related Questions