Search code examples
c#actionscript-3flashcode-translation

I have a piece of server-client code, however this bit is in AS3 and I'm working in C#. Can someone help me translate it?


I've been working on converting an old server from a Flash game into something I can use as a starting point for a new server-client system, however I just hit a stopping point I can't really figure out. Can someone look at this code and possibly translate it into C#?

var dists:Array = [];
for(a=0;a<deltas.length;a++) {
    dists.push({offset:Math.abs(deltas[a]-avg), 
                time:deltas[a], 
                toString:function(){
                     return "offset:" + 
                     this.offset + ", 
                     time: " + this.time }
               })
}

As a note, these numbers are doubles - aside from the int in the for loop. And further clarification, Dists I've translated into a double list array, but that might be wrong. This is what I have so far:

List<Double> Dist = new List<double>();

for (int i = 0; i < Deltas.Count; i++)
{
    Dist.Add(Math.Abs(Deltas[i] - Average));
}

Solution

  • From the information given, this is what I could work out...

    void Main()
    {
        Dictionary<int, double> deltas = new Dictionary<int, double>();
        int avg = 0;
    
        List<Item> dists = new List<Item>();
        for (int a = 0; a < deltas.Count(); a++)
        {
            dists.Add(new Item { Offset = Math.Abs(deltas[a] - avg), Time = deltas[a].ToDateTime()});
        }
    }
    
    public class Item 
    {
        public double Offset { get; set; }
        public DateTime Time { get; set; }
        public string DisplayString { get; set; }
    
        public override string ToString()
        {
            return string.Format("offset: {0}, time: {1}", this.Offset, this.Time);
        }
    }
    
    public static class Extension
    {
        public static DateTime ToDateTime(this double time)
        {
            return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(time);
        }
    }