为变量赋值
我有这个变量,它是业务层中类的接口的实例。 我需要从数据访问层发送消息,到业务层,最后到表示层。我在数据访问中的类“LogBinaryWriter”我有这样的:
public class LogBinaryWriter : ILogBinaryWriter
{
private readonly IImageLogBuilder _imageLogBuilder;
public void WriteFrameCodes(string filePath, int logSelected)
{
var fileExists = FileExists(binaryFilePath);
if (fileExists == true)
{
_imageLogBuilder.displayMessage("The file " + binaryFileName + " exist. Dou you want overwrite it? (Y/N)");
}
}
}
我有一条消息:“值_imageLogBuilder从未分配,并且将始终为空”
我该如何解决这个问题?
I have this variable, it's an instance of a Interface of a class in business layer.
I need to send messages since data access layer, to business layer and finally to presentation layer. I my class "LogBinaryWriter" in data access I have this:
public class LogBinaryWriter : ILogBinaryWriter
{
private readonly IImageLogBuilder _imageLogBuilder;
public void WriteFrameCodes(string filePath, int logSelected)
{
var fileExists = FileExists(binaryFilePath);
if (fileExists == true)
{
_imageLogBuilder.displayMessage("The file " + binaryFileName + " exist. Dou you want overwrite it? (Y/N)");
}
}
}
I have a message: "the value _imageLogBuilder is never assigned and will always be null"
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
LogBinaryWriter
的构造函数中实例化IImageLogBuilder
实例,并将其分配给_imageLogBuilder
。您必须在构造函数中执行此操作,因为您已将_imageLogBuilder
标记为readonly
。例如,假设您有一个名为
MyImageLogBuilder
的类,它实现了IImageLogBuilder
:您还可以重载构造函数,以便传入您想要的
IImageLogBuilder
使用(查找构造函数注入以获取有关此模式的更多信息):记住,您将需要一个实现 IImageLogBuilder 接口的类,以便能够创建一个新实例并将其分配给
_imageLogBuilder
变量。例如:如果您有类似上面定义的类,那么您可以在 LogBinaryWriter 构造函数中执行以下操作,并且您将不再收到空引用错误。
Instantiate an instance of
IImageLogBuilder
in your constructor forLogBinaryWriter
and assign it to_imageLogBuilder
. You would have to do it in the constructor since you have_imageLogBuilder
marked asreadonly
.For example, assuming you have a class called
MyImageLogBuilder
that implementsIImageLogBuilder
:You could also overload the constructor so you can pass in the
IImageLogBuilder
you want to use (lookup constructor injection for more info on this pattern):Remember, you will need a class that implements the
IImageLogBuilder
interface to be able to create a new instance and assign it to the_imageLogBuilder
variable. For example:If you had something like the classes defined above then you could the following in the
LogBinaryWriter
constructor and you would no longer get the null reference error.您需要将实现
IImageLogBuilder
接口的类的实例分配给_imageLogBuilder
字段。现在,您的字段的值将始终为
null
。例如:
You need to assign an instance of a class that implements
IImageLogBuilder
interface to_imageLogBuilder
field.Right now your field will always have a value of
null
.For example:
也许在构造函数中初始化 _imageLogBuilder ?
perhaps initialize
_imageLogBuilder
in constructor ?您从未设置过
_imageLogBuilder
变量的值。由于您将其标记为只读,因此唯一可以设置它的位置是在字段初始值设定项或构造函数中。也许你打算做这样的事情?
You never set the value of the
_imageLogBuilder
variable. And since you marked it asreadonly
, the only place it can be set is in a field initializer, or in a constructor.Did you mean to do something like this, perhaps?