C# 0 The Complete Reference


RangeArray  throw a custom exception when a range error occurs.  This is precisely what the following version of  RangeArray



Download 4,07 Mb.
Pdf ko'rish
bet462/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   458   459   460   461   462   463   464   465   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

RangeArray

 throw a custom exception when a range error occurs. 

This is precisely what the following version of 

RangeArray

 does:


// Use a custom Exception for RangeArray errors.

using System;

// Create a RangeArray exception.

class RangeArrayException : Exception {

  /* Implement all of the Exception constructors. Notice that

     the constructors simply execute the base class constructor.

     Because RangeArrayException adds nothing to Exception,

     there is no need for any further actions. */

  public RangeArrayException() : base() { }

  public RangeArrayException(string str) : base(str) { }

  public RangeArrayException(string str, Exception inner) :

    base(str, inner) { }

  protected RangeArrayException(

    System.Runtime.Serialization.SerializationInfo si,

    System.Runtime.Serialization.StreamingContext sc) :

      base(si, sc) { }

  // Override ToString for RangeArrayException.

  public override string ToString() {

    return Message;

  }


}

// An improved version of RangeArray.

class RangeArray {

  // Private data.

  int[] a; // reference to underlying array

  int lowerBound; // smallest index

  int upperBound; // largest index

  // An auto-implemented, read-only Length property.

  public int Length { get; private set; }

www.freepdf-books.com




356

 

P a r t   I :  



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

  // Construct array given its size.

  public RangeArray(int low, int high) {

    high++;

    if(high <= low) {

      throw new RangeArrayException("Low index not less than high.");

    }

    a = new int[high - low];



    Length = high - low;

    lowerBound = low;

    upperBound = --high;

  }


  // This is the indexer for RangeArray.

  public int this[int index] {

    // This is the get accessor.

    get {

      if(ok(index)) {

        return a[index - lowerBound];

      } else {

        throw new RangeArrayException("Range Error.");

      }

    }


    // This is the set accessor.

    set {

      if(ok(index)) {

        a[index - lowerBound] = value;

      }

      else throw new RangeArrayException("Range Error.");

    }

  }


  // Return true if index is within bounds.

  private bool ok(int index) {

    if(index >= lowerBound & index <= upperBound) return true;

    return false;

  }

}

// Demonstrate the index-range array.



class RangeArrayDemo {

  static void Main() {

    try {

      RangeArray ra = new RangeArray(-5, 5);

      RangeArray ra2 = new RangeArray(1, 10);

      // Demonstrate ra.

      Console.WriteLine("Length of ra: " + ra.Length);

      for(int i = -5; i <= 5; i++)

        ra[i] = i;

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   458   459   460   461   462   463   464   465   ...   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