So I have a struct that is defined in the following manner:
public struct Item
{
public string _name { get; set; }
public double _weight
{
get
{
return _weight;
}
set
{
_weight = value;
//Shipping cost is 100% dependent on weight. Recalculate it now.
_shippingCost = 3.25m * (decimal)_weight;
//Retail price is partially dependent on shipping cost and thus on weight as well. Make sure retail price stays up to date.
_retailPrice = 1.7m * _wholesalePrice * _shippingCost;
}
}
public decimal _wholesalePrice
{
get
{
return _wholesalePrice;
}
set
{
//Retail price is partially determined by wholesale price. Make sure retail price stays up to date.
_retailPrice = 1.7m * _wholesalePrice * _shippingCost;
}
}
public int _quantity { get; set; }
public decimal _shippingCost { get; private set; }
public decimal _retailPrice { get; private set; }
public Item(string name, double weight, decimal wholesalePrice, int quantity) : this()
{
_name = name;
_weight = weight;
_wholesalePrice = wholesalePrice;
_quantity = quantity;
}
//More stuff
I also have an instance of Item in a different class. When I try to invoke the weight property through the following command, the program crashes:
currentUIItem._weight = formattedWeight;
A descriptive error is not provided. Note that at this point, currentUIItem has been newed with the parameterless default constructor. Now, here is the weird part. When I remove the custom implementation of weight's set property and replace it with a generic { get; set; }, the assignment works flawlessly.
Does anyone know what's going on here? Is this a quirk of structs that would work fine with classes?