C
C#3y ago
phattanuki

Should you assert input arguments ins

Why am I getting a null-reference error here?
namespace Joust_.Manager {
public static class GenerateSkillList {
public static List<ISkill> AllSkills { get; } = GenerateSkillList.Setup();
private static List<ISkill> Setup() {
//do I have a better way to do this?
AllSkills.Add(new Skills.Attack());
AllSkills.Add(new Skills.Defend());
AllSkills.Add(new Skills.Warcry());
AllSkills.Add(new Skills.ThrowSmellyCheese());
return AllSkills;
}
}
}
namespace Joust_.Manager {
public static class GenerateSkillList {
public static List<ISkill> AllSkills { get; } = GenerateSkillList.Setup();
private static List<ISkill> Setup() {
//do I have a better way to do this?
AllSkills.Add(new Skills.Attack());
AllSkills.Add(new Skills.Defend());
AllSkills.Add(new Skills.Warcry());
AllSkills.Add(new Skills.ThrowSmellyCheese());
return AllSkills;
}
}
}
10 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
phattanuki
phattanukiOP3y ago
it can not return...null? new Skills.Attack(), meaning create a new object from namespace from Skill from the class Attack is this because the attack is a record? not a class? example:
using Joust_.Interfaces;

namespace Joust_.Skills {
internal record Attack : ISkill {
public string SkillName { get; init; }
public SkillTypes SkillType { get; init; }

private const double CriticalChance = 0.7;

private const double CriticalHit = 1.5;

private const int NormalHit = 1;

public Attack() {
SkillName = nameof(Attack);
SkillType = SkillTypes.Targeted;
}

public void Action(ref IContender Challanger, ref IContender Defender) {
Random critChance = new();
Double Multiplier = critChance.NextDouble() > CriticalChance ? CriticalHit : NormalHit;
Defender.HP -= Convert.ToInt32((Challanger.Strength - Defender.Armor) * Multiplier);
}
}
}
using Joust_.Interfaces;

namespace Joust_.Skills {
internal record Attack : ISkill {
public string SkillName { get; init; }
public SkillTypes SkillType { get; init; }

private const double CriticalChance = 0.7;

private const double CriticalHit = 1.5;

private const int NormalHit = 1;

public Attack() {
SkillName = nameof(Attack);
SkillType = SkillTypes.Targeted;
}

public void Action(ref IContender Challanger, ref IContender Defender) {
Random critChance = new();
Double Multiplier = critChance.NextDouble() > CriticalChance ? CriticalHit : NormalHit;
Defender.HP -= Convert.ToInt32((Challanger.Strength - Defender.Armor) * Multiplier);
}
}
}
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
phattanuki
phattanukiOP3y ago
huh, I need a backing field for this?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
phattanuki
phattanukiOP3y ago
ah, hm then can I write
AllSkills = new List<ISkill>() { GenerateSkillList.Setup() }
AllSkills = new List<ISkill>() { GenerateSkillList.Setup() }
or something
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
phattanuki
phattanukiOP3y ago
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
phattanuki
phattanukiOP3y ago
how about a private static constructor?
Want results from more Discord servers?
Add your server