简体   繁体   English

如何将值设置为 Null 作为可选参数的 C# 任务?

[英]How do I make a C# Task with values set to Null as Optional Parameters?

I'm working on a Discord Bot and one of the commands is to pick a random number between 1 and 4, called D4.我正在研究 Discord Bot,其中一个命令是选择 1 到 4 之间的随机数,称为 D4。

By itself it works fine, but I wanted to allow for modifiers, where the user gives input and then the bot will produce the result after adding everything together.它本身工作正常,但我想允许修改器,用户提供输入,然后机器人将在将所有内容加在一起后产生结果。

The issue is that I want to allow for up to 5 separate numbers, but not have them be required in order for the Task/Command to function properly.问题是我想允许最多 5 个单独的数字,但不要求它们才能正确执行 function 的任务/命令。

I've tried making them nullable, using int?我试过让它们可以为空,使用int? and set them to null , but I get an error that states Nullable Objects must have a Value.并将它们设置为null ,但我收到一条错误消息,指出可空对象必须具有值。 If they're not nullable, then the error is that there are too few parameters entered if the user does not enter enough modifier values.如果它们不可为空,则错误是如果用户没有输入足够的修饰符值,则输入的参数太少。

Here's how it currently looks -这是它目前的样子 -

public async Task D4(int? modifier1 = 4, int? modifier2 = null, int? modifier3 = null ,
        int? modifier4 = null, int? modifier5 = null)
    {

        int modifiers = (int)(modifier1 + modifier2 + modifier3 + modifier4 + modifier5);
        int randNum = Rnumber.Next(1, 4);

        int sum = randNum + modifiers;

        await Context.Channel.SendMessageAsync(Context.User.Mention + " rolled " + randNum.ToString()
            + " With Modifier the total is: " + sum.ToString());
    }

I'm not sure what I need ot do to solve this.我不确定我需要做什么来解决这个问题。 There's a chance I may be way off.有机会我可能会离开。

The errors I get are "The input text has too few parameters."我得到的错误是“输入文本的参数太少”。 and "Nullable object must have a value."和“可为空的 object 必须有一个值。”

If you want that the modifiers are ignored, if they are not entered, try 0 as a default value.如果您希望忽略修饰符,如果未输入,请尝试将 0 作为默认值。 That way, you don't need nullable integers.这样,您就不需要可为空的整数。 Also, modifiers that have no value provided, won't modify the sum:此外,没有提供值的修饰符不会修改总和:

public async Task D4(int modifier1 = 4, int modifier2 = 0, int modifier3 = 0,
        int modifier4 = 0, int modifier5 = 0)
    {

        int modifiers = modifier1 + modifier2 + modifier3 + modifier4 + modifier5;
        int randNum = Rnumber.Next(1, 4);

        int sum = randNum + modifiers;

        await Context.Channel.SendMessageAsync(Context.User.Mention + " rolled " + randNum.ToString()
            + " With Modifier the total is: " + sum.ToString());
    }

The problem is with the line:问题出在这条线上:

int modifiers = (int)(modifier1 + modifier2 + modifier3 + modifier4 + modifier5);

It's warning you because it wouldn't be able to add the numbers up if one of them were null .这是警告您,因为如果其中一个是null ,它将无法将数字相加。

You can change that line to something like the following to handle the cases when the arguments are null :当 arguments 为null时,您可以将该行更改为如下内容:

int modifiers = modifier1 ?? 0 
                + modifier2 ?? 0 
                + modifier3 ?? 0 
                + modifier4 ?? 0 
                + modifier5 ?? 0;

This will allow you to pass null into the D4() , which is better than passing 0 since 0 is like a magic number, whereas passing null feels more like no value is being passed.这将允许您将 null 传递给D4() ,这比传递 0 更好,因为 0 就像一个幻数,而传递 null 感觉更像是没有传递任何值。


Regarding:关于:

The issue is that I want to allow for up to 5 separate numbers, but not have them be required in order for the Task/Command to function properly.问题是我想允许最多 5 个单独的数字,但不要求它们才能正确执行 function 的任务/命令。

Like Alexei suggested in the comments, you could also change the signature to something like:就像 Alexei 在评论中建议的那样,您也可以将签名更改为:

public async Task D4(params int[] modifiers)
{
    // TODO: Handle the case where too many "modifiers" are passed
    int sum = modifiers.Sum();
    // ...
}

It can be called as:它可以称为:

await D4(1);
await D4(1, 2);
// etc.

The only downside to this approach is that you can't limit the number of "modifiers" at compile time.这种方法的唯一缺点是您不能在编译时限制“修饰符”的数量。

For this particular case, I'd still stick to the earlier example that allows null values.对于这种特殊情况,我仍然坚持前面的示例,它允许 null 值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM