Canalblog
Editer l'article Suivre ce blog Administration + Créer mon blog
Publicité
LINUX & OPEN SOURCE
30 décembre 2008

Différence entre vala et c#: string as indexer

Voici un exemple c# que j'ai pris dans la doc de Micro$oft qui ne marche pas en vala.  Je vais me plonger de nouveau dans la doc de vala pour trouver ce qui ne va pas.

Le message d'erreur généré par valac:

/media/hda3/vala/daycollection.vala:22.16-22.19: error: syntax error, unexpected this /media/hda3/vala/daycollection.vala:29.1-29.1: error: syntax error, unexpected } /media/hda3/vala/daycollection.vala:38.1-38.1: error: syntax error, unexpected }

Le code:

// Using a string as an indexer value
using GLib;
class DayCollection
{
    string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };

    // This method finds the day or returns -1
    private int GetDay(string testDay)
    {
        for(int j = 0; j < days.Length - 1; j++)
        {      
            if (days[j] == testDay)
            {
                return j;
            }
        }

        throw new System.ArgumentOutOfRangeException(testDay, "testDay must be in the form \"Sun\", \"Mon\", etc");
    }

    // The get accessor returns an integer for a given string

    public int this[string day]                  //  <= le pb est ici !!!
    {
        get
        {
            return (GetDay(day));
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        DayCollection week = new DayCollection();
        stdout.printf("%s",week["Fri"]);
    }
}
// Output: 5

Publicité
Publicité
Commentaires
LINUX & OPEN SOURCE
Publicité
Archives
Publicité