C
C#2y ago
thieved

how can i add a function/method to a class that can be changed upon construction?

namespace Elfin
{
public class ElfinCommand
{
public string Name;

public ElfinCommand(ElfinCommand data)
{
this.Name = data.Name;
this.Respond = data.Respond;
}
}
}
namespace Elfin
{
public class ElfinCommand
{
public string Name;

public ElfinCommand(ElfinCommand data)
{
this.Name = data.Name;
this.Respond = data.Respond;
}
}
}
i'm trying to make a command class but how can i make it so i can provide a response function to the command when i construct a new one? (this.Respond)
23 Replies
thieved
thieved2y ago
im new to C# by the way oh hey moles i remember u mentioned delegates but im not sure how id use it.. elaborate
public string Name;
public delegate Respond;
public string Name;
public delegate Respond;
i tried this but it says
thieved
thieved2y ago
thieved
thieved2y ago
oh maybe i should provide a return type too lol now how can i change it in the construction?
public ElfinCommand(ElfinCommand data)
{
this.Name = data.Name;
this.Respond = data.Respond;
}
public ElfinCommand(ElfinCommand data)
{
this.Name = data.Name;
this.Respond = data.Respond;
}
i cant do this.Respond = data.Respond;
thieved
thieved2y ago
thieved
thieved2y ago
i see so how can i do what im looking for? i need to be able to create a method/change it upon construction example if you can? i hat eto be a bother im just not familiar with these concepts lol
Florian Voß
Florian Voß2y ago
lambda goes Hmm
thieved
thieved2y ago
i understand how to create a delegate now but is there a way to process it this way
using DSharpPlus.Entities;

namespace Elfin
{
public class ElfinCommand
{
public string Name;
public delegate void Respond(DiscordMessage Message);

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}
}
using DSharpPlus.Entities;

namespace Elfin
{
public class ElfinCommand
{
public string Name;
public delegate void Respond(DiscordMessage Message);

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}
}
? i want to assign it in the constructor if possible oh ok
Florian Voß
Florian Voß2y ago
how about using Action<T> ye
thieved
thieved2y ago
what is Action? oh
Florian Voß
Florian Voß2y ago
there are differen delegate types predefined Func, Predicate, Action read online for reference action is void ye
thieved
thieved2y ago
using DSharpPlus.Entities;

namespace Elfin
{
public class ElfinCommand
{
public string Name;
public Action<DiscordMessage> Respond;

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}
}
using DSharpPlus.Entities;

namespace Elfin
{
public class ElfinCommand
{
public string Name;
public Action<DiscordMessage> Respond;

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}
}
looks good?
Florian Voß
Florian Voß2y ago
and they all exist with overloads up to like 18 parameters or smth like Func<T1, T2, T3........., TResult>
thieved
thieved2y ago
oh
[Command("greet")]
[Command("greet")]
is this a macro? or something of the sort its used in D#+ to create new commands
Florian Voß
Florian Voß2y ago
we call those attribute
thieved
thieved2y ago
o ah okay so now if i want to make a new command how do i write the response function? like to fit the action type
Respond = (DiscordMessage Message) => {

}
Respond = (DiscordMessage Message) => {

}
figured it out
using DSharpPlus.Entities;

namespace Elfin.Commands
{
public class ElfinCommand
{
public required string Name;
public required Action<DiscordMessage> Respond;

public ElfinCommand()
{ }

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}

public class ElfinClient
{
public ElfinCommand[] Commands = {
new ElfinCommand() {
Name = "ping",
Respond = async (DiscordMessage Message) => {
await Message.RespondAsync("Pong!");
}
}
};
}
}
using DSharpPlus.Entities;

namespace Elfin.Commands
{
public class ElfinCommand
{
public required string Name;
public required Action<DiscordMessage> Respond;

public ElfinCommand()
{ }

public ElfinCommand(ElfinCommand CommandData)
{
this.Name = CommandData.Name;
this.Respond = CommandData.Respond;
}
}

public class ElfinClient
{
public ElfinCommand[] Commands = {
new ElfinCommand() {
Name = "ping",
Respond = async (DiscordMessage Message) => {
await Message.RespondAsync("Pong!");
}
}
};
}
}
i need to move elfinclient but its going well :D
Florian Voß
Florian Voß2y ago
just Respond = (message) => //code; should be enough
Respond = async (message) => await message.RespondAsync("Pong!");
Respond = async (message) => await message.RespondAsync("Pong!");
thieved
thieved2y ago
omg im getting so good catlaugh
using Elfin.Commands;

namespace Elfin.Core
{
public class ElfinClient
{
public ElfinCommand[] Commands = {
new ElfinCommand() {
Name = "ping",
Respond = async (Message) => {
await Message.RespondAsync("Pong!");
}
}
};
}
}
using Elfin.Commands;

namespace Elfin.Core
{
public class ElfinClient
{
public ElfinCommand[] Commands = {
new ElfinCommand() {
Name = "ping",
Respond = async (Message) => {
await Message.RespondAsync("Pong!");
}
}
};
}
}
thieved
thieved2y ago
thieved
thieved2y ago
Florian Voß
Florian Voß2y ago
Ok
thieved
thieved2y ago
C# is so fun
Florian Voß
Florian Voß2y ago
yep
thieved
thieved2y ago
thanks guys ❤️