From 36700b30b35d214dc2d5a72e18e4053a035b6b52 Mon Sep 17 00:00:00 2001 From: PocketMiner82 Date: Mon, 15 Apr 2024 10:10:39 +0200 Subject: [PATCH] fix: benutzereingabe string always returned null --- .../csharpcode/TemplateCodeOutput.cs | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pseudocodeIde/interpreter/csharpcode/TemplateCodeOutput.cs b/pseudocodeIde/interpreter/csharpcode/TemplateCodeOutput.cs index 7aaf3c7..3dc0834 100644 --- a/pseudocodeIde/interpreter/csharpcode/TemplateCodeOutput.cs +++ b/pseudocodeIde/interpreter/csharpcode/TemplateCodeOutput.cs @@ -74,14 +74,22 @@ protected virtual void _warte(int millis) try { - return prompt.ShowDialog() == DialogResult.OK && double.TryParse(textBox.Text, out double result) - ? (T?)Convert.ChangeType(result, Nullable.GetUnderlyingType(typeof(T))) - : (T?)Convert.ChangeType(textBox.Text, Nullable.GetUnderlyingType(typeof(T))); - } - catch - { - return default; + DialogResult result = prompt.ShowDialog(); + Type nullableType = Nullable.GetUnderlyingType(typeof(T)); + Type type = typeof(T); + Type usedType = nullableType ?? type; + + if (result == DialogResult.OK) + { + return (usedType == typeof(double) || usedType == typeof(int)) + && double.TryParse(textBox.Text, out double parsed) + ? (T)Convert.ChangeType(parsed, usedType) + : (T)Convert.ChangeType(textBox.Text, usedType); + } } + catch { } + + return default; } }