为什么它永远是一个空值? [英] Why will it always be a null value?
问题描述
好了,我得到了下面的类,但产品总是空的时候我 GetIstance();
Visual Studio中显示:
字段'PageStyle.item永远不会分配到,并且总是有它的默认值null
我该如何解决?我究竟做错了什么?是否有这样做的更好的方法什么下文做了什么?
公共类PageStyle
{
私有静态PageStyle _instance = NULL;
//实例化与Sitecore的项目的路径变量。
数据库webDB;
Sitecore.Data.Items.Item项目;
//构造
私人PageStyle()
{
如果(webDB!= NULL)
{
webDB = Sitecore.Configuration.Factory.GetDatabase(网);
Sitecore.Data.Items.Item项目= webDB.Items [起始物品]
}
}
//方法,获取实例
公共静态PageStyle的GetInstance()
{
如果(_instance == NULL)
_instance =新PageStyle();
返回_instance;
}
私人无效InitializeWebDB()
{
如果(webDB == NULL)
{
webDB = Sitecore.Configuration.Factory.GetDatabase(网);
}
}
私人无效InitializeStartItem()
{
如果(webDB!= NULL)
{
项目= webDB.Items [起始物品]
}
}
公共字符串起始物品
{
得到
{
返回_startItem;
}
组
{
_startItem =价值;
}
}
}
您从来没有将其设置为一个值。你可能认为你做,但你真的永远只设置具有相同名称的局部变量。此外,你检查 webDB
变量同时非空值时,它总是空:
//构造
私人PageStyle()
{
如果(webDB!= NULL)
{
webDB = Sitecore.Configuration.Factory.GetDatabase(网);
Sitecore.Data.Items.Item项目= webDB.Items [起始物品]
}
}
更改为:
//构造
私人PageStyle()
{
this.webDB = Sitecore.Configuration.Factory.GetDatabase(网);
this.item = webDB.Items [起始物品]
}
我假定你总是需要一个数据库实例,并且您的如果(webDB!= NULL)
是一个错误。
Okay I got the following class, but item is always null when I GetIstance();
Visual studio shows:
Field 'PageStyle.item' is never assigned to, and will always have its default value null
How can I resolve this? What am I doing wrong? Is there a better way of doing whats done below?
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;
}
}
}
You never set it to a value. You might think that you do, but you really only ever set a local variable with the same name. Also, you're checking the webDB
variable for non-null values at a time when it is always null:
// constructor
private PageStyle()
{
if (webDB != null)
{
webDB = Sitecore.Configuration.Factory.GetDatabase("web");
Sitecore.Data.Items.Item item = webDB.Items[StartItem];
}
}
Change this to:
// constructor
private PageStyle()
{
this.webDB = Sitecore.Configuration.Factory.GetDatabase("web");
this.item = webDB.Items[StartItem];
}
I'm assuming that you always need a database instance, and that your if (webDB != null)
was a mistake.
这篇关于为什么它永远是一个空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!