Interesting, I have never seen that type of structure before. Could you explain it please?
Printable View
Interesting, I have never seen that type of structure before. Could you explain it please?
Type is just a class in the .NET framework...Code:private Type[] _arrayOfTypes = new Type[]
{
typeof(IndexOutOfRangeException),
typeof(HttpException)
};
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...
Well in terms of lines of code it is pretty much the same. I just like to use the new language features when I have the opportunity.Code:Type[] _arrayOfTypes = new Type[2];
_arrayOfTypes[0] = typeof(IndexOutOfRangeException);
_arrayOfTypes[1] = typeof(HttpException);
Edit: I would like to put a disclaimer here to say that I'm aware that C#3.0 isn't exactly new...:)
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;
}
Sorry I meant:
Code:// 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;
}
Quote:
Originally Posted by stepsh
Every object in .NET framework implements a GetType method which should give you the type.Code:try
{
}
catch (Exception e)
{
ErroHandler.GetErrorMsg(e.GetType());
}
Thanks I can now display the type of exception! I havent been able to find an exception that fits my type yet but Im sure it works :)