C# create list within class properties
I created the following within my class
private List<Credi开发者_如何转开发tCardTransaction> _ccTransactions = new List<CreditCardTransaction>();
public List<CreditCardTransaction> ccTransactions
{
get { return _ccTransactions; }
set { _ccTransactions = value; }
}
Within another public function (in the same class) I attempted to add a value to the list using the following code:
_ccTransactions.Add(new CreditCardTransaction(Convert.ToString(items[0]), Convert.ToString(items[1]), Convert.ToDouble(items[2]), DateTime.Parse(items[3])));
However a red wavy line under "_ccTransactions" saying
Error 1 An object reference is required for the non-static field, method, or property 'CreditCardTransactionKeeper.CreditCardTransaction._ccTransactions'
What is the proper way for me to add a new item to the list when I am within a method in the class that defined this list?
You can't access the non-static field (_ccTransactions
) inside a static function- by the error, I assume your other function is static.
You need to either make _ccTransactions
static, make your calling function non-static, or get a reference to an object of that class to access _ccTransactions
from.
精彩评论