The prog is too complicated to understand. Please can anyone give me any
link to easier oneI am trying to learn hoe to create self defined exception
handling programs.
using System;
// Create a RangeArray exception.
class RangeArrayException : ApplicationException
{
// Implement the standard constructors
public RangeArrayException() : base() { }
public RangeArrayException(string str) : base(str) { }
// 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; // lowest index
int upperBound; // greatest index
int len; // underlying var for Length property
// 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];
len = high - low;
lowerBound = low;
upperBound = --high;
}
// Read-only Length property.
public int Length
{
get
{
return len;
}
}
// This is the indexer for RangeArray.
public int this[int index] // <Unable
to understand this property>
{
// This is the get accessor.
get
{
if (ok(index)) //<We
have not declared OK then how wil compiler understand this keyword>
{
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
{
public 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;
Console.Write("Contents of ra: ");
for (int i = -5; i <= 5; i++)
Console.Write(ra + " ");
Console.WriteLine("\n");
// Demonstrate ra2
Console.WriteLine("Length of ra2: " + ra2.Length);
for (int i = 1; i <= 10; i++)
ra2 = i;
Console.Write("Contents of ra2: ");
for (int i = 1; i <= 10; i++)
Console.Write(ra2 + " ");
Console.WriteLine("\n");
}
catch (RangeArrayException exc)
{
Console.WriteLine(exc);
}
// Now, demonstrate some errors.
Console.WriteLine("Now generate some range errors.");
// Use an invalid constructor.
try
{
RangeArray ra3 = new RangeArray(100, -10); // Error
}
catch (RangeArrayException exc)
{
Console.WriteLine(exc);
}*
// Use an invalid index.
try
{
RangeArray ra3 = new RangeArray(-2, 2);
for (int i = -2; i <= 2; i++)
ra3* = i;
Console.Write("Contents of ra3: ");
for (int i = -2; i <= 10; i++) // generate range error
Console.Write(ra3 + " ");
}
catch (RangeArrayException exc)
{
Console.WriteLine(exc);
}
Console.ReadKey();
}
}*
If anyone try to compile this proggplease remove these lines from the progg
if you are trying to compile it
<We have not declared OK then how wil compiler understand this keyword>
and
<Unable to understand this property>
these are two problems which I was unable to understand
please if possible for anyone write comment on this probs also
I'll be thankful to you
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web
Services,.NET Remoting" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/DotNetDevelopment
You may subscribe to group Feeds using a RSS Feed Reader to stay upto date
using following url
<a href="http://feeds.feedburner.com/DotNetDevelopment">
http://feeds.feedburner.com/DotNetDevelopment</a>
-~----------~----~----~----~------~----~------~--~---