Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How to do that Console.ReadLine() fetch only a given number of characters? For example, 20th
I have read that gives you the name but it can not be longer than 20 characters and this must be checked dynamically as you enter.
Since I have a set type text between [(here 20 space here)] and I can not let you type text escaped as "]".

C#
Console.SetCursorPosition(25, 5);
      Console.WriteLine("Enter the playlist name");

      Console.SetCursorPosition(25, 6);
      Console.WriteLine("[");

      Console.SetCursorPosition(47, 6);
      Console.WriteLine("]");

      Console.SetCursorPosition(26, 6);

      Console.ForegroundColor = ConsoleColor.White;
      // ****************

      // *****************
      MyPlayList.Add(new PlayList(Console.ReadLine()));


Now my question How can I restrict the Console.Readline() to accept only XX(let say 20) number of characters and not allow user to event type on console screen further ?
Posted
Updated 11-Feb-12 8:54am
v2
Comments
LanFanNinja 11-Feb-12 14:43pm    
I don't understand what your asking for???
Sergey Alexandrovich Kryukov 11-Feb-12 15:16pm    
Why? This is crazy and unreasonable, but not unclear (may be it was unclear in the question version you saw). I answered, please see.
--SA
LanFanNinja 11-Feb-12 15:22pm    
You're right. I missed this text at the bottom (if it was there when I read the question the first time?).

"Now my question How can I restrict the Console.Readline() to accept only XX(let say 20) number of characters and not allow user to event type on console screen further ?"

I should have been more careful reading the question.
RDBurmon 11-Feb-12 14:56pm    
Hello LanXXXXXX , Read the updated question by me.

No, you cannot. The Console methods Code.Read and Console.ReadLine do exactly what they do and nothing else.

The style of work with console simply makes no sense. The console is designed to work with a human operator who normally can clearly perceive and input lines, in worst case, some properly delimited words.

Theoretically speaking, you can get an input stream System.Console.In and operate it in byte-to-byte manner, see http://msdn.microsoft.com/en-us/library/system.console.in.aspx[^].

However, even that would make no sense. What makes sense is the following pattern: always use Console.ReadLine, read the whole string, assign it to the local variable and parse this string the way you want. Keep it simple and convenient to the user.

Also, let me tell you: in any practical sense, a dialog with the user in a console is unreliable; it's hard to process user's error, etc. Such dialogs are often used just for learning purposes by software engineering students. If you need more complex input, develop a windowed application, with a Window/Form and controls.

The usable application in 99.9% cases don't use Console.ReadLine or Console.Read at all. It simply expect all the information at once in a command line which is parsed during run time. If more data is expected from a user, an input file is used.

For command-line parsing, you are welcome to use my easy-to-use library Enumeration-based Command Line Utility[^].

—SA
 
Share this answer
 
v2
You can't. The ReadLine Method always returns what the user input, you do not get any control until the user types a character indicating the end of the line, or end of the stream. You cannot tell it how many character it may fetch.

To do this, you need to call the ReadKey method[^] in a loop, and check for end-of-line yourself.
 
Share this answer
 

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