Add a list of <DateTime> values ​​inside the list <T>

It could be some kind of Tricky. I basically have a class that looks like this:

class Timer
{
    public string boss { get; set; }
    public List<DateTime> spawnTimes { get; set; }
    public TimeSpan Runtime { get; set; }
    public BossPriority priority { get; set; }

}

As you can see, I want to add a DateTimes list to my object. So I created a list that looks like this:

List<Timer> bosses = new List<Timer>();

I was hoping I could do something similar to add DateTimes:

bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = {  DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });

Unfortunately, this gives me a reference to an object not set to an instance of the object. error.

Doing this also doesn't matter :(

Timer boss = new Timer();
DateTime t1 = DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture);
DateTime t2 = DateTime.ParseExact("11:30 +0000", "hh:mm zzz", CultureInfo.InvariantCulture);
boss.spawnTimes.AddRange(new List<DateTime> { t1, t2 });

Do I really have .Add () for each DateTime?

+4
source share
6 answers

Your NRE is caused by what you are not initializing Timer.spawnTimes.

, :

public class Timer {

    public List<DateTime> SpawnTimes { get; private set; }
    ...

    public Timer() {
        this.SpawnTimes = new List<DateTime>();
    }

}

- , params:

public class Timer {

    public List<DateTime> SpawnTimes { get; private set; }
    ...

    public Timer() {
        this.SpawnTimes = new List<DateTime>();
    }

    public Timer(String boss, /*String runtime,*/ BossPriority priority, params String[] spawnTimes) : this() {

        this.Boss = boss;
//      this.Runtime = TimeSpan.Parse( runtime );
        this.Priority = priority;

        foreach(String time in spawnTimes) {

            this.SpawnTimes.Add( DateTime.ParseExact( time, "HH:mm" ) );
        }

    }
}

:

bosses.Add( new Timer("Tequat1", BossPriority.HardCore, "07:00 +0000" ) );
bosses.Add( new Timer("Tequat2", BossPriority.Nightmare, "01:00 +0000", "01:30 +0000" ) );
bosses.Add( new Timer("Tequat3", BossPriority.UltraViolence, "12:00 +0000" ) );

: FxCop/StyleCop time!

  • (, ) PascalCase
  • PascalCase ( Java, camelCase)
    • . public BossPriority priority public BossPriority priority
  • (.. private set set ( public)
  • Collection<T> ReadOnlyCollection<T> List<T> T[]
+5

... .

new[], List:

bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore,
    spawnTimes = new[] { DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) }.ToList() });
+2

:

spanTimes = new List<DateTime>{  DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) }

, , List Initializer.

+2
source
bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = new List<DateTime> {  DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });

you will need newyourspawnTimes

+2
source

You must initialize the spawnTimes collection before using it.

boss.spawnTimes = new List<DateTime>();
+1
source

You are close, but you need to include an instance of List. Try

bosses.Add(new Timer { boss = "Tequatl", priority = BossPriority.HardCore, spanTimes = new List<DateTime>{ DateTime.ParseExact("07:00 +0000", "hh:mm zzz", CultureInfo.InvariantCulture) } });
+1
source

Source: https://habr.com/ru/post/1547101/


All Articles