C# 0 The Complete Reference


PART I C h a p t e r   1 0



Download 4,07 Mb.
Pdf ko'rish
bet334/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   330   331   332   333   334   335   336   337   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt


PART I

C h a p t e r   1 0 :  

I n d e x e r s   a n d   P r o p e r t i e s  

253


PART IPART I

    rows = r;

    cols = c;

    a = new int[rows, cols];

    Length = rows * cols;

  }


  // This is the indexer for FailSoftArray2D.

  public int this[int index1, int index2] {

    // This is the get accessor.

    get {

      if(ok(index1, index2)) {

        ErrFlag = false;

        return a[index1, index2];

      } else {

        ErrFlag = true;

        return 0;

      }

    }


    // This is the set accessor.

    set {

      if(ok(index1, index2)) {

        a[index1, index2] = value;

        ErrFlag = false;

      }


      else ErrFlag = true;

    }


  }

  // Return true if indexes are within bounds.

  private bool ok(int index1, int index2) {

   if(index1 >= 0 & index1 < rows &

      index2 >= 0 & index2 < cols)

         return true;

   return false;

  }


}

// Demonstrate a 2D indexer.

class TwoDIndexerDemo {

  static void Main() {

    FailSoftArray2D fs = new FailSoftArray2D(3, 5);

    int x;

    // Show quiet failures.

    Console.WriteLine("Fail quietly.");

    for(int i=0; i < 6; i++)

      fs[i, i] = i*10;

    for(int i=0; i < 6; i++) {

      x = fs[i,i];

      if(x != -1) Console.Write(x + " ");

    }


www.freepdf-books.com


254

 

P a r t   I :  



T h e   C #   L a n g u a g e

    Console.WriteLine();

    // Now, display failures.

    Console.WriteLine("\nFail with error reports.");

    for(int i=0; i < 6; i++) {

      fs[i,i] = i*10;

      if(fs.ErrFlag)

        Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds");

    }

    for(int i=0; i < 6; i++) {



      x = fs[i,i];

      if(!fs.ErrFlag) Console.Write(x + " ");

      else

        Console.WriteLine("fs[" + i + ", " + i + "] out-of-bounds");

    }

  }


}

The output from this program is shown here:

Fail quietly.

0 10 20 0 0 0

Fail with error reports.

fs[3, 3] out-of-bounds

fs[4, 4] out-of-bounds

fs[5, 5] out-of-bounds

0 10 20 fs[3, 3] out-of-bounds

fs[4, 4] out-of-bounds

fs[5, 5] out-of-bounds

Properties

Another type of class member is the 



property.

 As a general rule, a property combines a field 

with the methods that access it. As some examples earlier in this book have shown, you will 

often want to create a field that is available to users of an object, but you want to maintain 

control over the operations allowed on that field. For instance, you might want to limit the 

range of values that can be assigned to that field. While it is possible to accomplish this goal 

through the use of a private variable along with methods to access its value, a property 

offers a better, more streamlined approach.

Properties are similar to indexers. A property consists of a name along with 


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   330   331   332   333   334   335   336   337   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish