Jul 25
2009
Data
Filed Under (Uncategorized) by nameless on 25-07-2009
Data tulajdonságok:
using System;
using System.Collections;
namespace Program
{
class Radio
{
public void TurnOn(bool on)
{
if (on)
Console.WriteLine("Jamming...");
else
Console.WriteLine("Quite time...");
}
}
class Car
{
//max sebesség konstansa
public const int MaxSpeed = 100;
//Belső állapot adatok
private int currSpeed;
private string petName;
//az autó még mindig működőképes?
private bool CarIsDead;
//Az autónak van egy rádiója
private Radio theMusicBox = new Radio();
//Konstruktorok
public Car() { }
public Car(string name, int currSp)
{
currSpeed = currSp;
petName = name;
}
//Rádió be?
public void CrankTunes(bool state)
{
theMusicBox.TurnOn(state);
}
//Kivételt jelez, hogyha a felhasználó a MaxSpeed értéke fölé gyorsítja za autót.
public void Accelerate(int delta)
{
if (CarIsDead)
Console.WriteLine("{0} is out of order...", petName);
else
{
currSpeed += delta;
if (currSpeed > MaxSpeed)
{
CarIsDead = true;
currSpeed = 0;
//A HelpLink tulajdonságot kell hívni, ezért az Exception objektum kiváltása előtt lokállissá kell tenni
Exception ex = new Exception(string.Format("{0} has overheated!", petName));
ex.HelpLink = "http://www.daniel-projects.netne.net"; //hibát dobunk
//Data
//a hibára vonatkozó egyedi adatok betöltése
ex.Data.Add("TimeStamp", string.Format("The car exploded at {0}", DateTime.Now));
ex.Data.Add("Cause", "You have a lead foot.");
throw ex;
}
else
Console.WriteLine("=> CurrSpeed = {0}", currSpeed);
}
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine("*****Simple Exception Example*****");
Console.WriteLine("=> Creating a car and steppong on it!");
var myCar = new Car("David", 20); //zippy a neve és jelenleg 20-szal megyünk
myCar.CrankTunes(true); //rádió bekapcsolva
try
{
for (int i = 0; i < 10; i++)
myCar.Accelerate(10); //10-zel nő a sebesség addig, amíg elnem éri a 100-at, utána felrobban
}
catch (Exception e)
{
Console.WriteLine("*** Error! ***");
//**********************<Data>************************
//Alapértelmezés azerint az adatmező üres, ellenőrizni kell a null értéket
Console.WriteLine("\n-> Custom Data:");
if (e.Data != null)
{
foreach (DictionaryEntry de in e.Data)
Console.WriteLine("-> {0}: {1}", de.Key, de.Value);
}
//**********************</Data>***********************
//**********************<HelpLink>**********************
Console.WriteLine("\n\nHelp Link: {0}", e.HelpLink);
//**********************</HelpLink>********************
//************************<StackTrace>******************
Console.WriteLine("\n\nStack: {0}\n\n", e.StackTrace); //megmutatja a hiba pontos helyét!
//************************</StackTace>*****************
Console.WriteLine("Method: {0}", e.TargetSite);
Console.WriteLine("Class defining member: {0}",e.TargetSite.DeclaringType);
Console.WriteLine("Member type: {0}", e.TargetSite.MemberType);
Console.WriteLine("Message: {0}", e.Message);
Console.WriteLine("Source: {0}", e.Source);
}
//A hibát kezeltük, a program FOLYTATÓDIK
Console.WriteLine("\n***** Out of Exception logic *****");
Console.ReadKey();
}
}
}
