# Win32ole type casting possible?

**URL:** <https://www.ruby-forum.com/t/win32ole-type-casting-possible/118609>\
**Category:** Ruby\
**Created:** [October 29, 2007, 1:43pm UTC](https://www.ruby-forum.com/t/win32ole-type-casting-possible/118609 "2007-10-29T13:43:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [October 29, 2007, 1:43pm UTC](https://www.ruby-forum.com/t/win32ole-type-casting-possible/118609/1 "2007-10-29T13:43:55Z")

</div>

Hello all,

I have a problem when I try to automate an application using Rubys  
win32ole  
class. I’ll try to explain my problem by some simplified sample code:

```
oleObj = WIN32OLE.new( 'Some.COM.application' )
foo = oleObj.getFoo #foo is either of type FooA or 

```

FooB  
fooType = oleObj.getFooType

getFooType returns either ‘A’ or ‘B’. This determines the actual type of  
foo. In case fooType is ‘A’, foo is of type FooA and has different  
ole-methods than in case that fooType is ‘B’. If I use C# to automate  
the  
COM application, this can easily be solved by a typecast of form:

```
FooA foo = ( FooA )oleObj.getFoo(); //For the case that getFooType

```

returns ‘A’

As far as I know Rubys typing concept makes such a cast impossible. My  
question therefore is: Does the win32ole class support a mechanism that  
allows to convert the type of foo? If not, does anybody see other ways  
of  
solving this problem? I already considered writing a Ruby extension in C  
and  
do the typecast there, but I am not a windows expert, and all the  
IDispatch  
stuff really caused me severe headaches.

Thank you,  
best regards  
Daniel

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [October 31, 2007, 9:28am UTC](https://www.ruby-forum.com/t/win32ole-type-casting-possible/118609/2 "2007-10-31T09:28:03Z")

</div>

2007/10/29, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> … question about type casts of OLE objects …

Daniel, I don’t think you need type casts here. AFAIK you just call  
the methods you want and win32ole should do the rest.

Regards,  
Pit
