0

I wrote this simple example in c# :

 using System;
 using System.Collections.Generic;
 using System.Linq;
  using System.Text;

  namespace DLLTest
  {
        public class MyDllTest
          {
                 public  int sumFunc(int a, int b)
                    {

        int sum = a + b;
        return sum;

    }

    public static string stringFunc(string a, int numT)
    {
           if (numT < 0)
            {
               string errStr = "Error! num < 0";
               return errStr;
            }
              else
             {
                 return a;
             }

         }
    }
}

As you can see - in the first function i'm not using "static" . When I run in in Iron python using this code :

import sys
 import clr
clr.addReferenceToFileAndPath(...path do dll...)

from DLLTest import *
 res = MyDllTest.sumFunc(....HERE MY PROBLEM IS...)

when I pass 2 args - I get this error :

>>> res = MyDllTest.sumFunc(4,5)

Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: sumFunc() takes exactly 3 arguments (2 given)

As I understand it asks for the fisrt argument to be from type "MyDllTest" but when trying to write : a = new MyDllTest I get an error.

what should I do? Any help would be very appreciated!

1 Answer 1

2

sumFunc is an instance method, so you first need to create an instance of the class to be able to call the method.

import clr
clr.addReferenceToFileAndPath(...path do dll...)

from DLLTest import MyDllTest

test = MyDllTest()
test.sumFunc(33, 44)

C# non-static methods could be called only on the instance of the class and static methods can be called on the class itself.

Static and instance methods

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

2 Comments

got it! I was sure I need to use "new MyDllTest" but now I see that "new" is not needed here... thanks a lot!!!
Yes, new doesn't exist in Python, just use the classes as normal Python classes.

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.