How to return a struct with a List in WRC c# to c++/WinRT

JRH3221 0 Reputation points
2023-07-23T14:10:39.3366667+00:00

I have two structs, one contains a List<struct> which holds the other struct. How can I return this to C++/WinRT?

I know that something like a list would have an alternative for Windows Runtime Component use, I tried to use IVector, but it doesn't work due to it's protection level. None of the types that the error says are allowed for structs can contain multiple pieces of data, so I'm confused as to what is expected. Note, I don't need these to be structs, class or any alternative that would work better is fine.

// i have a method that returns Holders
public struct Holder {
	...
	List<Person> people;
	...
}

public struct Person {
...
some code
...
}
Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,286 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,850 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 71,266 Reputation points
    2023-07-23T16:28:30.6166667+00:00

    You should use Person[] instead of a list.


  2. JRH3221 0 Reputation points
    2023-07-25T09:40:26.21+00:00

    I figured it out.

    Instead of using a struct, use a class, but all the variables have to be private, so you need to use a property rather than a field. An example of what it might look like:

    sealed class Class1 {
    	public Class2 Run() { // call this from c++
    		return new Class2();
    	}
    }
    
    public sealed class Class2 {
    	// vars that you want to access
    	private int iD;
    	private IList<string> strings = new List<string>();
    
    	// the properties for those vars, use a different name, i recommend having the same name with different
    	// starting case, lower for private, upper for public
    	public int ID {
    		get { return ID; }
    		set { iD = value; }
    	}
    	public IList<string> Strings {
    		get { return Strings; }
    		set { strings = value; }
    	}
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.