1

I'm trying to port some code from Net5.0 to Netstandard2.1, but running out of C# knowledge.

I am trying to rewrite this function

public static readonly Dictionary<Direction, CoordIJK> DirectionToUnitVector =
    Enum.GetValues<Direction>().ToDictionary(e => e, e => e switch {
        Direction.Invalid => CoordIJK.InvalidIJKCoordinate,
        _ => UnitVectors[(int)e]
    });

My attempt so far is:

public static readonly Dictionary<Direction, CoordIJK> DirectionToUnitVector =
    (Direction[])Enum.GetValues(typeof(Direction)).ToString().ToDictionary(e => e, e => e switch {   
        Direction.Invalid => CoordIJK.InvalidIJKCoordinate, 
        _ => UnitVectors[(int)e]
    });

which is giving me

Error   CS0266  Cannot implicitly convert type 'H3.Model.Direction' to 'char'. An explicit conversion exists (are you missing a cast?)  

For reference, Direction is defined as the following Enum

public enum Direction
{
    Center = 0,
    K = 1,
    J = 2,
    JK = 3,
    I = 4,
    IK = 5,
    IJ = 6,
    Invalid = 7
}

and CoordIJK.InvalidIJKCoordinate is defined as

public static readonly CoordIJK InvalidIJKCoordinate = new CoordIJK(-int.MaxValue, -int.MaxValue, -int.MaxValue);

public CoordIJK(int i, int j, int k) {
    I = i;
    J = j;
    K = k;
}

Firstly, everything after the DirectionToUnitVector is pretty much just magic symbols to me. How is the garble of code going to return a Dictionary<Direction, CoordIJK> and what can I do to fix it? I naively tried just adding (char) before Direction.Invalid to fix the error, but that didn't solve the issue.

Any help would be great.

Edit001: Added in the original code.

  • `(Direction[])Enum.GetValues(typeof(Direction)).ToString()` Did you add that `ToString` there, or was it in the original? – mjwills Sep 15 '21 at 23:54
  • I added that in, and the `Direction[]` at the start, cause that's what my googling discovered was a potential fix for another issue. I'll fix the question to have the original code. – infinityplusb Sep 15 '21 at 23:59
  • 1
    Yeah, that `ToString` should clearly not be there. – mjwills Sep 15 '21 at 23:59

1 Answers1

3

The issue is that you can't use switch that way in .Net Standard

try this

using System.Linq;

public static readonly Dictionary<Direction, CoordIJK> DirectionToUnitVector =
    Enum.GetValues(typeof(Direction)).Cast<Direction>().ToDictionary(e => e, e => {
   return e == Direction.Invalid ? CoordIJK.InvalidIJKCoordinate : UnitVectors[(int)e];
});

If you add using System.Linq; and still having issues with missing ToDictionary() try rewriting it like so:

public static readonly Dictionary<Direction, CoordIJK> DirectionToUnitVector =
    System.Linq.Enumerable.ToDictionary(Enum.GetValues(typeof(Direction)).Cast<Direction>(), e => e, e => {
   return e == Direction.Invalid ? CoordIJK.InvalidIJKCoordinate : UnitVectors[(int)e];
});
zaitsman
  • 8,984
  • 6
  • 47
  • 79
  • That fixes *that* error, but another has popped up about the `ToDictionary` ``` Error CS1061 'Array' does not contain a definition for 'ToDictionary' and no accessible extension method 'ToDictionary' accepting a first argument of type 'Array' could be found (are you missing a using directive or an assembly reference?) ``` – infinityplusb Sep 16 '21 at 00:02
  • @infinityplusb You likely removed some of the `using`s at the top of https://github.com/pocketken/H3.net/blob/0264123d4050ee8a671685631f2a363fff9f2520/src/H3/Model/LookupTables.cs from the file. – mjwills Sep 16 '21 at 00:03
  • No, those 4 are still there. I'm trying to change as little as possible, so really just fixing the errors. Is `Array.ToDictionary()` a thing in net5 that isn't in .net standard? – infinityplusb Sep 16 '21 at 00:06
  • Unless there is another using that is implied in net5, that I can include? – infinityplusb Sep 16 '21 at 00:06
  • @infinityplusb there is no `Array.ToDictionary()`, only `Enumerable.ToDictionary()` https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.todictionary?view=net-5.0 – zaitsman Sep 16 '21 at 00:06
  • @zaitsman cool beans. `System.Linq` was already included, but your new version of the function compiled. – infinityplusb Sep 16 '21 at 00:11