Reputation: 1031
I'm working on a Discord Bot and one of the commands is to pick a random number between 1 and 4, called 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.
I've tried making them nullable, using int?
and set them to null
, but I get an error that states Nullable Objects must have a Value. 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."
Upvotes: 0
Views: 938
Reputation: 14064
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
.
You can change that line to something like the following to handle the cases when the arguments are 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.
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.
Like Alexei suggested in the comments, you could also change the signature to something like:
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.
Upvotes: 2
Reputation: 8743
If you want that the modifiers are ignored, if they are not entered, try 0 as a default value. 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());
}
Upvotes: 2