Page 1 of 1

using self/this

PostPosted: Wed Oct 29, 2008 9:32 am
by mindstormss
Is there a way in cobra do something like below: (pyhon code)

Code: Select all
class Test
   def __init__(self):
       doSomething(self)


Where you in the init (or in any method want to send the object) in C# it is this, but that errors on me.

Also, how can I access system dlls from within cobra? the c# code is

Code: Select all
[DllImport("user32.dll"]
public static extern void theFunction(string aVariable, int anotherVariable);
theFunction(aString,anInt);

Re: using self/this

PostPosted: Wed Oct 29, 2008 11:18 am
by Charles
class Test
def init
.doSomething

def doSomething
print 'do something'

Cobra doesn't have the native DLL import feature of C#, so you either have to include C# code in your files, or you have to create a managed DLL wrapper. To include C# code directly, the command line would be something like:
Code: Select all
cobra -c MyProg.cobra SharpLib.cobra SharpLib.cs

Where SharpLib.cs contains your DllImport declarations and SharpLib.cobra informs the Cobra compiler what's in there:
class Util
is extern
shared
def theFunction(aVariable as String, anotherVariable as int)
pass


Another option is to have a little C# project that creates a DLL and then incorporate that DLL in your Cobra project. You can ref it on the command line:
Code: Select all
cobra -ref:MyLib.dll -c MyProg.cobra

Or just "use" it:
use MyLib
# or
use MyLib from 'MyLibFilename'

That last example only works if you're using Cobra out of the Subversion workspace, otherwise you must have both -ref: and "use".

Re: using self/this

PostPosted: Fri Oct 31, 2008 10:33 am
by mindstormss
Thanks for clarification on the second issue, but on the first issue, I am not talking about calling a method of the class, but calling another class/function/method that is outside this class and passing this class instance to it. Basically calling an external function with the argument as this instance. In c# one would use this and in python self, but is there an equivalent in cobra?

Re: using self/this

PostPosted: Fri Oct 31, 2008 12:31 pm
by Charles
Cobra uses "this".

Also, if you had guessed "self" instead, you get a helpful error message:
Code: Select all
foo.cobra(73): error: For "bar" arg 1: Cannot find "self". Maybe you should try "this".

For further syntax and semantic issues see:

http://cobra-language.com/trac/cobra/wiki/C#Diffs

http://cobra-language.com/docs/python/

Feel free to ask further questions here in the forums, though. And to update the wiki as you discover things.