1. Object-Oriented Programming Using C#
Objectives
In this session, you will learn to:
Implement read and write in text files
Implement read and write in binary files
Implement the Windows File System
Describe exceptions
Ver. 1.0 Session 13 Slide 1 of 24
2. Object-Oriented Programming Using C#
Implementing Reading and Writing in the Text Files
• The Stream class is used to read from and to write data in
the text files.
• It is an abstract class, which supports reading and writing
bytes into it.
• If data of a file is only text, then you can use the
StreamReader class and the StreamWriter class to
accomplish the reading and writing tasks, respectively.
Ver. 1.0 Session 13 Slide 2 of 24
3. Object-Oriented Programming Using C#
StreamReader Class
• The StreamReader class is inherited from the abstract class
TextReader.
• The TextReader class represents a reader which can read
a series of characters.
Ver. 1.0 Session 13 Slide 3 of 24
4. Object-Oriented Programming Using C#
StreamReader Class (Contd.)
• The following table describes some of the commonly used
methods of the StreamReader class.
Methods Description
Close Closes the object of StreamReader class and the underlying stream, and
releases any system resources associated with the reader
Peek Returns the next available character but does not consume it
Read Reads the next character or the next set of characters from the stream
ReadLine Reads a line of characters from the current stream and returns data as a
string
Seek Allows the read/write position to be moved to any position within the file
Ver. 1.0 Session 13 Slide 4 of 24
5. Object-Oriented Programming Using C#
StreamWriter Class
• The StreamWriter class is inherited from the abstract class
TextWriter.
• The TextWriter class represents a writer, which can write
a series of characters.
• The following table describes some of the commonly used
methods of the StreamWriter class.
Methods Description
Close Closes the current StreamWriter object and the underlying stream
Flush Clears all buffers for the current writer and causes any buffered data to be
written to the underlying stream
Write Writes to the stream
WriteLine Writes data specified by the overloaded parameters, followed by end of
line
Ver. 1.0 Session 13 Slide 5 of 24
6. Object-Oriented Programming Using C#
Implementing Reading and Writing in Binary Files
• In C#, you can directly display the contents of the file on the
screen.
• Binary read and write means that a number is written as a
float representation consuming four bytes of space.
• The BinaryReader and BinaryWriter classes are used
for reading and writing the binary data in to files.
Ver. 1.0 Session 13 Slide 6 of 24
7. Object-Oriented Programming Using C#
BinaryReader Class
• The BinaryReader class is used to read binary data from
a file.
• The following table describes some of the commonly used
methods of the BinaryReader class.
Method Description
Close Closes the current reader and the underlying stream
Read Reads characters from the underlying stream and advances the current
position of the stream
Ver. 1.0 Session 13 Slide 7 of 24
8. Object-Oriented Programming Using C#
BinaryWriter Class
• The BinaryWriter class is used to write binary data to a
stream.
• The following table describes some of the commonly used
methods of the BinaryWriter class.
Method Description
Close Closes the current BinaryWriter and the underlying stream
Seek Sets the position within the current stream
Write Writes a value to the current stream
Flush Clears all buffers for the current writer and causes any buffered data
to be written to the underlying device
Ver. 1.0 Session 13 Slide 8 of 24
9. Object-Oriented Programming Using C#
Implementing the Windows File System
• The ability to browse and locate files and directories for a
specific directory is essential for many programming tasks.
• Using classes such as the DirectoryInfo and FileInfo
classes in combination is an efficient way to gather the
required information about files and directories in a specific
location.
Ver. 1.0 Session 13 Slide 9 of 24
10. Object-Oriented Programming Using C#
DirectoryInfo Class
• The DirectoryInfo class is derived from the
FileSystemInfo class.
• The following table describes some of the commonly used
properties of the DirectoryInfo class.
Property Description
Gets or sets attributes associated with the current file. This property is inherited
Attributes
from FileSystemInfo.
Gets or sets CreationTime of the current file. This property is inherited from
CreationTime
FileSystemInfo.
Exists Gets a Boolean value indicating whether the directory exist or not.
Gets a string containing the file extension. This property is inherited from
Extension
FileSystemInfo.
Gets a string containing the full path of the directory. This property is inherited from
FullName
FileSystemInfo.
Gets the last accessed time of the directory. This property is inherited from
LastAccessTime
FileSystemInfo.
Name Gets a string containing the name of a given file.
Ver. 1.0 Session 13 Slide 10 of 24
11. Object-Oriented Programming Using C#
DirectoryInfo Class (Contd.)
• The following table describes some of the commonly used
methods of the DirectoryInfo class.
Method Description
Create Creates a directory.
CreateSubdirectory Creates a subdirectory.
Delete Deletes a directory.
Returns the directories in the current directory after matching all the
GetDirectories
criteria. It also allows you to search subdirectories within directories.
GetFiles Returns the files in the current directory.
Ver. 1.0 Session 13 Slide 11 of 24
12. Object-Oriented Programming Using C#
FileInfo Class
• The FileInfo class is derived from FileSystemInfo
class.
• The following table describes some commonly used
properties FileInfo class.
Property Description
Gets or sets attributes associated with the current file. This property is inherited from
Attributes
FileSystemInfo.
Gets or sets CreationTime of the current file. This property is inherited from
CreationTime
FileSystemInfo.
Directory Gets an instance of the directory which the file belongs to.
Exists Gets a boolean value indicating whether the file exists or not.
Extension Gets a string containing the file extension. This property is inherited from FileSystemInfo.
Gets a string containing the full path of the file. This property is inherited from
FullName
FileSystemInfo.
LastAccessTime Gets the last accessed time of the file. This property is inherited from FileSystemInfo.
Gets the time of the last written activity to the file. This property is inherited from
LastWriteTime
FileSystemInfo.
Length Gets the size of the file.
Name Gets a string containing the name of a given file.
Ver. 1.0 Session 13 Slide 12 of 24
13. Object-Oriented Programming Using C#
FileInfo Class (Contd.)
• The following table describes some of the commonly used
methods of the FileInfo class.
Method Description
Create Creates a file
AppendText Appends a text to the file represented by the FileInfo object
Delete Deletes a file
Open Opens file
OpenRead Opens a file in read-only mode
Ver. 1.0 Session 13 Slide 13 of 24
14. Object-Oriented Programming Using C#
Describing Exceptions
An exception is an erroneous situation that occurs during
program execution.
When an exception occurs in an application, the system
throws an error.
The error is handled through the process of exception
handling.
Ver. 1.0 Session 13 Slide 14 of 24
15. Object-Oriented Programming Using C#
Types of Errors
There are three types of errors that can occur in the
application:
– Syntax errors: Occurs when statements are not constructed
properly, keywords are misspelled, or punctuation is omitted.
– Run-time errors: Occurs when an application attempts to
perform an operation, which is not allowed at runtime.
– Logical errors: Occurs when an application compiles and runs
properly but does not produce the expected results.
Let us understand the various types of errors in detail.
Ver. 1.0 Session 13 Slide 15 of 24
16. Object-Oriented Programming Using C#
Syntax Errors
class Errors
{
Console.WriteLine(“Enjoy Errors”)
}
Semicolon is missing from Console.WriteLine statement
Ver. 1.0 Session 13 Slide 16 of 24
17. Object-Oriented Programming Using C#
Run-Time Errors
class Errors
{
int Num1=0;
int Num2=20;
int Num3;
Num3=Num2/Num1;
Console.WriteLine(“The Result is {0}”,
Num3);
}
Division by zero has taken place
Ver. 1.0 Session 13 Slide 17 of 24
18. Object-Oriented Programming Using C#
Logical Errors
class Errors
{
int Num1=10;
int Num2=2;
int Num3;
Num3=Num2/Num1;
Console.WriteLine(“The Result is
{0}”, Num3);
}
Expected result = 5
Present result = 0.2
Ver. 1.0 Session 13 Slide 18 of 24
19. Object-Oriented Programming Using C#
Exception Classes
• There are many exception classes which are directly or
indirectly derived from the System.Exception class.
Some of these classes are:
– System.ApplicationException class
– System.SystemException class
Ver. 1.0 Session 13 Slide 19 of 24
20. Object-Oriented Programming Using C#
Exception Classes (Contd.)
The hierarchy of the exception classes is displayed in the
following figure.
System.Exception
System.ApplicationException
System.SystemException
Ver. 1.0 Session 13 Slide 20 of 24
21. Object-Oriented Programming Using C#
Exception Classes (Contd.)
• The System.SystemException acts as a base class for
all the predefined system exceptions. The following table
describes some of the classes derived from the
System.SystemException class.
Exception Classes Description
System.IO.IOException Handles I/O errors
System.IndexOutOfRangeException Handles errors generated when a method refers to an array
element, which is out of its bound
System.NullReferenceException Handles errors generated during the process of
dereferencing a null object
System.DivideByZeroException Handles errors generated during the process of dividing the
dividend with zero
System.InvalidCastException Handles errors generated during typecasting
System.OutOfMemoryException Handles memory allocation to the application errors
Ver. 1.0 Session 13 Slide 21 of 24
22. Object-Oriented Programming Using C#
Summary
In this session, you learned that:
– The Stream class is used to read and write data to the text
files. It is an abstract class, which supports reading and writing
bytes into it.
– The StreamReader class is inherited from the abstract class
TextReader. The TextReader class represents a reader
which can read a series of characters.
– The StreamWriter class is inherited from the abstract class
TextWriter. The TextWriter class represents a writer,
which can write a series of characters.
– The BinaryReader class allows reading of binary data from a
file.
– The BinaryWriter class allows writing of binary data to a
stream.
Ver. 1.0 Session 13 Slide 22 of 24
23. Object-Oriented Programming Using C#
Summary (Contd.)
– The DirectoryInfo class is derived from FileSystemInfo
class and it works on a specific directory and shows the
fullpath of the current directory.
– The FileInfo class is derived from FileSystemInfo class
and it works on a specific directory to display the list of all the
files.
– An exception is an erroneous situation that occurs during
program execution.
– A syntax error occurs when a compiler cannot compile the
code. This may happen when the statements are not
constructed properly, keywords are misspelled, or punctuation
is omitted.
– A run-time error occurs when an application attempts to
perform an operation that is not allowed.
Ver. 1.0 Session 13 Slide 23 of 24
24. Object-Oriented Programming Using C#
Summary (Contd.)
A logical error occurs when an application compiles and runs
properly but does not produce the expected results.
Exceptional conditions arise when an operation cannot be
completed normally during the processing of C# statements
and expressions.
Ver. 1.0 Session 13 Slide 24 of 24
Notes de l'éditeur
Students have learnt the structure of different types of dimensions and the importance of surrogate keys in Module I. In this session, students will learn to load the data into the dimension tables after the data has been transformed in the transformation phase. In addition, students will also learn to update data into these dimension tables. Students already know about different types of dimension tables. Therefore, you can start the session by recapitulating the concepts. Initiate the class by asking the following questions: 1. What are the different types of dimensions? 2. Define flat dimension. 3. What are conformed dimension? 4. Define large dimension. 5. Define small dimension. 6. What is the importance of surrogate key in a dimension table? Students will learn the loading and update strategies theoretically in this session. The demonstration to load and update the data in the dimension table will be covered in next session.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Student already have learnt about SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 1 SCDs? Given an example to explain type 1 SCDs. This will recapitulate what they have learnt about type 1 SCD in Module 1. Now explain the strategy to load the data into these dimension tables with help of the given diagram. Relate this diagram to the example given in SG.
Students know the importance of surrogate keys. In this session students will learn the strategy to generate the surrogate key. Give an example to explain the strategy to generate the surrogate keys by concatenating the primary key of the source table with the date stamp. For example, data from a Product table has to be loaded into the Product_Dim dimension table on Feb 09, 2006. The product_code is the primary key column in the Product table. To insert the surrogate key values before loading the data into the dimension table, you can combine the primary key value with the date on which the data has to be loaded. In this case the surrogate key value can be product_code+09022006.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Students know what is the structure of Flat dimension. You can initiate the session by asking the following questions: 1. What are flat dimension tables? 2. What is the structure of flat dimension? 3. Given examples of a flat dimension? Next, tell the strategy to load the data into the flat dimension table. You can explain the loading strategy with the help of the example given in SG. Continue this session by asking the following questions: 4. What are large flat dimension tables? 5. Give examples of large flat dimensions? Then, explain the strategy to load data into the large flat dimension table. Before explaining the strategy to load data into the small dimension table ask the following questions and the tell the strategy to load the data into the dimension table. 6. What are small flat dimension tables? 7. Give examples of small flat dimension tables. With the help of these questions, students will be able to recall about flat dimensions, they have learnt in Module I. Explain this topic with the help of an example given in SG.
Student already have learnt about type 2 SCDs in Module I. Therefore, you can start this topic by asking the following questions to students: What are type 2 SCDs? Given an example to explain type 2 SCDs. This will recapitulate what they have learnt about type 2 SCD in Module 1. Now explain the strategy to update the data into these dimension tables with help the example given in SG. After explaining the examples, you can ask students to think of an example of a type 2 SCD and then tell the strategy to update the data into this dimension table.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.
You can summarize the session by running through the summary given in SG. In addition, you can also ask students summarize what they have learnt in this session.