Visual C++ .NET/Class/Property

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Array Property

 
#include "stdafx.h"
using namespace System;
ref class MyClass
{
public:
    MyClass(int size)
    {
        numArray = gcnew array<int>(size);
    }
    property array<int>^ NumArray
    {
        array<int>^ get() 
        {
            return numArray;
        }
        void set ( array<int>^ value )
        {
            numArray = value;
        }
    }
private:
    array<int>^ numArray;
};
void main() 
{
    MyClass aprop(5);
    for ( int i = 0 ; i < aprop.NumArray->Length ; ++i )
        aprop.NumArray[i] = i;
    for each (int i in aprop.NumArray)
        Console::WriteLine(i);
}


Declaring properties

 
#include "stdafx.h"
using namespace System;
value class MyClass{
   public:
     property unsigned int MyInt;
     property double MyDouble;
     property String^ Name;
     property String^ Symbol;
};
int main()
{
   MyClass myobject;
   myobject.MyInt = 8;
   myobject.MyDouble = 15.9994;
   myobject.Name = "AA";
   myobject.Symbol = "O";
   Console::WriteLine("Element: {0} Symbol: {1}", myobject.Name, myobject.Symbol);
   Console::WriteLine("Number: {0} Weight: {1}",myobject.MyInt, myobject.MyDouble);
}


Defining Index Properties

 
#include "stdafx.h"
using namespace System;
ref class Numbers 
{
public:
    Numbers()
    {
        defaultArray = gcnew array<String^> 
        { 
            "zero", "one", "two", "three", "four", "five" 
        };
    }
    property String^ default [int]
    {
        String^ get(int index)
        {
            if (index < 0)
                index = 0;
            else if (index > defaultArray->Length)
                index = defaultArray->Length - 1;
            return defaultArray[index];
        }
    }
private:
     array<String^>^ defaultArray;
};
void main() 
{
    Numbers numbers;
    Console::WriteLine(numbers[-1]);
    Console::WriteLine(numbers[3]);
    Console::WriteLine(numbers[10]);
}


Indexed properties

 
#include "stdafx.h"
using namespace System;
ref class Numbers
{
   array<String^>^ ordinals;
   public:
      Numbers()
      {
          ordinals = gcnew array<String^> { "zero", "one", "two", "three" };
      }
      property String^ ordinal[unsigned int]
      {
         String^ get(unsigned int index)
         {
             return ordinals[index];
         }
         void set(unsigned int index, String^ value)
         {
             ordinals[index] = value;
         }
      }
};
int main()
{
   Numbers^ nums = gcnew Numbers();
   Console::WriteLine( nums->ordinal[0] );
}


Indexed properties 2

 
#include "stdafx.h"
using namespace System;
ref class Numbers
{
   array<String^>^ ordinals;
   public:
      Numbers()
      {
          ordinals = gcnew array<String^> { "zero", "one", "two", "three" };
      }
      property String^ default[unsigned int]
      {
         String^ get(unsigned int index)
         {
             return ordinals[index];
         }
         void set(unsigned int index, String^ value)
         {
             ordinals[index] = value;
         }
      }
};
int main()
{
   Numbers nums;
   Console::WriteLine( nums[0] );
   Numbers^ nums2 = gcnew Numbers();
   Console::WriteLine( nums2[1] );
   Console::WriteLine( nums.default[2] );
   Console::WriteLine( nums2->default[3] );
}


Property accessor delegate

 
#include "stdafx.h"
using namespace System;
delegate double ValueAccessor();
value class MyClass
{
   public:
     property double MyDouble;
};
int main()
{
  MyClass myobject;
  myobject.MyDouble = 15.9994;
  ValueAccessor^ get_method = gcnew ValueAccessor(myobject,&MyClass::MyDouble::get);
  Console::WriteLine("{0}", get_method->Invoke());
}


Public properties can be used as named parameters

 
#include "stdafx.h"
using namespace System;
[AttributeUsageAttribute(AttributeTargets::Assembly | AttributeTargets::Class)]
public ref class OwnerAttribute : Attribute
{
   public:
      property String^ DevOwner;
      property String^ TestOwner;
      OwnerAttribute() { }
};
[ Owner(DevOwner="John") ]
ref class C2
{
};

int main()
{
    Attribute^ attribute = Attribute::GetCustomAttribute( C2::typeid,OwnerAttribute::typeid);
    if (attribute != nullptr)
    {
         Console::WriteLine("{0}", attribute);
    }
}


Scalar number Property

 
#include "stdafx.h"
using namespace System;
ref class MyClass{
public:
    MyClass(){
        Cost        = 0.0;
        number      = 0;
        name        = "name";
        description = "Property";
    }
    property double Cost;
    property int Number
    {
        void set(int value)
        {
            if (value < 1)
                value = 1;
            else if (value > 10)
                value = 10;
            
            number = value;
        }
        int get()
        {
            return number;
        }
    }
    property String^ Name
    {
        void set(String^ value)
        {
            name = value;
        }
    }
    property String ^Description
    {
        String^ get()
        {
            return String::Concat(name, " ", description);
        }
    }
private:
    String ^name;
    String ^description;
    int     number;
};

void main()
{
    MyClass sp;
    sp.Name = "new name";
    Console::WriteLine(sp.Description);
    sp.Cost = 123.45;
    Console::WriteLine(sp.Cost);
    sp.Number = 20;    
    Console::WriteLine(sp.Number);
    sp.Number = -5;    
    Console::WriteLine(sp.Number);
    sp.Number = 6;    
    Console::WriteLine(sp.Number);
}


Static property

 
#include "stdafx.h"
value class MyClass{
   public:
      static property array<MyClass>^ pp;
      static MyClass()
      {
          pp = gcnew array<MyClass>(120);
      }
};


Virtual properties

 
#include "stdafx.h"
using namespace System;
ref class Base
{
   public:
   virtual property int Prop;
};
ref class Derived : Base
{
   int prop;
   public:
   virtual property int Prop
   {
      int get() override { return prop; }
      void set(int value) override { prop = value; }
   }
};


Write only property

 
#include "stdafx.h"
using namespace System;
ref class MyClass
{
    unsigned int atomic_number;
    public:
       property unsigned int MyValueNumber;
       property unsigned int MyInt
       {
            public: unsigned int get()
            {
                return atomic_number;
            }
            protected: void set(unsigned int n)
            {
                atomic_number = n;
            }
       }
};
ref class RadioactiveMyClass : MyClass
{
    public:
      void AlphaDecay()
      {
          MyInt -= 2;
          MyValueNumber -= 4;
      }
};