Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
How can you set a default input value in a .net console app?

tx
Posted
Comments
Richard MacCutchan 4-Oct-11 3:53am    
By setting a variable and checking your input.
Sergey Alexandrovich Kryukov 4-Oct-11 4:06am    
And how about the default? I think I understand what OP needs, tried to give an example.
--SA

1 solution

For example:
C#
const byte defaultInput = 4;
const byte inputMin = 1;
const byte inputMax = 8;

//...

byte input = defaultInput; //this answers the question, see below
System.Console.Write("Enter value ({0}..{1}); for default value ({2}), press ENTER: ",
    inputMin, inputMax, defaultInput); //updated to show default
string line = System.Console.ReadLine().Trim();
if (line.Length > 0) { // if not (trimmed string is empty) default value remains
   try {
       input = byte.Parse(line); //may throw exception
       if (input < inputMin || input > inputMax)
           //deal with invalid range
   } catch (/*...*/) {
       //deal with invalid input format
   }
}


—SA
 
Share this answer
 
v6
Comments
Richard MacCutchan 4-Oct-11 4:26am    
Agreed that this is probably what OP wants; I just thought the question was self-evident.
Sergey Alexandrovich Kryukov 4-Oct-11 11:14am    
Thank you, Richard.
--SA
Espen Harlinn 4-Oct-11 4:45am    
Nice reply, 5'ed!
Sergey Alexandrovich Kryukov 4-Oct-11 11:14am    
Thank you, Espen.
--SA
Simon Bang Terkildsen 4-Oct-11 5:01am    
Most likely what the OP is looking for, my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900