locked
How to convert Object^ to int32 in Windows Runtime Component?

    Question

  • Hi, 

    I want to write a function in WinRT component and call it in javascript. I use Object^ as parameter so that I need not to write many overloaded functions for different types of parameters. But I cannot convert Object^ to int32 or other types using static_cast.

    demo code:

    //in WinRT component
    void someComponent::DoSomething(EnumType type, Object^ para)
    {
    
        switch(type)
    
        {
            case EnumType::INT32:
            {
                int32 tmp = static_cast<int32>(para);
                //...
            }
            break;
            case EnumType::DOUBLE:
            {        
               double tmp = static_cast<double>(para);
               //...
            }
            break;
            default:
            break;
        }
    }

    call in javascript:

    var compon = new someComponent();
    compon.doSomething(enumType::int32, 100);
    compon.doSomething(enumType::double, 99.99);


    • Edited by Lattimore Monday, September 16, 2013 7:42 AM some word error
    Monday, September 16, 2013 7:40 AM

Answers

  • I find that if I call GetType in WinRT Component, the number "1" passed from js will return "Double". It means that every number passed from javascript to WinRT is a double. Is it right?

    And I use static_cast<double>(para) to convert parameters successfully.

    • Marked as answer by Lattimore Sunday, September 22, 2013 8:31 AM
    Monday, September 16, 2013 7:55 AM

All replies

  • I find that if I call GetType in WinRT Component, the number "1" passed from js will return "Double". It means that every number passed from javascript to WinRT is a double. Is it right?

    And I use static_cast<double>(para) to convert parameters successfully.

    • Marked as answer by Lattimore Sunday, September 22, 2013 8:31 AM
    Monday, September 16, 2013 7:55 AM
  • In all honesty, I would reconsider your design. Trying to convert a type strict interface into something that behave like java script is just attempting to pound square pegs into round holes.
    Tuesday, September 17, 2013 2:31 PM
  • Thanks for your suggest, I will think about it.
    Sunday, September 22, 2013 8:30 AM