Ref Class Question

Hello, I have a question regarding the way intellisense works with ref classes. I have a class like so:
public ref class Man abstract sealed
    {
        public:   
            public ref class Size
            {
            public:
                static int Height;
                static int Weight;
            };
            public ref class Name
            {
            public:
                static String^ First_Name;
                static String^ Last_Name;
                static String^ Middle_Name;
            };
            public ref class Colour
            {
            public:
                static Color^ SkinColour;
                static Color^ EyesColour;
            };
    };

Now I created a new instance of Man called m. then I typed m-> but intellisense doesn't see the classes inside it, if I use them in my code it works, though. Also, if I write Man:: I see all of the classes inside. Why is this

With Thanks,
                    Gal Beniamini.


Answer this question

Ref Class Question

  • Kang Su

    Hmm... no problem... but what are you trying to do in fact

    ref class C1

    {

    public:

    ref class NC1

    {

    public:

    static int s1;

    int m1;

    String ^m2;

    };

    NC1 ^mnc1;

    static int s2;

    };

    With this you can write the following:

    C1 ^c = gcnew C1; // create an instance of the C1 class

    c->mnc1 = gcnew C1::NC1; // create an instance of the C1::NC1 class

    c->mnc1->m2 = "2"; //access of the m1 instance field of the C1::NC1 class

    C1::NC1::s1 = 2; // access of the s1 static field of the C1::NC1 class

    C1::s2 = 3; // access of the s2 static field of the C1 class

     


  • Yutong MSFT

    Well, the -> operator is used to access members of the class like fields, methods, properties. Size, Name and Colour are nested classes, not class members and in C++ :: is used to access them. It's not an intellisense thing, this is how C++ language works.
  • chris_m_willis

    Thanks very much, I owe you.

  • JHorn

    Thanks again, Mike. Although, if it isn't too troublesome - could you provide a short exmaple please of a class with other nested classes in it

    With Thanks,
                        Gal Beniamini.

  • wonderliza

    No. You cannot access the neste classes using ->. I suspect what you really want is not to access the neste class but some instances of them:

    public ref class Man abstract sealed {

    ...

    Size ^size;

    Name ^name;

    Colour ^colour;

    }

    Now you can write something like m->size, m->name, m->colour.

    What I don't understand is why you have declared all the members of the nested classes as static. Is this really what you intended

     


  • Ken England

    Hello Mike, thanks for the answer, just that I do want to be able to access the nested classes using ->. Is this possible Oh and by the way, why does 'm->Name::First_Name = "Lag";' work

    With Thanks,
                        Gal Beniamini.

  • Ref Class Question