[Ironruby-core] Calling a C# method with a byref parameter

Shay Friedman shay.friedman at gmail.com
Thu Feb 11 09:52:53 EST 2010


You should call generic methods a bit differently.

The next code works:
ret_val = c.method(:do_something_else).of(String).call(str)

Shay.
--------------------------------------------------------
Shay Friedman | .NET Technologies Expert | Author of IronRuby Unleashed |
Sela Technology Center
Blog: http://IronShay.com | Twitter: http://twitter.com/ironshay


On Thu, Feb 11, 2010 at 4:44 PM, Michael Erasmus
<michaelerasmus at gmail.com>wrote:

> Thanks Shay.
>
> OK your code works. But my trouble seems to come in with the Generic
> method.
>
> When I do:
>
> public class Class1
>     {
>         public string DoSomething(ref string test)
>         {
>             test = test + "tamtamtam";
>
>             return "return value";
>         }
>
>         public string DoSomethingElse<T>(ref T test)
>         {
>             return test.ToString();
>         }
>     }
>
> and then call it from IR:
>
>
> >>> c = TestClass::Class1.new
> => TestClass.Class1
> >>> c.do_something("Bla")
> => ['return value', 'Blatamtamtam']
> >>> c.do_something_else("Bla")
> Microsoft.Scripting.Core:0:in `Bind': Expression of type
> 'IronRuby.Builtins.Muta
> bleString&' cannot be used for parameter of type
> 'IronRuby.Builtins.MutableStrin
> g' of method 'System.String
> DoSomethingElse[MutableString](IronRuby.Builtins.Mut
> ableString ByRef)' (ArgumentError)
>         from Microsoft.Scripting.Core:0:in `BindCore'
>         from :0
>
>
>
>
> On Thu, Feb 11, 2010 at 4:12 PM, Shay Friedman <shay.friedman at gmail.com>wrote:
>
>> It returns another return value with the new ref value.
>>
>> For example, if you have the next c# class:
>> public class Class1
>> {
>>     public string DoSomething(ref string test)
>>     {
>>         test = test + "tamtamtam";
>>
>>         return "return value";
>>     }
>> }
>>
>> This is the IronRuby code you can write to use it:
>> >>> require 'c:\dev\TestApps\TestClass\TestClass\bin\Debug\TestClass.dll'
>> => true
>> >>> c = Class1.new
>> => TestClass.Class1
>> >>> str = "hello"
>> => "hello"
>> >>> result = c.do_something(str)
>> => ['return value', 'hellotamtamtam']
>>
>> Pay attention that the result variable is an array that contains the
>> method return value as the first item and the ref value as the second
>> parameter.
>>
>> Shay.
>> --------------------------------------------------------
>> Shay Friedman | .NET Technologies Expert | Author of IronRuby Unleashed |
>> Sela Technology Center
>> Blog: http://IronShay.com | Twitter: http://twitter.com/ironshay
>>
>>
>> On Thu, Feb 11, 2010 at 3:44 PM, Michael Erasmus <
>> michaelerasmus at gmail.com> wrote:
>>
>>> Hi Everyone,
>>>
>>> I'm trying to write a IronRuby script that interops with a a .NET
>>> assembly written in C#.
>>>
>>> It has a class that derives from a base class in the .NET assembly. One
>>> of the base class protected methods looks like this:
>>>
>>> protected void OnNotifyPropertyChanged<T>(string name, ref T localmember,
>>> T value)
>>>  {
>>>          ......
>>>  }
>>>
>>> I can't for the life of me figure out how to call this method from IR in
>>> my derived class.
>>> The documentation mentions that you can call *out *parameters without
>>> using them as arguments, but I can't seem to find anything about *ref *
>>> params.
>>>
>>> If I just try calling the method like this:
>>>
>>> OnNotifyPropertyChanged(property_name, value, value)
>>>
>>> I get this error:
>>>
>>> *Microsoft.Scripting.Core:0:in `Bind': Expression of type
>>> 'IronRuby.Builtins.Muta*
>>> *bleString&' cannot be used for parameter of type
>>> 'IronRuby.Builtins.MutableStrin*
>>> *g' of method 'Void
>>> #base#OnNotifyPropertyChanged[MutableString](System.String, I*
>>> *ronRuby.Builtins.MutableString ByRef, IronRuby.Builtins.MutableString)'
>>> (Argumen*
>>> *tError)*
>>> *        from Microsoft.Scripting.Core:0:in `BindCore'*
>>> *        from ./dynamic_event_item.rb:22:in `method_missing'*
>>> *        from :0*
>>>
>>>
>>> Am I missing something?
>>>
>>> Thanks
>>>
>>> Michael Erasmus
>>>
>>>
>>> _______________________________________________
>>> Ironruby-core mailing list
>>> Ironruby-core at rubyforge.org
>>> http://rubyforge.org/mailman/listinfo/ironruby-core
>>>
>>>
>>
>> _______________________________________________
>> Ironruby-core mailing list
>> Ironruby-core at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/ironruby-core
>>
>>
>
> _______________________________________________
> Ironruby-core mailing list
> Ironruby-core at rubyforge.org
> http://rubyforge.org/mailman/listinfo/ironruby-core
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100211/3030f2c4/attachment-0001.html>


More information about the Ironruby-core mailing list