# How to instanciate a System::String\[\] from IronRuby?

**URL:** <https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564>\
**Category:** IronRuby\
**Created:** [April 2, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564 "2009-04-02T17:00:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 2, 2009, 5:00pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/1 "2009-04-02T17:00:29Z")

</div>

Hi,

while trying to call some C# code from IronRuby, I get this:

my\_csharp\_class\_instance.call(%w(some array))

:0: can’t convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a  
conversion ?

cheers,

– Thibaut

---

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 2, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/2 "2009-04-02T17:28:37Z")

</div>

I don’t know if it’s the easiest way, but this should work as a  
conversion:

x = [‘a’, ‘b’, ‘c’]  
System::Array[System::String].new(x.map { |s| s.to\_s.to\_clr\_string })

(Obviously, if you know that the elements are already Ruby strings, you  
can omit the “to\_s”.)

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Thibaut  
BarrÃ¨re  
Sent: Thursday, April 02, 2009 7:36 AM  
To: ironruby-core  
Subject: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my\_csharp\_class\_instance.call(%w(some array))

:0: can’t convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a  
conversion ?

cheers,

– Thibaut

---

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 2, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/3 "2009-04-02T17:28:37Z")

</div>

class Array  
def to\_clr\_arr  
arr = System::Array.CreateInstance self[0].class.to\_clr\_type,  
System::Int32.pase(self.length.to\_s)  
self.each\_with\_index { |r, i| arr[i] = r }  
return arr  
end  
end

my\_csharp\_class\_instance.call(%w(some array).map{|x|  
x.to\_clr\_string}.to\_clr\_arr)

2009/4/2 Thibaut BarrÃ¨re [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 2, 2009, 5:43pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/4 "2009-04-02T17:43:16Z")

</div>

Jimmy and I have discussed a to\_clr\_array(type) where type defaults to  
Object, but nothing like that exists yet.

JD

* * *

From: removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid] on behalf of Curt H.  
[removed\_email\_address@domain.invalid]  
Sent: Thursday, April 02, 2009 8:03 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?

I donâ€™t know if itâ€™s the easiest way, but this should work as a  
conversion:

x = [‘a’, ‘b’, ‘c’]  
System::Array[System::String].new(x.map { |s| s.to\_s.to\_clr\_string })

(Obviously, if you know that the elements are already Ruby strings, you  
can omit the â€œto\_sâ€.)

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Thibaut  
BarrÃ¨re  
Sent: Thursday, April 02, 2009 7:36 AM  
To: ironruby-core  
Subject: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my\_csharp\_class\_instance.call(%w(some array))

:0: can’t convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a  
conversion ?

cheers,

– Thibaut

---

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 2, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/5 "2009-04-02T20:40:26Z")

</div>

This is the right way:

x = [‘a’, ‘b’, ‘c’]  
System::Array[System::String].new(x.map { |s| s.to\_s.to\_clr\_string })

This would simplify to

x = [‘a’, ‘b’, ‘c’]  
System::Array[System::String].new(x)

As soon as we implement implicit conversions here.

Tomas

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Jim D.  
Sent: Thursday, April 02, 2009 8:41 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?

Jimmy and I have discussed a to\_clr\_array(type) where type defaults to  
Object, but nothing like that exists yet.

JD

* * *

From: removed\_email\_address@domain.invalid  
[removed\_email\_address@domain.invalid] on behalf of Curt H.  
[removed\_email\_address@domain.invalid]  
Sent: Thursday, April 02, 2009 8:03 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?  
I don’t know if it’s the easiest way, but this should work as a  
conversion:

x = [‘a’, ‘b’, ‘c’]  
System::Array[System::String].new(x.map { |s| s.to\_s.to\_clr\_string })

(Obviously, if you know that the elements are already Ruby strings, you  
can omit the “to\_s”.)

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Thibaut  
BarrÃ¨re  
Sent: Thursday, April 02, 2009 7:36 AM  
To: ironruby-core  
Subject: [Ironruby-core] How to instanciate a System::String[] from  
IronRuby?

Hi,

while trying to call some C# code from IronRuby, I get this:

my\_csharp\_class\_instance.call(%w(some array))

:0: can’t convert Array into System::String[] (TypeError)

Is there some easy way to construct an array of System::String ? Or a  
conversion ?

cheers,

– Thibaut

---

<div class="post-metadata">

**Author:** ![thbar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/eb8c5e/32.png) [@thbar](https://www.ruby-forum.com/u/thbar)\
**Post date:** [April 3, 2009, 1:43am UTC](https://www.ruby-forum.com/t/how-to-instanciate-a-system-string-from-ironruby/164564/6 "2009-04-03T01:43:21Z")

</div>

Hi,  
thanks for the feedback - I’ll give these a go.

cheers

– Thibaut
