anders9ustafsson / sharpen

Sharpen is an Eclipse plugin created by db4o that allows you to convert your Java project into c#
2 stars 3 forks source link

Improve HashMap support #2

Open PerfectCarl opened 12 years ago

PerfectCarl commented 12 years ago

Hello,

I'm evaluating sharpen and I found areas where this usefull could use some enhancements : hashmap. I found some basic use cases with Hashmaps that weren't working. Namely, the method get, put and EntrySet are not defined.

I don't know what the solution should be :

Please find below a sample java program and the produced C# file with the compilation errors noted in comments.

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Map<String, String> cities = new HashMap<String, String>();
        cities.put("France", "Paris");
        cities.put("Germany", "Berlin");
        cities.put("Spain", "Madrid");

        String capital = cities.get("France");
        // One way to iterate
        System.out.println("With iterator");
        Iterator it = cities.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry) it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
        }

        System.out.println("\nWith entrySet");
        // Another one
        for (Map.Entry<String, String> entry : cities.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = "
                    + entry.getValue());
        }

        System.out.println("\nWith keys");
        for (String key : cities.keySet()) {
            System.out.println(key + " = " + cities.get(key));
        }

    }

}

Output :

using System.Collections;
using System.Collections.Generic;
using Sharpen;

public class TestProgram
{
    /// <param name="args"></param>
    public static void TestMain(string[] args)
    {
        IDictionary<string, string> cities = new Dictionary<string, string>();
        // *** ERROR Method Put undefined
        cities.Put("France", "Paris");
        cities.Put("Germany", "Berlin");
        cities.Put("Spain", "Madrid");
        string capital = cities.Get("France");
        // One way to iterate
        System.Console.Out.WriteLine("With iterator");
        // *** ERROR EntrySet undefined
        Iterator it = cities.EntrySet().Iterator();
        while (it.HasNext())
        {
            DictionaryEntry pairs = (DictionaryEntry)it.Next();
            System.Console.Out.WriteLine(pairs.Key + " = " + pairs.Value);
        }
        System.Console.Out.WriteLine("\nWith entrySet");
        // Another one
        // *** ERROR EntrySet undefined
        foreach (KeyValuePair<string, string> entry in cities.EntrySet())
        {
            System.Console.Out.WriteLine("Key = " + entry.Key + ", Value = " + entry.Value);
        }
        System.Console.Out.WriteLine("\nWith keys");
        foreach (string key in cities.Keys)
        {
            // *** ERROR Method Get undefined
            System.Console.Out.WriteLine(key + " = " + cities.Get(key));
        }
    }
}
PerfectCarl commented 12 years ago

Well, the code is already set up in the Extensions classes (Extensions.cs), but the class being internal, the extensions are note registered.

So, all you need is to update the Extensions class visibility to public (and do so with all the related classes), and you're good to go...

Why are most of the classes in the Sharpen project internal by default ?

cureos commented 12 years ago

I cannot say for sure why the Sharpen C# classes are internal by default, since I have "shamelessly" copied them from the NGit project, but my guess is that the original intention was that these classes should be incorporated in the same library as the C# application.