Kha hat folgendes geschrieben : |
Falls Letzteres zutrifft, dann haben Fehlermeldungen an den User dort nix verloren . Gib sie per Exception an die Anwendung weiter, die sie dann anzeigen soll. |
Es geht um meinen XML-Adapter, den ich grad schreibe. Also eine rein funktionelle und wiederverwendbare Klasse.
Ja, ich habe auch schon überlegt die Ausgabe in das View zu legen. Allerdings ist das Problem, dass mein Mitprogrammierer, welcher für die GUI verantwortlich ist, mit CSharp seine erste OOP-Programmiersprache erlernt. Ich habe genug damit zu tun ihm die wichtigesten Grundlagen und Kniffe zu zeigen. Für mich ist es da aufgrund von Delphi-Erfahrungen schon einfacher.
Aber Du hast auf jeden Fall recht.
Habe erst einmal folgenden Workaround:
Classes/Tools/Program.cs
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
| namespace Classes.Tools { static public class Program { public const int APPTYPE_UNKNOWN = 0; public const int APPTYPE_CONSOLE = 1; public const int APPTYPE_WINFORM = 2;
static public int AppType = APPTYPE_UNKNOWN; } } |
Classes/Tools/Message.cs
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
| using System; using System.Windows.Forms;
namespace Classes.Tools { static public class Message { static public void ShowException(string pTitle, string pMessage) { if (Program.AppType == Program.APPTYPE_CONSOLE) { Console.WriteLine(pTitle + ": " + pMessage); } if (Program.AppType == Program.APPTYPE_WINFORM) { MessageBox.Show(pMessage, pTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
static public void ShowException(string pTitle, Exception pException) { ShowException(pTitle, pException.Message); }
static public void ShowException(Exception pException) { ShowException("Ausnahmefehler", pException.Message); } } } |
Classes/Xml/Adapter.cs
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42:
| using System; using System.IO;
namespace Classes.Xml { class Adapter { public Adapter() { }
public Adapter(String pPath) { this._path = pPath; this._data = new System.Xml.XmlDocument(); try { this._data.Load(this._sourceFile); } catch (DirectoryNotFoundException) { Tools.Message.ShowException("IO-Fehler", "Pfad der XML-Ressource nicht gefunden.\n\n" + this._path); } catch (FileNotFoundException) { Tools.Message.ShowException("IO-Fehler", "XML-Ressource nicht gefunden.\n\n" + this._path); } catch (IOException) { Tools.Message.ShowException("IO-Fehler", "Unbekannter IO-Fehler beim Laden der XML-Ressource.\n\n" + this._path); } catch (Exception e) { Tools.Message.ShowException("Ausnahmefehler( XML-Adapter )", e); } }
private String _path;
private System.Xml.XmlDocument _data; } } |
(Anwendung) Program.cs
C#-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23:
| using System; using System.Windows.Forms;
namespace AdDresser { static public class Program { [STAThread] static void Main() { Classes.Tools.Program.AppType = Classes.Tools.Program.APPTYPE_WINFORM;
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormStartmenu()); } } } |
So stelle ich jetzt im Prinzip über eine generische Klasse den Typ der Anwendung ein. Ist sicher noch nicht Optimal, aber doch sehr einfach nutzbar.
Werde sicher bei Gelegenheit ein Exception-Handling für die GUI implementieren. (Es gibt immer so viel, was man gerne bei Gelegenheit machen möchte.)
Danke Euch Zweien.
Christian