user10735129
user10735129

Reputation:

Java HashMap to a C# Dictionary

I was using HashMap in Java, like this:

Map<Player, List<Attribute>> map = new HashMap<>();

map.put(
  player("Lebron James"), 
  attribute("mid height", "mid weidght", "high vertical")
);

It works perfectly, but now I wanted to convert it into C#, using Dictionary:

Dictionary<Player, List<Attribute>> dictionary = new Dictionary<Player, List<Attribute>>();

dictionary.Add(
  player("Kobe Bryant"), // <- Doesn't compile
  attribute("mid height", "mid weidght", "high vertical")
);

Can someone explain me, why C# code doesn't compile ? The message is

A player component that cannot be called cannot be used as a method

Upvotes: 0

Views: 1893

Answers (2)

Kevin Cruijssen
Kevin Cruijssen

Reputation: 9326

FYI: The accepted answer using new is NOT a port of the provided Java answer. The end result may be the same (and I actually recommend using new instances instead of the methods called player and attribute), but it's not a port of the Java answer. I primarily wanted to clarify that in this answer in case someone else comes across this SO question & answer in the future.

Based on the Java code, it seems your program should contain the methods player(String) and attribute(String, String, String), which return a Player-instance and List<Attribute>-instance respectively. So you should create those same methods in the C# .NET program.

Your current Java snippet doesn't work as is, and actually would result in a similar error! Try it online.

The accepted answer using the new to create new instances of the Player and List<Attribute> directly may work and give the same results in both programs, but it's not a direct port of your current Java implementation! Otherwise your Java code would have looked like this instead:

Map<Player, List<Attribute>> map = new HashMap<>();
map.put(
  new Player("Lebron James"), 
  new ArrayList<Attribute>(){{
    add(new Attribute("mid height"));
    add(new Attribute("mid weidght"));
    add(new Attribute("high vertical"));
  }}
);

Try it online.

Assuming your more complete Java code is something like this:

  ...

  Map<Player, List<Attribute>> map = new HashMap<>();
  map.put(
    player("Lebron James"), 
    attribute("mid height", "mid weidght", "high vertical")
  );
}

private Player player(String name){
  return new Player(name);
}

private List<Attribute> attribute(String... strAttributes){
  List<Attribute> resultList = new ArrayList<>();
  for(String strAttr : strAttributes){
    resultList.add(new Attribute(strAttr));
  }
  return resultList;
}

Try it online.

The ported C# .NET code would become this:

  ...

  IDictionary<Player, IList<Attribute>> dictionary = new Dictionary<Player, IList<Attribute>>();
  dictionary.Add(
    player("Lebron James"), 
    attribute("mid height", "mid weidght", "high vertical")
  );
}

private Player player(string name){
  return new Player(name);
}

private IList<Attribute> attribute(params string[] strAttributes){
  IList<Attribute> resultList = new List<Attribute>();
  foreach(string strAttr in strAttributes){
    resultList.Add(new Attribute(strAttr));
  }
  return resultList;
}

Try it online.

Again, I primarily created this answer for other people coming across this question & answer in the future, and to clarify that the accepted answer is not a direct port of the provided Java snippet.

Upvotes: 1

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186688

You, probably, want to create instances - a Player and List<Attribute> with its items.

dictionary.Add(
  new Player("Kobe Bryant"), 
  new List<Attribute>() {
    new Attribute("mid height"), 
    new Attribute("mid weidght"), 
    new Attribute("high vertical"),
  }
);

Upvotes: 1

Related Questions