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
webDBvariable for non-null values at a time when it is always null:Change this to:
I’m assuming that you always need a database instance, and that your
if (webDB != null)was a mistake.