Reputation: 253
In my Item class I have these two variables.
public string ItemDescription
{
get { return itemDescription; }
set { value = itemDescription; }
}
public string itemDescription;
I know its redundant to have two of the same variables but I'm using them for debugging purposes right now. Each Item inherited has their constructor like this.
public Log()
{
//other stuff
itemDescription = "A log.";
ItemDescription = itemDescription;
}
I display the ItemDescription like so.
if (item.ItemSlotRectangle.Contains(MousePosition) && item.ItemName != "empty")
{
spriteBatch.DrawString(font, item.ItemDescription, new Vector2(item.ItemSlotPosition.X, item.ItemSlotPosition.Y - 20), Color.Black);
}
This works some of the time. When I create an item and set it on the ground I am able to view the description. But when I create a new item and try to view the description, I get a null error at the item.ItemDescription variable in the draw method.
I create a new item like this.
if (item.ItemSlotRectangle.Contains(mp) && item.ItemName != "empty")
{
Item newItem = new Item();
newItem.ItemName = item.ItemName;
newItem.Texture = item.Texture;
newItem.ItemDescription = item.ItemDescription;
return newItem;
}
My question is how come this is returning null for the ItemDescription, while the texture and name work? I guess I don't fully understand how creating new instances of a class works.
Edit: Im gonna try to add more code for clarity and describe what I'm trying to do. Each time I create a new Item, I am looping through and list and calling a method which returns the output of that item in the list.
if (recipe.CheckForItemsNeeded(player) != null)
{
Output = recipe.Output;
SetItemPositionInTable(Output);
}
Output is of type Item. The SetItemPositionInTable method is supposed to draw the item so I can click on it and craft it. The method is as follows.
for (int i = 0; i < CraftingTableItems.Count(); i++)
{
if (CraftingTableItems[i].ItemName == "empty")
{
CraftingTableItems[i].ItemName = output.ItemName;
CraftingTableItems[i].Texture = output.Texture;
break;
}
else if (CraftingTableItems[i].ItemName == output.ItemName)
{
break;
}
//fix this
else if (CraftingTableItems[i].ItemName != "empty" && output.ItemName == "empty")
{
CraftingTableItems[i].ItemName = output.ItemName;
break;
}
}
This method is a mess right now, but that is for another question. And while typing this out I realize I forgot a line in the if loop to set the description... Problem solved :P
Upvotes: 1
Views: 946
Reputation: 20842
set { value = itemDescription; } // should be itemDescription = value
This is backwards, (assigning to value in the setter), it should be the other way. value is the argument, not the backing field. So your setting is doing nothing, and the only way you ever get a value in for ItemDescription property is by the constructor that sets (lowercase) itemDescription = "A log.";
public string ItemDescription
{
get { return itemDescription; }
set { itemDescription = value; } // correct
}
Behind the scenes, set {...}
is rewritten by the compiler as a method like this:
void set_ItemDescription(string value) { ... }
so your original code was assigning to the argument, not the backing field.
Also, since itemDescription is the backing field for the property, your constructor has a redundant assignment (ItemDescription = itemDescription)
public Log()
{
//other stuff
itemDescription = "A log.";
ItemDescription = itemDescription; // <--- redundant
}
One more suggestion; unless the code you show is a factory method, you should write a "copy" constructor, or implement a Clone() or Copy() method rather than explicitly copy object properties from external code. Your class should know how to copy or instantiate fully initialized instances and you should only write that code within the class, or in a factory. Unless you are deserializing, your constructor or factory should produce a fully initialized object, even when copying another object.
Upvotes: 3
Reputation: 822
` public string ItemDescription {
get { return itemDescription; }
set { value = itemDescription; }
} `
You have the wrong code in set section,value should be assigned to itemDescription.
Upvotes: 0
Reputation: 101
Members of a class that are of value types will never be null. Nullable values and class members on the other hand, will be null until explicitly initialized. String is nullable, so your ItemDescription is null when it is not initialized. You can compare null strings such as the name, but when trying to draw a null string, you will get an exception.
Here is a link on value types vs reference types: http://msdn.microsoft.com/en-us/library/t63sy5hs.aspx
Hope that helps.
Upvotes: 0