A field initializer cannot reference the nonstatic field, method, or property

前端 未结 4 957
小鲜肉
小鲜肉 2020-11-22 02:31

I have a class and when I try to use it in another class I receive the error below.

using System;
using System.Collections.Generic;
using System.Linq;

name         


        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 03:25

    You need to put that code into the constructor of your class:

    private Reminders reminder = new Reminders();
    private dynamic defaultReminder;
    
    public YourClass()
    {
        defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
    }
    

    The reason is that you can't use one instance variable to initialize another one using a field initializer.

提交回复
热议问题