private Type[] _arrayOfTypes = new Type[]
{
typeof(IndexOutOfRangeException),
typeof(HttpException)
};
Type is just a class in the .NET framework...
What's happening here is that I'm using the collection initializer construct from C#3.0/.NET 3.5. It enables me to create the array with the elements I need in a single statement.
The verbose alternative would have been something like this...
That makes more sense to me, thanks! I rewrote my ErrorClass using the code you gave me and I took out the error msgs which dont really concern the user. The question is how do I retrieve the error message type so that I can check it against the array?
Code:
// Create an array of types
protected Type[] _arrayOfTypes = new Type[]
{
typeof(HttpException),
typeof(UnauthorizedAccessException),
typeof(FileNotFoundException),
typeof(IOException),
typeof(IndexOutOfRangeException),
typeof(NullReferenceException)
};
// This is a different way for doing:
#region Old method
//protected void GetArray()
//{
// _arrayOfTypes[0] = typeof(IndexOutOfRangeException);
// _arrayOfTypes[1] = typeof(HttpException);
//}
// where
//protected Type[] _arrayOfTypes = new Type[2];
#endregion
public string GetErrorMsg(Type sError)
{
if (sError == _arrayOfTypes[0])
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (sError == _arrayOfTypes[1])
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (sError == _arrayOfTypes[2])
{
sMsgErr = "Die Datei wurde nicht gefunden.";
}
else if (sError == _arrayOfTypes[3])
{
sMsgErr = "Ein E/A-Fehler ist auftritten.";
}
else if (sError == _arrayOfTypes[4])
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
return sMsgErr;
}
// Create an array of types
protected Type[] _arrayOfTypes = new Type[]
{
typeof(HttpException),
typeof(UnauthorizedAccessException),
typeof(FileNotFoundException),
typeof(IOException),
typeof(IndexOutOfRangeException)
};
public string GetErrorMsg(Type sError)
{
if (sError == _arrayOfTypes[0])
{
sMsgErr = "Zugang zur Seite nicht erlaubt.";
}
else if (sError == _arrayOfTypes[1])
{
sMsgErr = "Zugang nicht erlaubt.";
}
else if (sError == _arrayOfTypes[2])
{
sMsgErr = "Die Datei wurde nicht gefunden.";
}
else if (sError == _arrayOfTypes[3])
{
sMsgErr = "Ein E/A-Fehler ist auftritten.";
}
else if (sError == _arrayOfTypes[4])
{
sMsgErr = "Die Angegebene ID existiert nicht.";
}
else if (sError == _arrayOfTypes[5])
{
sMsgErr = "Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.";
//errorMsg = "Ein nicht vorhandenes Objekt wird verweisen.";
}
else
{
sMsgErr = "Ein Fehler ist aufgetreten.";
}
return sMsgErr;
}
Bookmarks