locked
C++/CX interface not allowed to have a getType() function? RRS feed

  • Question

  • Hi,

    I'm trying to create and implement an interface in C++/CX that can be consumed by a JavaScript App. One of the issues is that I have a getType() function in my interface. With this function, the App throws an exception as soon as I try to use any class that implements the interface. If I rename it to getType2, it works fine. Also, I created a small dummy class and interface to see if I could reproduce the problem, and sure enough, my app crashed.

    Is this intended? If yes, then could someone explain why this is? I'd like to know that this is not just a bug or error before I change my API.

    Thanks,

    Tomas

    Wednesday, January 9, 2013 11:37 PM

All replies

  • Hi Tomas,

    I guess you create a component with c++. And I also create a component with c#, but I don't get the scenario you said. That is running normal. The code snippet as follow:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace SampleComponent
    {
       
            public sealed class Example
            {
                public static string GetAnswer()
                {
                    return "The answer is 42.";
                }
    
                public static string getType()
                {
                    return "The answer is 45.";
                }
    
                public int SampleProperty { get; set; }
    
                public static IDictionary<int, string> GetMapOfNames()
                {
                    Dictionary<int, string> retval = new Dictionary<int, string>();
                    retval.Add(1, "one");
                    retval.Add(2, "two");
                    retval.Add(3, "three");
                    retval.Add(42, "forty-two");
                    retval.Add(100, "one hundred");
                    return retval;
                }
            }
          
       
       
    }
    


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, January 10, 2013 5:37 AM
  • Yes, my component is in C++/CX.

    The GetType function is not an issue if it's just a class. It needs to be an interface. I'm trying to get a simple sample project up and running. Basically, it should look like this:

    #pragma once
    
    namespace MyComponent
    {
    	public interface class IBase1
    	{
    	public:
    		int GetType();
    
    	};
    
    	public ref class Derived1 sealed : IBase1
    	{
    	public:
    		Derived1();
    		virtual int GetType(); // Should crash the JS App
    	};
    
    	public interface class IBase2
    	{
    	public:
    		int GetType2(); 
    
    	};
    
    	public ref class Derived2 sealed : IBase2
    	{
    	public:
    		Derived2();
    		virtual int GetType2(); // Should be fine
    	};
    
    	public ref class NotDerived sealed
    	{
    	public:
    		NotDerived();
    		int GetType(); // Should be fine
    	};
    
    }

    Straight forward implementations:

    // Class1.cpp
    #include "pch.h"
    #include "Class1.h"
    
    using namespace MyComponent;
    using namespace Platform;
    
    Derived1::Derived1()
    {
    }
    
    int Derived1::GetType()
    {
    	return 5;
    }
    
    Derived2::Derived2()
    {
    }
    
    int Derived2::GetType2()
    {
    	return 5;
    }
    
    NotDerived::NotDerived()
    {
    
    }
    
    int NotDerived::GetType()
    {
    	return 5;
    }

    Now, in JS, try doing this:

    var v1 = MyComponent.Derived2(); // works fine
    
    var v2 = MyComponent.NotDerived(); // works fine
    
    var v3 = MyComponent.Derived1(); // instant crash!

    It's the interface with the GetType that is the problem it seems. Other names seem fine. Also, a class with a GetType is fine as well, as long as it's not from an interface.

    The error message isn't particularly useful either :)

    As you can see, you don't even have to call that function, it just instant crashes as soon as you try to use the class.

    Thanks a bunch!

    Tomas


    • Edited by T Hofmann Thursday, January 10, 2013 5:07 PM
    Thursday, January 10, 2013 5:06 PM
  • Hi,

    I reproduced the scenario on my side again with C#. But I also don't get the result.

    I suggest you don't use GetType, because it is the keyword. You can use other word freely.


    Roy
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, January 11, 2013 8:31 AM
  • Alright, will do.

    Any idea why this is the case? Why is it that you can't use GetType, but only when using an interface?

    Also, may I suggest some form of documentation that states that GetType is a bad word to use (but only in an interface). It strikes me as really random and it was quite time consuming for me to try to figure out what was wrong. The app just crashes when I try to create an object of the type, and there's no helpful error message at all.

    It'd be nice if the next person who runs into this doesn't have to spend hours on it.

    Thanks,

    Tomas


    • Edited by T Hofmann Friday, January 11, 2013 4:43 PM
    Friday, January 11, 2013 4:42 PM