1

I know there are already tons of question similar to this, but i can't solve my problem.

I wrote this little code in Visual C++

#pragma once

using namespace System;

namespace MyWrapper {
public ref class MySubClass{
        String^ username;
        String^ password;
    };

public enum class MyEnum{
        VALUE1 = 1,
        VALUE2 = 2,
        VALUE3 = 3,
        VALUE4 = 4
    };

public ref class MyClass{
        String^ string1;
        String^ string2;
        String^ string3;
        long exampleNumber;
        MyEnum choosenValue;
        MySubClass credentials;
        unsigned int otherNumber;

    };
}

MyClass is a struct used in a external dll and i wrote this looking at the dll's source code. It compile and i can see it from my C# code, this C# code works:

MyClass instance = new MyClass();

but this doesn't work:

instance.string1 = "test";

it can't find string1 inside MyClass. string1 is just an example, i can't see any of the values i need. What i'm doing wrong?

3
  • 3
    string1 is private, so you can't access it from outside. Commented Oct 20, 2014 at 15:54
  • 1
    I think you probably want to use property - How to: Use Properties in C++/CLI. You can use the get and set to forward to your C++ struct (although you should name them differently) Commented Oct 20, 2014 at 15:54
  • thank you i'm reading it right now! This is the first time i use C++/CLI and i have a lot to learn! Commented Oct 20, 2014 at 15:57

1 Answer 1

4

By default, the members of a class are private. You need to declare them to be public.

public ref class MyClass 
{
public:
    String^ string1;
    String^ string2;
    String^ string3;
    long exampleNumber;
    MyEnum choosenValue;
    MySubClass credentials;
    unsigned int otherNumber;
};

If the structures you are wrapping are pure data containers, then it may be more convenient to use ref struct rather than ref class. By default, members of a ref struct have public accessibility.


Following on from the comments, you now have problems with credentials, which is a ref class in your code. Personally I'd be inclined to use ref struct for both of these structures. And then it may just be simplest to expose the contained struct using properties:

public ref struct MySubClass
{
    String^ username;
};

public ref struct MyClass 
{
private:
    MySubClass credentials;
public:
    property String^ username {
        String^ get()
        {
            return credentials.username;
        }
        void set(String^ value)
        {
            credentials.username = value;
        }
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

wow it works, i feel pretty dumb now. thank you, i will mark this as answer in 5 minutes :)
just another thing: i can't set instance.credentials from C# code, it says its "read only"! I just declared a MySubClass object and assigned to instance.credentials
just did it (both MyClass and MySubClass are now ref struct) but still same error :( When reading MyClass metadata from C# looks like MySubClass is the only one marked as readonly

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.