C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet330/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   326   327   328   329   330   331   332   333   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

FailSoftArray

, a variable called 



ErrFlag

 contains the 

outcome of each operation. This field can be examined after each operation to assess the 

success or failure of the operation. (In Chapter 13, you will see a better way to handle errors 

by using C#’s exception subsystem, but for now, using an error flag is an acceptable 

approach.)

The

set

 accessor is shown here. It too prevents a boundary error.

set {

  if(ok(index)) {



    a[index] = value;

    ErrFlag = false;

  }

  else ErrFlag = true;



}

Here, if 



index

 is within bounds, the value passed in 



value

 is assigned to the corresponding 

element. Otherwise, 

ErrFlag

 is set to 



true

. Recall that in an accessor method, 



value

 is an 


www.freepdf-books.com


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  

249


PART IPART I

implicit parameter that contains the value being assigned. You do not need to (nor can you) 

declare it.

It is not necessary for an indexer to support both 



get

 and 


set

. You can create a read-only 

indexer by implementing only the 

get

 accessor. You can create a write-only indexer by 

implementing only 

set

.

Indexers Can Be Overloaded

An indexer can be overloaded. The version executed will be the one that has the closest 

type-match between its parameter and the argument used as an index. Here is an example 

that overloads the 

FailSoftArray

 indexer for indexes of type 



double

. The 


double

 indexer 

rounds its index to the nearest integer value.

// Overload the FailSoftArray indexer.

using System;

class FailSoftArray {

  int[] a;    // reference to underlying array

  public int Length; // Length is public

  public bool ErrFlag; // indicates outcome of last operation

  // Construct array given its size.

  public FailSoftArray(int size) {

    a = new int[size];

    Length = size;

  }


  // This is the int indexer for FailSoftArray.

  public int this[int index] {

    // This is the get accessor.

    get {

      if(ok(index)) {

        ErrFlag = false;

        return a[index];

      } else {

        ErrFlag = true;

        return 0;

      }

    }


    // This is the set accessor.

    set {

      if(ok(index)) {

        a[index] = value;

        ErrFlag = false;

      }


      else ErrFlag = true;

    }


  }

www.freepdf-books.com




250

 

P a r t   I :  



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

  /* This is another indexer for FailSoftArray.

     This index takes a double argument. It then

     rounds that argument to the nearest integer index. */

  public int this[double idx] {

    // This is the get accessor.

    get {

      int index;

      // Round to nearest int.

      if( (idx - (int) idx) < 0.5) index = (int) idx;

      else index = (int) idx + 1;

      if(ok(index)) {

        ErrFlag = false;

        return a[index];

      } else {

        ErrFlag = true;

        return 0;

      }


    }

    // This is the set accessor.

    set {

      int index;

      // Round to nearest int.

      if( (idx - (int) idx) < 0.5) index = (int) idx;

      else index = (int) idx + 1;

      if(ok(index)) {

        a[index] = value;

        ErrFlag = false;

      }

      else ErrFlag = true;

    }

  }


  // Return true if index is within bounds.

  private bool ok(int index) {

   if(index >= 0 & index < Length) return true;

   return false;

  }

}

// Demonstrate the fail-soft array.



class FSDemo {

  static void Main() {

    FailSoftArray fs = new FailSoftArray(5);

    // Put some values in fs.

    for(int i=0; i < fs.Length; i++)

      fs[i] = i;

    // Now index with ints and doubles.

    Console.WriteLine("fs[1]: " + fs[1]);

www.freepdf-books.com



Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   326   327   328   329   330   331   332   333   ...   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