Forums

using self/this

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

using self/this

Postby mindstormss » Wed Oct 29, 2008 9:32 am

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);
Eat a live toad the first thing in the morning and nothing worse will happen to you the rest of the day.
mindstormss
 
Posts: 8

Re: using self/this

Postby Charles » Wed Oct 29, 2008 11:18 am

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".
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: using self/this

Postby mindstormss » Fri Oct 31, 2008 10:33 am

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?
Eat a live toad the first thing in the morning and nothing worse will happen to you the rest of the day.
mindstormss
 
Posts: 8

Re: using self/this

Postby Charles » Fri Oct 31, 2008 12:31 pm

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.
Charles
 
Posts: 2515
Location: Los Angeles, CA


Return to Discussion

Who is online

Users browsing this forum: No registered users and 17 guests