Visual C++ .NET/Class/Property

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

Array Property

<source lang="csharp">

  1. 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);

}

 </source>


Declaring properties

<source lang="csharp">

  1. 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);

}

 </source>


Defining Index Properties

<source lang="csharp">

  1. 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]);

}

 </source>


Indexed properties

<source lang="csharp">

  1. 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] );

}

 </source>


Indexed properties 2

<source lang="csharp">

  1. 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] );

}

 </source>


Property accessor delegate

<source lang="csharp">

  1. 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());

}

 </source>


Public properties can be used as named parameters

<source lang="csharp">

  1. 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);
   }

}

 </source>


Scalar number Property

<source lang="csharp">

  1. 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);

}

 </source>


Static property

<source lang="csharp">

  1. include "stdafx.h"

value class MyClass{

  public:
     static property array<MyClass>^ pp;
     static MyClass()
     {
         pp = gcnew array<MyClass>(120);
     }

};

 </source>


Virtual properties

<source lang="csharp">

  1. 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; }
  }

};

 </source>


Write only property

<source lang="csharp">

  1. 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;
     }

};

 </source>