locked
static methods

    Question

  • Hi,

    i want to use Csharp WinRT component in c++ blank project template.

    I have some static methods in my winrt component . how to use them is c++ main page ? 

    Tuesday, June 18, 2013 10:37 PM

Answers

  • Can you please explain more clearly what you are doing and what you are having trouble with? Can you provide minimal code to demonstrate what you are attempting to do and an explanation of the behavior you would like in contrast to the actual behavior?

    See Creating Windows Runtime Components for an overview of creating and using Windows Runtime Components. Generally one would write a component in C++ and then call it from a C# app rather than the other way around.

    --Rob

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:01 PM
    Owner
  • You can create a object of class exposed through WRT component and call a public function inside it.

    Something like this - 

    FinancialsWinRT::ClassName^ obj= ref new FinancialsWinRT::ClassName(); 
    obj->CallFunctionInsideClass(600);


    Thanks,
    Sachin
    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:09 PM Typo
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:08 PM
  • There's a glitch (or at least used to be) where you have to clean the C# project before adding the reference to the C++ component.

    See: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/1900bd21-8693-4127-800a-f35cfd5daac2/c-metro-library-cant-be-referenced-in-c-metro-application-


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:18 PM
  • Then try this (assumed public static method)

    FinancialsWinRT::MyClass::StaticMethod();


    Thanks,
    Sachin
    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:23 PM added desc
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:22 PM
  • Try this 

    FinancialsWinRT::Employee::GetEmployeeName();

    Or to be precise

    String^ employeeName = FinancialsWinRT::Employee::GetEmployeeName();


    Thanks,
    Sachin

    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:29 PM Added more code
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:27 PM
  • Hello Neha Verma,

    What is the SharedComponent?

    If this is the part of the namespace then you must have to use it with :: operator.

    Like this.

    FinancialsWinRT::SharedComponent::Employee::GetEmployeeName();

    Meanwhile you can unload the other projects and just focus on your required project.

    you can unload them by right clicking and choosing the option.

    I presume this would be reference(memory) glitch as Reed Copsey suggest you.

    First clean the WinRT component and then add a referece in your CPP project.

    Now using above line of code.

    We are waiting for your error track. put them here so that we could see.


    Vote As Helpful, Propose As Answer, or Mark As Answer
    Microsoft Certified Solutions Developer
    My Samples

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Wednesday, June 19, 2013 12:15 AM
  • Not sure if I understood properly, can you just add right click on Solution and add back those projects using Add Existing Project? Are they WinRT component projects or of what type?


    Thanks,
    Sachin
    My Samples

    • Proposed as answer by Mehmood Ahmed Wednesday, June 19, 2013 4:23 AM
    • Marked as answer by neha-verma Wednesday, June 19, 2013 10:52 PM
    Wednesday, June 19, 2013 1:58 AM

All replies

  • Hi,

    I am developing an app using blank project template.

    I have to use the Windows Runtime Component in c++.

    The component is created in C sharp.

    I  have added the reference of this component.

    Now how to use a namespace.

    I have read some articles but it 'll better if someone could write here.

    WinRT component name : FinancialsWinRT.

    You can presume that i have to use it in Main page.

    Tuesday, June 18, 2013 10:19 PM
  • Can you please explain more clearly what you are doing and what you are having trouble with? Can you provide minimal code to demonstrate what you are attempting to do and an explanation of the behavior you would like in contrast to the actual behavior?

    See Creating Windows Runtime Components for an overview of creating and using Windows Runtime Components. Generally one would write a component in C++ and then call it from a C# app rather than the other way around.

    --Rob

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:01 PM
    Owner
  • You can create a object of class exposed through WRT component and call a public function inside it.

    Something like this - 

    FinancialsWinRT::ClassName^ obj= ref new FinancialsWinRT::ClassName(); 
    obj->CallFunctionInsideClass(600);


    Thanks,
    Sachin
    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:09 PM Typo
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:08 PM
  • Ron,

     I created a WinRT component in C#.

    Now want to utilise it in C++ Blank Store app project.

    See the code .

    =============================FinancialsWinRT Component====
    
    namespace FinancialsWinRT
    {
        public sealed class Employee
        {
            public static string GetEmployeeName()
            {
                return "John";
            }
    
        }
    }
    

    Now want to use it in C++ project.

    void RJ_CPP::MainPage::Button1_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    		try
    			{
    														TextBox1->Text = FinancialsWinRT:: Employee::GetEmployeeName();
    				}
    			}
    			catch (...)
    			{
    			}
    }

    Something wrong in this . Correct me

    Tuesday, June 18, 2013 11:15 PM
  • this is the static method
    Tuesday, June 18, 2013 11:17 PM
  • There's a glitch (or at least used to be) where you have to clean the C# project before adding the reference to the C++ component.

    See: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/1900bd21-8693-4127-800a-f35cfd5daac2/c-metro-library-cant-be-referenced-in-c-metro-application-


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:18 PM
  • Then try this (assumed public static method)

    FinancialsWinRT::MyClass::StaticMethod();


    Thanks,
    Sachin
    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:23 PM added desc
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:22 PM
  • Try this 

    FinancialsWinRT::Employee::GetEmployeeName();

    Or to be precise

    String^ employeeName = FinancialsWinRT::Employee::GetEmployeeName();


    Thanks,
    Sachin

    My Samples


    • Edited by Sachin S Tuesday, June 18, 2013 11:29 PM Added more code
    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Tuesday, June 18, 2013 11:27 PM
  • Thanks All but unfortunately didn't work.

    and now after clean the winrt component and adding the reference in c++.

    I am getting many errors in my other projects.

    this could be actual calling source.

    TextBox1->Text =  FinancialsWinRT.SharedComponent::Employee::GetEmployeeName();

    • Edited by neha-verma Tuesday, June 18, 2013 11:52 PM Complete
    Tuesday, June 18, 2013 11:46 PM
  • What exception you are getting? 

    Thanks,
    Sachin
    My Samples

    Tuesday, June 18, 2013 11:50 PM
  • Hello Neha Verma,

    What is the SharedComponent?

    If this is the part of the namespace then you must have to use it with :: operator.

    Like this.

    FinancialsWinRT::SharedComponent::Employee::GetEmployeeName();

    Meanwhile you can unload the other projects and just focus on your required project.

    you can unload them by right clicking and choosing the option.

    I presume this would be reference(memory) glitch as Reed Copsey suggest you.

    First clean the WinRT component and then add a referece in your CPP project.

    Now using above line of code.

    We are waiting for your error track. put them here so that we could see.


    Vote As Helpful, Propose As Answer, or Mark As Answer
    Microsoft Certified Solutions Developer
    My Samples

    • Marked as answer by neha-verma Wednesday, June 19, 2013 12:46 AM
    Wednesday, June 19, 2013 12:15 AM
  • Thanks for everyone. Much appreciated for your responses.

    Ok, i try and 'll let you know about this.

    Initially i got different error as Insufficient access like these and now they gone after cleaning the winrt component.

    Now i am getting this.

    Error      102         error C2653: 'FinancialsWinRT' : is not a class or namespace name              RJ            c:\users\RJ\desktop\ RJ\mainpage.xaml.cpp  52           1

    Error      103         error C3861: 'GetEmployeeName': identifier not found  RJ            c:\users\RJ\desktop\ RJ\mainpage.xaml.cpp  52           1

    Error      104         error C2653: 'Employee' : is not a class or namespace name          RJ                            c:\users\RJ\desktop\ RJ\mainpage.xaml.cpp  70           1

    106         IntelliSense: name followed by '::' must be a class or namespace name  RJ   c:\users\RJ\desktop\ RJ\mainpage.xaml.cpp  52           24

    Wednesday, June 19, 2013 12:30 AM
  • I unloaded other projects except of c++ blank project.

    Again cleaned the WinRT component.

    Added a  reference in c++ project and wrote with scope resolution operator.

    Build succeeded and now run too.

    Thanks Sachin, Rob, Reed Copsey and Mehmood.

    I am really much grateful to all of you that it worked.

    How to include those projects again sachin?

    Wednesday, June 19, 2013 12:45 AM
  • Not sure if I understood properly, can you just add right click on Solution and add back those projects using Add Existing Project? Are they WinRT component projects or of what type?


    Thanks,
    Sachin
    My Samples

    • Proposed as answer by Mehmood Ahmed Wednesday, June 19, 2013 4:23 AM
    • Marked as answer by neha-verma Wednesday, June 19, 2013 10:52 PM
    Wednesday, June 19, 2013 1:58 AM
  • thanks sachin
    Wednesday, June 19, 2013 10:52 PM