Here is my problem. Do I need to give a new identifier to each delegate I write of the following delegate type? like so: or could i use one delegate that accounts for any Datatype I need to use so i don't have to keep repeating the code?
Something that may or may not be relevant: I'm calling these functions to prompt user's input for new fields to add for records in a database. So database field types are the data types I'm dealing with, but I'm using this approach for learning purposes to try and minimize redundancy.
public static class stcHelper // helper class { public delegate T DelReturnType<T>(); static public DelReturnType<int> UserInt = () => { Console.Write("Enter integer: "); return int.Parse(Console.ReadLine()); }; static public DelReturnType<string> UserString = () => { Console.Write("Enter string: "); return Console.ReadLine(); }; static public DelReturnType<AnotherDataType> UserAnotherDataType = () => { Console.Write("Enter AnotherDataType: "); return SomeKindOfConversionFunction(Console.ReadLine()); }; // and so on for what could be 15 other data types .. } or is there a better way to reduce redundant code?
The code below is also part of the same class.
public static class stcHelper { static public T InsistValidInput<T>(DelTypeReturn<T> MyInputAction) { bool bSuccess = true; do { try { return MyInputAction(); } catch (Exception ex) { Console.WriteLine("Invalid input!" + ex.ToString()); bSuccess = false; } } while (!bSuccess); return default(T); } }