Wednesday, May 27, 2009

How to Use Streams

Dear Friends following article will help you to under to understand the stream class in .NET


Streams are another very common type because they are the means for reading from and writing to the disk and communicating across the network. The System.IO.Stream type is the base type for all task-specific stream types. Table 1-5 shows some of the most commonly used stream types. In addition, network streams are found in the System.Network.Sockets namespace, and encrypted streams are found in the System.Security.Cryptography namespace.

Table 1-5: Common Stream Types System.IO Type
Use to

FileStream
Create a base stream used to write to or read from a file

MemoryStream
Create a base stream used to write to or read from memory

StreamReader
Read data from the stream

StreamWriter
Write data to the stream


The simplest stream classes are StreamReader and StreamWriter, which enable you to read and write text files. You can pass a filename as part of the constructor, enabling you to open a file with a single line of code. After you have processed a file, call the Close method so that the file does not remain locked. The following code, which requires the System.IO namespace, demonstrates how to write to and read from a text file:

' VB
' Create and write to a text file
Dim sw As StreamWriter = New StreamWriter("text.txt")
sw.WriteLine("Hello, World!")
sw.Close

' Read and display a text file
Dim sr As StreamReader = New StreamReader("text.txt")
Console.WriteLine(sr.ReadToEnd)
sr.Close

// C#
// Create and write to a text file
StreamWriter sw = new StreamWriter("text.txt");
sw.WriteLine("Hello, World!");
sw.Close();

// Read and display a text file
StreamReader sr = new StreamReader("text.txt");
Console.WriteLine(sr.ReadToEnd());
sr.Close();

Saturday, May 23, 2009

DIagramatic Discription of DOT NET FrameWork

Dear Friends
I found Some of the Pictures which will give idea about what DOT NET is so here it is.



----------------------------------------------------------------------------------



-----------------------------------------------------------------------------------



------------------------------------------------------------------------------------



-------------------------------------------------------------------------------------



Hope these might help....................................

Wednesday, May 20, 2009

VB.NET Interview Series Part 2

Dear Friends
As I have promised here comes the second lot of VB.NET Interview Questions

Q7 : What is .NET?

Ans :- .NET is the development platform which provides advance features facilities for Windows and Web Application development. Dot NET acts as a middle ware between operating system and the .NET Application . The main advantage of dot net is that it's component can talk to each other regard less of the fact in which language they are created.
The two main components are
1.CLR(Common Language Runtime)
2.Frame Work base Libraries
CLR Performs task of Converting IL Code into Native Code .
The other component like .NET framework Case Class acts as a service to be provided to the .NET application.

Q8 : What is the Diff b/w String and StringBuilder?

Ans :- String is mutable means if you want to concatenate string into existing string, the framework copies both existing data into memory ,deletes the existing string and reads the data in new string . This operation hence is very resource consuming for lengthy string concatenation.

String builde is immutable means while adding one more char at the end of string it directly upend the string without removing original string .
(NOTE :- For it's clarification I have posted one article on this blog by the name String Builder in which how it works and how StringBuilder is better then String is explained with the help of an example.)

Q9 : What is the diff. b/w web.config file and machine.Config file ?

Ans : The settings made in web.config file are applied to the particular web application only where as the settings of the machine.cofig file are applied to the whole asp.net application.

Q10 : What is Jagged array?











Fig 1
A jagged array is the array which can have fixed rows but will have diff no of col per row as shown in the Fig 1.
A two dimensional jagged array can be thought of as a table where each row can be thought of as a table where families are the rows and family members are Columns
Syntax(To define) :- Dim Myarray(9)() As Integer

there are two ways of defining jagged array.

Method 1- Dim Myarray1() As Integer = {1,2,3}
Dim Myarray1() As Integer = {4,5}
Dim all_values ()() As Integer = {Myarray1,Myarray2}

Method 2- Dim all_Values()() As Integer = {New Integer () {1,2,3} ,New Integer{4,5}}

Tuesday, May 19, 2009

Create Folder on Specific Path

Dear Friends
In this article we will see how to create a Folder on a specific Location using vb.net Code......

Method 1
STEP 1: Create on Console based Application.

STEP 2: Paste the following Code

Imports System.IO
Module Module1

Sub Main()
Dim di As DirectoryInfo = New DirectoryInfo("D:\MyDir")
' Determine whether the directory exists.
If di.Exists Then
' Indicate that it already exists.
Console.WriteLine("Directory already Present")
Console.ReadLine()
Else
' Try to create the directory.
di.Create()

End If
End Sub

End Module

Metod 2
Method 1
STEP 1: Create on Console based Application.

STEP 2: Paste the following Code

Imports System.IO
Module Module1
Sub Main()
'System.IO.Directory.CreateDirectory("D:\MyDirector")
End Sub

End Module

String Builder

Dear Friends

Following article contains information regarding STRINGBUILDER in VB.NET

String concatenation is one of the commonly used operations among programmers. If you don't handle the string concatenation in .NET properly, it may decrease the performance of an application.

You can concatenate strings in two ways -

* First, traditional way of using string and adding the new string to an existing string. In the .NET Framework, this operation is costly. When you add a string to an existing string, the Framework copies both the existing and new data to the memory, deletes the existing string, and reads data in a new string. This operation may be very resource consuming in lengthy string concatenation operations.
* The second and better way to concatenate strings in .NET is using the StringBuilder class. The StringBuidler class provides the Append method, that inserts a new string to an existing string.

To prove this theory, we will write two routines and both will repeat the same operations. One is using the string class and second is using the StringBuilder class.

Listing 1 shows the routine that uses string class to concatenate strings.

As you can see from Listing 1, I make a loop and concatenate string using the "+" operator. Before I start the loop, I display the start time and when the loop is done, I display the stop time.

// Put user code to initialize the page here
// Concatenation using string
Console.WriteLine("String routine");
string str = string.Empty;
DateTime startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<10; i++)
{
str += i.ToString();
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());

Listing 1

Now let's see the routine that uses the StringBuidler class. As you can see from Listing 2, we do the exact sample operation but this time we use StringBuilder.Append method to concatenate strings.

// Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine");
StringBuilder builder = new StringBuilder();
startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<10; i++)
{
builder.Append(i.ToString());
}
stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());

Listing 2

Now let's run the application. You may not notice the difference for the loop from 1 to 10 as the difference is in milliseconds.

But if you change the loop as following:

for(int i=0; i<10000; i++)

You will notice the difference in seconds .

What about changing loop to the following?

for(int i=0; i<100000; i++)

Now you will see a big difference. This time the difference is in minutes.

Well, that was just time. Every time you add two strings using + operator, a new string allocation is reserverd in memory and used for this new string. So let's say, you are adding strings 100 times using + operator, there will be 100 new memory allocations. In case of StringBuilder, there will only be one memory allocation.

Recommendation
If you have to concatenate a string more than 10 times, it's better to use StringBuilder.

Sample Code

So be cautious when concatenating strings more than just once. Here is the complete listing of the sample code:

using System;
using System.Text;
namespace StringBuilderSamp
{
class Class1
{
static void Main()
{
// Concatenation using string
Console.WriteLine("String routine");
string str = string.Empty;
DateTime startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<100000; i++)
{
str += i.ToString();
}
DateTime stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());
// Concatenation using StringBuilder
Console.WriteLine("StringBuilder routine");
StringBuilder builder = new StringBuilder();
startTime = DateTime.Now;
Console.WriteLine("Start time:" + startTime.ToString());
for(int i=0; i<100000; i++)
{
builder.Append(i.ToString());
}
stopTime = DateTime.Now;
Console.WriteLine("Stop time:" + stopTime.ToString());
}
}
}

VB.NET Interview Questions Series 1

Dear Friends
From here on wards I am starting a new Series of Interview Questions and this series will be found on all of my blogs ,

http://learning-mysql.blogspot.com/
http://explore-aspnet.blogspot.com/
http://deepvbnet.blogspot.com/

Hope this may help those who are seeking for new Job,
If any of you found one of my answers as inappropriate I will really appreciate the correct answer and and if any one of you guys are looking for some question which you won't found on blog you can put that on any of Question series Comments area or can always mail me at
1.akashazad@hotmail.com"
2.akashazad@gmail.com"

So here Come the very first Part

VB.NET Interview Questions

Q1. What are the Benifites of using Class Library?

Ans :- Simple ! Why do we have public library? B/c once book is written, many people can subscribe to it and reuse it again & again.

In the same way once you ceate your own class library which have multiple function and procedures they can be published for your own use, or rewriting or performing copying of Code .

Q2. Which is the base class of .NET?

Ans :- System.Object
System is the Name space

Q3. What keyboard Should you use if you don’t want your class to be inherited?

Ans :- For Vb.NET
a. Class NonInheritable
b. Method  Notoverridable
And in C# use sealed

Q4. What is IL?

Ans :- When compiling to managed code, the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations. Before code can be run, MSIL must be converted to CPU-specific code, usually by a just-in-time (JIT) compiler. Because the common language runtime supplies one or more JIT compilers for each computer architecture it supports, the same set of MSIL can be JIT-compiled and run on any supported architecture. When a compiler produces MSIL, it also produces metadata. Metadata describes the types in your code, including the definition of each type, the signatures of each type’s members, the members that your code references, and other data that the runtime uses at execution time. The MSIL and metadata are contained in a portable executable (PE) file that is based on and extends the published Microsoft PE and common object file format (COFF) used historically for executable content. This file format, which accommodates MSIL or native code as well as metadata, enables the operating system to recognize common language runtime images. The presence of metadata in the file along with the MSIL enables your code to describe itself, which means that there is no need for type libraries or Interface Definition Language (IDL). The runtime locates and extracts the metadata from the file as needed during execution.


Q 5. Explain the Diff and similarities between a class and a structure in VB.NET?

Ans : Similarity
a. Both are User defined Types.
b. Both are temple of the object.
c. Both contains members like methods, Properties, fields and use Constructor for initialize.
d. Both of them Inherits from System.Object
Differences
a. Classes are Reference Types where as Structures are Value Types.

Q 6. What is implicit Type casting and Explicit Type Casting ?

Ans : Implicit Type Casting allow conversion without any loss of Data and it should be done when there is no possibility of loss of data.
But There is loss of data Explicit type Casting must be done.
Ex:- Implicit Type Casting
Sub Main()
Dim I as integer
Dim J as double
J= 123.12332
I=D
Writeline(“Integer Value of ” &I)
Print (Result)
I = 132
End Sub

Explicit Type Casting
Sub Main()
Dim I as integer
Dim J as double
J= 123.12332
D =CType(D,I)
Writeline(“Integer Value of ” &I)
Print (Result)
I = 132.12332
End Sub