NullReferenceException 和事件
NullReferenceException and events
我是 C# 的新手,我从以下行收到 NullReferenceException:
CommandEntered(readedLine);
其余的相关代码应该是这样的:
using System;
namespace Eksamensprojekt
{
class StregSystemCLI : IStregSystemUI
{
IStregSystem stregSystem = new StregSystem();
public event StregSystemEvent CommandEntered;
public StregSystemCLI(IStregSystem stregSystem)
{
this.stregSystem = stregSystem;
}
public void Start()
{
Console.WriteLine(stregSystem.activeProducts().ToString());
Console.WriteLine("Enter your username and then a product ID and hit enter to buy a product. Example: 'username 49340'");
Console.WriteLine("Alternatively, to buy multiple instances of the same product you can use: 'username amount ID' instead. Example: 'username 10 49340'");
string readedLine = Console.ReadLine();
Console.WriteLine("{0}", readedLine);
if(readedLine != null)
{
CommandEntered(readedLine);
}
}
}
}
namespace Eksamensprojekt
{
class StregSystemCommandParser
{
IStregSystem stregSystem;
IStregSystemUI stregSystemUI;
public StregSystemCommandParser(IStregSystem stregSystem, IStregSystemUI stregSystemUI)
{
stregSystem = new StregSystem( );
stregSystemUI = new StregSystemCLI(stregSystem);
stregSystemUI.CommandEntered += ParseCommand;
}
void ParseCommand(string command)
{
...lots of code here, that I don't think is relevant to the solution
}
}
}
namespace Eksamensprojekt {
public class Program
{
static void Main(string[] args)
{
IStregSystem stregSystem = new StregSystem();
IStregSystemUI ui = new StregSystemCLI(stregSystem);
StregSystemCommandParser sc = new StregSystemCommandParser(stregSystem, ui);
ui.Start();
}
}
}
namespace Eksamensprojekt
{
public delegate void StregSystemEvent(string Command);
}
我希望上面的内容在没有完整代码的情况下也可以阅读。
我想要发生的是,如果在 readedLine 中输入了某些内容并且字符串不为空,我希望它触发一个事件,该事件导致方法 ParseCommand() 在 class StregSystemCommandParser 中成为 运行 .
如何制定修复 NullReferenceException 的解决方案?
您的 CommandEntered
事件没有附加监听器。
call an event 的正确 C# 习语是:
CommandEntered?.Invoke(readedLine);
我是 C# 的新手,我从以下行收到 NullReferenceException:
CommandEntered(readedLine);
其余的相关代码应该是这样的:
using System;
namespace Eksamensprojekt
{
class StregSystemCLI : IStregSystemUI
{
IStregSystem stregSystem = new StregSystem();
public event StregSystemEvent CommandEntered;
public StregSystemCLI(IStregSystem stregSystem)
{
this.stregSystem = stregSystem;
}
public void Start()
{
Console.WriteLine(stregSystem.activeProducts().ToString());
Console.WriteLine("Enter your username and then a product ID and hit enter to buy a product. Example: 'username 49340'");
Console.WriteLine("Alternatively, to buy multiple instances of the same product you can use: 'username amount ID' instead. Example: 'username 10 49340'");
string readedLine = Console.ReadLine();
Console.WriteLine("{0}", readedLine);
if(readedLine != null)
{
CommandEntered(readedLine);
}
}
}
}
namespace Eksamensprojekt
{
class StregSystemCommandParser
{
IStregSystem stregSystem;
IStregSystemUI stregSystemUI;
public StregSystemCommandParser(IStregSystem stregSystem, IStregSystemUI stregSystemUI)
{
stregSystem = new StregSystem( );
stregSystemUI = new StregSystemCLI(stregSystem);
stregSystemUI.CommandEntered += ParseCommand;
}
void ParseCommand(string command)
{
...lots of code here, that I don't think is relevant to the solution
}
}
}
namespace Eksamensprojekt {
public class Program
{
static void Main(string[] args)
{
IStregSystem stregSystem = new StregSystem();
IStregSystemUI ui = new StregSystemCLI(stregSystem);
StregSystemCommandParser sc = new StregSystemCommandParser(stregSystem, ui);
ui.Start();
}
}
}
namespace Eksamensprojekt
{
public delegate void StregSystemEvent(string Command);
}
我希望上面的内容在没有完整代码的情况下也可以阅读。 我想要发生的是,如果在 readedLine 中输入了某些内容并且字符串不为空,我希望它触发一个事件,该事件导致方法 ParseCommand() 在 class StregSystemCommandParser 中成为 运行 . 如何制定修复 NullReferenceException 的解决方案?
您的 CommandEntered
事件没有附加监听器。
call an event 的正确 C# 习语是:
CommandEntered?.Invoke(readedLine);