Hi I’m making a positive habit app and I’m trying to reduce repetitive code by maing an abstract class for positive and negative habits.
I then made a method that takes a list and dictionary of type of the abstract class to allow me to use the derived types to further reduce repetitive code.
But for some reason it isn’t working.
Here is the code:
abstract class Activity
{
[SerializeField] string _activity;
[Range(0, 10)]
[SerializeField] int _value = 1;
public string ActivityName => _activity;
public int Value => _value;
protected Activity(string activity, int value)
{
_activity = activity;
_value = value;
}
}
[System.Serializable]
class RewardingActivity: Activity
{
public RewardingActivity(string activity, int value) : base(activity, value)
{
}
}
[System.Serializable]
class PunishmentingActivity: Activity
{
public PunishmentingActivity(string activity, int value) : base(activity, value)
{
}
}
void FillDropDownsAndActivities(List<Activity> activities,
List<string> activityNames, Dictionary<int, Activity> activityDic, TMP_Dropdown[] dropDowns )
{
for (int i = 0; i < activities.Count; i++)
{
activityNames.Add($"{activities[i].ActivityName}" +
$" RP:{activities[i].Value}");
activityDic.Add(i, activities[i]);
}
foreach (var dropDown in dropDowns)
{
dropDown.options.Clear();
dropDown.AddOptions(activityNames);
}
}
Now the problem is with trying to call FillDropDownsAndActivities, when I try to pass either a list or dictionary of one of the derived types I’m getting errors like:
Error CS1503 Argument 1: cannot convert from ‘System.Collections.Generic.List<ActivityScore.RewardingActivity>’ to ‘System.Collections.Generic.List<ActivityScore.Activity>’ Assembly-CSharp, Assembly-CSharp.Player
Can someone please explain why this is happening, and how to/suggestions for resolving it.