为变量赋值

发布于 2024-11-04 05:51:20 字数 601 浏览 0 评论 0原文

我有这个变量,它是业务层中类的接口的实例。 我需要从数据访问层发送消息,到业务层,最后到表示层。我在数据访问中的类“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

内心荒芜 2024-11-11 05:51:20

LogBinaryWriter 的构造函数中实例化 IImageLogBuilder 实例,并将其分配给 _imageLogBuilder。您必须在构造函数中执行此操作,因为您已将 _imageLogBuilder 标记为 readonly

例如,假设您有一个名为 MyImageLogBuilder 的类,它实现了 IImageLogBuilder

public LogBinaryWriter()
{
    _imageLogBuilder = new MyImageLogBuilder();
}

您还可以重载构造函数,以便传入您想要的 IImageLogBuilder使用(查找构造函数注入以获取有关此模式的更多信息):

public LogBinaryWriter(IImageLogBuilder imageLogBuilder)
{
    _imageLogBuilder = imageLogBuilder;
}

记住,您将需要一个实现 IImageLogBuilder 接口的类,以便能够创建一个新实例并将其分配给_imageLogBuilder 变量。例如:

public interface IImageLogBuilder
{
    void Log(string message);
}

//The class below IMPLEMENTS the IImageLogBuilder interface
public class MyImageLogBuilder : IImageLogBuilder
{
   //Implement IImageLogBuilder methods here
   public void Log(string message)
   {
      //Log message
   }
}

如果您有类似上面定义的类,那么您可以在 LogBinaryWriter 构造函数中执行以下操作,并且您将不再收到空引用错误。

public LogBinaryWriter()
{
    _imageLogBuilder = new MyImageLogBuilder();
}

Instantiate an instance of IImageLogBuilder in your constructor for LogBinaryWriter and assign it to _imageLogBuilder. You would have to do it in the constructor since you have _imageLogBuilder marked as readonly.

For example, assuming you have a class called MyImageLogBuilder that implements IImageLogBuilder:

public LogBinaryWriter()
{
    _imageLogBuilder = new MyImageLogBuilder();
}

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):

public LogBinaryWriter(IImageLogBuilder imageLogBuilder)
{
    _imageLogBuilder = imageLogBuilder;
}

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:

public interface IImageLogBuilder
{
    void Log(string message);
}

//The class below IMPLEMENTS the IImageLogBuilder interface
public class MyImageLogBuilder : IImageLogBuilder
{
   //Implement IImageLogBuilder methods here
   public void Log(string message)
   {
      //Log message
   }
}

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.

public LogBinaryWriter()
{
    _imageLogBuilder = new MyImageLogBuilder();
}
望笑 2024-11-11 05:51:20

您需要将实现 IImageLogBuilder 接口的类的实例分配给 _imageLogBuilder 字段。

现在,您的字段的值将始终为 null

例如:

private readonly IImageLogBuilder _imageLogBuilder = new ImageLogBuilder();

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:

private readonly IImageLogBuilder _imageLogBuilder = new ImageLogBuilder();
暮光沉寂 2024-11-11 05:51:20

也许在构造函数中初始化 _imageLogBuilder ?

public class LogBinaryWriter : ILogBinaryWriter
{
   private readonly IImageLogBuilder _imageLogBuilder;

   public LogBinaryWriter(IImageLogBuilder imageLogBuilder)
   {
      _imageLogBuilder = imageLogBuilder;
   }
....


}

perhaps initialize _imageLogBuilder in constructor ?

public class LogBinaryWriter : ILogBinaryWriter
{
   private readonly IImageLogBuilder _imageLogBuilder;

   public LogBinaryWriter(IImageLogBuilder imageLogBuilder)
   {
      _imageLogBuilder = imageLogBuilder;
   }
....


}
破晓 2024-11-11 05:51:20

您从未设置过_imageLogBuilder 变量的值。由于您将其标记为只读,因此唯一可以设置它的位置是在字段初始值设定项或构造函数中。

也许你打算做这样的事情?

private readonly IImageLogBuilder _imageLogBuilder = // get the value from somewhere else, or make a new one

You never set the value of the _imageLogBuilder variable. And since you marked it as readonly, 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?

private readonly IImageLogBuilder _imageLogBuilder = // get the value from somewhere else, or make a new one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文