PV178: Programming for the CLI Environment Seminar: Week 3 Michal Ordelt Institute of Computer Science and Faculty of Informatics Masaryk University March 19, 2007 * S public virtual string ToStringO This method returns a human-readable string that represents the current Object. Example: Modify L i b r a r y class and Book structure from previous seminar. For displaying do not use P r i n t () method, override T o S t r i n g method and use C o n s o l e . W r i t e O method. Do not forget to display capacity. Use S t r i n g B u i l d e r class. i using System . 10 ; 2 \ \ d e c l a r i n g a T e x t R e a d e r 3 p r i v a t e T e x t R e a d e r t e x t r e a d e r ; 4 \ \ a s i g n i n g a s t r e a m t o T e x t R e a d e r 5 t e x t r e a d e r = new S t r e a m R e a d e r ( path ); Example: Create GetData:IEnumerable class with constructor having one parameter of type s t r i n g . The class reads lines from the text file specified by the parameter and returns enumerator that goes throught lines of file. Try your class on the file from IS. A regular expression consists of two types of characters: literals and metacharacters. A literal is a character you wish to match in the target string. A metacharacter is a special symbol that acts as a command to the regular expression parser. The parser is the engine responsible for understanding the regular expression. All the objects associated with regular expressions are in the Base Class Library namespace System.Text .RegularExpressions. The central class for regular expression support is Regex, which represents an immutable, compiled regular expression. Although instances of Regex can be created, the class also provides a number of useful static methods. i using System . Text. RegularExpressions; 2 private Regex rx; 3 rx = new Regex( expression ); 4 i f ( r x . l s M a t c h ( t e x t ) ) { 5 \ \ D o something I Example: Extend the previous example as follows. The constructor have two parameters, the first is the path, the second is the regular expression, and it returns only string matching the expression. Implement one parametric constructor using two parametric constructor. Regex.Replace Method public string Replace ( string input, string replacement ) Example: Extend the previous example again. The returned strings will have all rude words replaced by -. C # , like many object-oriented languages, handles errors and abnormal conditions with exceptions. An exception is an object that encapsulates information about an unusual program occurrence. It is important to distinguish between bugs, errors, and exceptions. A bug is a programmer mistake that should be fixed before the code is shipped. Exceptions are not a protection against bugs. Although a bug might cause an exception to be thrown, you should not rely on exceptions to handle your bugs. Rather, you should fix the bug. When your program encounters an exceptional circumstance, such as running out of memory, it throws (or "raises") an exception. When an exception is thrown, execution of the current function halts and the stack is unwound until an appropriate exception handler is found. To signal an abnormal condition in a C # class, you throw an exception. To do this, use the keyword throw. This code creates a new instance of System.Exception and then throws it: i E x c e p t i o n e x c e p t i o n ; 2 e x c e p t i o n = new E x c e p t i o n (" S o m e t h i n g ^ b a d ^ h a p p e n e d " ) 3 throw e x c e p t i o n ; In C # , an exception handler is called a catch block and is created with the catch keyword. i Encoding encoding = Encoding . GetEncoding (codepage) 2 encoding . Get Bytes ( s t r i n g ) 3 encoding. G e t S t r i n g f byte [ ] ) public StreamWriter ( string path, bool append, Encoding encoding ) Example: Create a class that convert text file from one coding to another and saves it as new file. Use exceptions to cover all possible problems, that may occur. * s ˇ s