开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜