3

How can I convert this code from C# to Python to be run on IronPython?

I don't have any experience with Python.

using System;
using Baz;

namespace ConsoleApplication
{
  class Program
  {
    static void Main()
    {
        Portal foo = new Portal("Foo"); 
        Agent bar = new Agent("Bar");

        foo.Connect("127.0.0.1", 1234); 
        foo.Add(bar);

        bar.Ready += new Agent.ReadyHandler(bar_Ready);               
    }

    static void bar_Ready(object sender, string msg)
    {    
       Console.WriteLine(msg.body);  
    }
}
}

4 Answers 4

5

Instantiation doesn't require a type definition. Methods called the same, assign delegates directly. The previous answer is absolutely right, you'll need a lot more context in order to "convert" a C# application to Python; it's more than just syntax.

foo = Portal("Foo")

bar = Agent("bar")

foo.Connect("ip", 1234)

foo.Add(bar)

bar.Ready = bar_Ready

def bar_Ready(sender, msg):

    print msg.body
Sign up to request clarification or add additional context in comments.

1 Comment

This is right, except that the event hook line should be 'bar.Ready += bar_Ready' if you assume that the Portal and Agent classes are still implemented in C#.
2

I think it would suit you best if you take a look at the following links:

http://www.learningpython.com/2006/10/02/ironpython-hello-world-tutorial/ http://msdn.microsoft.com/en-us/magazine/cc300810.aspx

Comments

2

Or if you're feeling really lazy, there's a C# to Python converter on developer fusion!

Comments

-1

In case someone else has this question SharpDevelop has a conversion utility to convert between C# and IronPython, VB.NET or Boo http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/11/ConvertingCSharpVBNetCodeToIronPython.aspx

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.