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
you can use like this
private dynamic defaultReminder => reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
private dynamic defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
is a field initializer and executes first (before any field without an initializer is set to its default value and before the invoked instance constructor is executed). Instance fields that have no initializer will only have a legal (default) value after all instance field initializers are completed. Due to the initialization order, instance constructors are executed last, which is why the instance is not created yet the moment the initializers are executed. Therefore the compiler cannot allow any instance property (or field) to be referenced before the class instance is fully constructed. This is because any access to an instance variable like reminder
implicitly references the instance (this
) to tell the compiler the concrete memory location of the instance to use.
This is also the reason why this
is not allowed in an instance field initializer.
A variable initializer for an instance field cannot reference the instance being created. Thus, it is a compile-time error to reference this in a variable initializer, as it is a compile-time error for a variable initializer to reference any instance member through a simple_name.
The only type members that are guaranteed to be initialized before instance field initializers are executed are class (static) field initializers and class (static) constructors and class methods. Since static members are instance independent, they can be referenced at any time:
class SomeOtherClass
{
private static Reminders reminder = new Reminders();
// This operation is allowed,
// since the compiler can guarantee that the referenced class member is already initialized
// when this instance field initializer executes
private dynamic defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
}
That's why instance field initializers are only allowed to reference a class member (static member). This compiler initialization rules will ensure a deterministic type instantiation.
For more details I recommend this document: Microsoft Docs: Class declarations.
This means that an instance field that references another instance member to initialize its value, must be initialized from the instance constructor or the referenced member must be declared static
.
This line:
private dynamic defaultReminder =
reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
You cannot use an instance variable to initialize another instance variable. Why? Because the compiler can rearrange these - there is no guarantee that reminder
will be initialized before defaultReminder
, so the above line might throw a NullReferenceException
.
Instead, just use:
private dynamic defaultReminder = TimeSpan.FromMinutes(15);
Alternatively, set up the value in the constructor:
private dynamic defaultReminder;
public Reminders()
{
defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
}
There are more details about this compiler error on MSDN - Compiler Error CS0236.
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.