new with delegates and lambda expressions [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Basically I want to create a method that can return a string message regarding on what currently happening inside a method
example:
public MainMethod()
{
//Execute One
//Execute Two
//Execute Three
}
upon using it I'm thinking of like this
something = delegate (string message) {console.writeline("{0}",message)};
the output would be
Execute One
Execute Two
Execute Three
Is this possible using delegate or lambda? if yes can I ask for an example on how I should correctly implement this? if no please help me with alternative.
Thanks

Use Func and Action. They make interacting with delegates much easier. Funcs have return values, Actions do not:
public MainMethod()
{
Action<string> writerAction = (message) => Console.WriteLine(message);
writerAction("Execute One");
writerAction("Execute Two");
writerAction("Execute Three");
}

Related

C# Is it possible to run a function with only a string? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Sorry, this is kind of a stupid question. So I’m making myself a personal assistant. I have an array of commands and for each one I have a function. If the user types in a command, how to I get it to go run the function?
Let's define some commands as void methods with no return value.
private void DoCommand1() {
Console.WriteLine("executing command 1");
}
private void DoCommand2() {
Console.WriteLine("executing command 2");
}
You can use a Dictionary to map strings to functions:
var commands = new Dictionary<string, Action>();
commands.Add("command1", () => DoCommand1());
commands.Add("command2", () => DoCommand2());
and then run a command from a string:
string myCommand = "command2";
commands[myCommand].Invoke(); //will print "executing command 2"
You should have a look at delegates and Action Delegates in C#.
You want to make some strongly type input and handle those input in C#. Here is how it can be done.
Make a Enum with all the command that you want to strongly type like Login, Logout for example.
Make a dictionary of
private readonly Dictionary<string, StronglyTypedCommands> localBotCommands = new Dictionary<string, StronglyTypedCommands>();
store some static string for what input you will get and what kind of command it is.
In the method you can figure out what exactly input is by help of this Dictionary.

How to pass a pinged username and a variable to a bot command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am trying to make a ranking system bot and I need to be able to pass in the command another user and an int variable like
[Command(/*#user*/ + " Has won round " + /*int*/)]
Is it even possible? (keep in mind I am pretty much a noobie so a simpler solution is generally better for my needs)
Edit: I forgot to add the discord.net part also in the title which has caused some misunderstandings but now it is fixed. also a little bit of fixing to the wording
The code snippet you provided is completely invalid. The Command attribute is used to define the command name, not accept parameters for the command. Expected command input (command parameters) are set in the command method's signature.
// example command call: !sample #RandomUser 25
[Command("sample")]
public async Task ExampleCommand(IUser user, int num)
{
// Do stuff
await ReplyAsync($"{user.Mention} has won round {25}");
}

Notepad in c# Replace All [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have made replace function, but I am really struggling to make "Replace All"
function.
I tried to make into for loop but that didn't work well...
I have dialog box just like in usual Notepad (another form) and i have richtextbox in Main form. Any ideas how can I make it?
You can simply use the String.Replace method:
private void DoReplace(string SearchFor, string ReplaceWith)
{
RichTextBox1.Text = RichTextBox1.Text.Replace(SearchFor, ReplaceWith);
}
Call this function and supply the string you want to search for in Richtextbox1 (or whatever it is called in your case) as the first parameter and the replacement as the second parameter.

What's desirable about callback functions when you can make a regular function call? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm new to programming and I've noticed that callback functions are passed in as parameters to other functions
Otherfunction(argument,callbackfunction(code...code))
This seems cumbersome no? Why not just do the following:
Otherfunction(argument)
{
callbackfunction();
}
Callbackfunction()
{
//callback function is defined here.
}
Why inundate the parameter list of a function with a callback?
If you don't pass in the callback function, then the called function needs to know which function to call. This severely limits its reuse. Passing in a callback function also allows you to modify its behavior.
Consider the following:
void DoSomethingAndNotify(Action<string> notifyCallback)
{
// Do something
...
string result = "something was done";
notifyCallback(result);
}
void EmailNotifier(string message)
{
// Send message via email
}
void ConsoleNotifier(string message)
{
Console.WriteLine(message);
}
Then you can "do something" with different types of notifications, and you can add new notification types without ever having to change DoSomethingAndNotify().

Best way to do a multithread foreach loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have a send email method with a foreach, like this:
static void Main(string[] args)
{
foreach(var user in GetAllUsers())
{
SendMail(user.Email);
}
}
I need to improve that method. Using a multithread, because i dont want to wait the SendMail method executes each time for each user.
Any sugestions to do that?
Thanks
Try using a parallel foreach. I.e.
Parallel.ForEach(GetAllUsers(), user=>
{
SendMail(user.Email);
});
You can try something like this
private void Send()
{
Parallel.Foreach(GetAllUsers(), user =>
{
SendMail(user.Email);
});
}
I think the easiest way to do this would be thread pooling. .Net makes this pretty easy and you can read more about it here.

Categories