【问题标题】:Why will it always be a null value?为什么它总是一个空值?
【发布时间】:2012-01-18 09:05:41
【问题描述】:

好的,我得到了以下课程,但是当我 GetIstance(); Visual Studio 显示时,项目始终为空:

Field 'PageStyle.item' is never assigned to, and will always have its default value null

我该如何解决这个问题?我究竟做错了什么?有没有更好的方法来做下面的事情?

public class PageStyle
{

private static PageStyle _Instance = null;

// Instantiate variables relating to sitecore item paths.
Database webDB;
Sitecore.Data.Items.Item item;

// constructor
private PageStyle()
{
    if (webDB != null)
    {
        webDB = Sitecore.Configuration.Factory.GetDatabase("web");
        Sitecore.Data.Items.Item item = webDB.Items[StartItem];
    }
}

// Method that gets instance
public static PageStyle GetInstance()   
{       
    if (_Instance == null)          
    _Instance = new PageStyle();        
    return _Instance;   
}

private void InitializeWebDB()
{
   if (webDB == null)
   {
    webDB = Sitecore.Configuration.Factory.GetDatabase("web");
   }

}

private void InitializeStartItem()
{
    if (webDB != null)
    {
        item = webDB.Items[StartItem];
    }
}

public string StartItem
{
    get
    {
        return _startItem;
    }

    set
    {
        _startItem = value;
    }
}
}

【问题讨论】:

    标签: c# asp.net .net visual-studio-2010


    【解决方案1】:

    您从未将其设置为值。您可能认为确实如此,但实际上您只设置了同名的局部变量。此外,您在 webDB 变量始终为空时检查非空值:

    // constructor
    private PageStyle()
    {
        if (webDB != null)
        {
            webDB = Sitecore.Configuration.Factory.GetDatabase("web");
            Sitecore.Data.Items.Item item = webDB.Items[StartItem];
        }
    }
    

    将其更改为:

    // constructor
    private PageStyle()
    {
        this.webDB = Sitecore.Configuration.Factory.GetDatabase("web");
        this.item = webDB.Items[StartItem];
    }
    

    我假设您总是需要一个数据库实例,而您的 if (webDB != null) 是一个错误。

    【讨论】:

    • 很好,基本上这样做我可以摆脱 Initialize WebDB() 方法。如果有效,干杯伙伴将标记为答案。
    【解决方案2】:

    也许您打算在构造函数中分配item

    private PageStyle()
    {
        if (webDB != null)
        {
            webDB = Sitecore.Configuration.Factory.GetDatabase("web");
            this.item = webDB.Items[StartItem];
        }
    }
    

    还要确保在某处调用 InitializeWebDB 私有方法,否则 webDB 变量也将为空。

    像这样:

    private PageStyle()
    {
        InitializeWebDB();
    
        if (webDB != null)
        {
            this.item = webDB.Items[StartItem];
        }
    }
    

    【讨论】:

      【解决方案3】:

      您在构造函数中有一个 item 的本地实例,隐藏了类级别的字段。

      【讨论】:

        【解决方案4】:

        你没有在构造函数中初始化它,你创建了一个新的同名局部变量,使用:

        // constructor
        private PageStyle()
        {
            webDB = Sitecore.Configuration.Factory.GetDatabase("web");
            this.item = webDB.Items[StartItem];
        }
        

        【讨论】:

        • 那仍然不会设置任何值。
        • 是的,删除了 if 语句,尽管其他人现在已经提供了更好的答案:-S
        【解决方案5】:

        也许将 webDB 的 init 方法放在类之外并使其成为静态不会有什么坏处。在您的代码中,您在 GetDatabase() 方法中检查 webDB 是否为 null 两次。

        你可以这样编码这部分:

        public class PageStyle
        {
            private static PageStyle _Instance;
        
            // Instantiate variables relating to sitecore item paths.
            private static readonly Database webDB = DbInitializer.InitializeWebDb("web");
            ...
        

        【讨论】:

          猜你喜欢
          • 2019-12-05
          • 2021-07-03
          • 1970-01-01
          • 2017-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多