Monday, May 31, 2010

Difference between Assembly and Namespaces

Namespace: It is a Collection of names wherein each name is Unique.
They form the logical boundary for a Group of classes.
Namespace must be specified in Project-Properties.

Assembly: It is an Output Unit. It is a unit of Deployment & a unit of versioning.
Assemblies contain MSIL code.
Assemblies are Self-Describing. [E. g. metadata, manifest]
An assembly is the primary building block of a .NET Framework application. It is a collection of functionality that is built, versioned, and deploymed as a single implementation unit (as one or more files). All managed types and resources are marked either as accessible only within their implementation unit, or by code outside that unit.

Which namespace is used for eventlog?

Using System.Diagnostics;

'USING' keyword in C#

USING as Directive- Using keyword is used for importing namespaces to our classes.

USING Block- If you declare any unmanaged object in 'USING' block, You need not explicitly dispose that object. Once the block execution is over the dispose() will be called automatically.

What is the difference between Server.Transfer and Response.Redirect?

Server.Transfer(): Using this statement it not only redirects to the next page but also all the content of the page is transferred to next page. Data is persisted to next pages using Context.ItemCollection which keeps the page state alive.

Response.Redirect(): Using this statement the Context.Items loses the persistence to transfer the content to next page. Hence every page is treated as a seperate page.

What is the difference between a value type and a reference type?

Value types are stored on the stack and reference types are stored on the heap. Value types are primitive data types such as int, char and float. Reference types are data types such as class, interface, event and delegate.

Saturday, May 29, 2010

What are Delegates?

Delegates are like function pointers in C/C++ but with the added benefit of being type safe. You use delegates to call the methods of other objects. They are object-oriented function pointers since they allow a function to be invoked indirectly by using a reference to the function.

What is versioning in .net

.Net has an Assembly. Assembly is a single unit of deployment. Assemblies are used to maintain .dll's and .exe's. If there is a modification in the code then it is difficult to find out which was the modified code. In order to overcome this problem versioning was implemented in Assemblies. Hence versioning came into existence.

Friday, May 28, 2010

What are valid signatures for the Main functions

Static void Main(string[] args)
Public static void Main(string[] args)
Static Public void Main(string[] args)

Thursday, May 27, 2010

What is the Difference between Boxing and Unboxing

Boxing :- is Converting Value type to Reference type.
Unboxing :- is Converting Reference type to Value type

Example on Boxing and UnBoxing

namespace Boxing_vs_Unboxing
{
class Program
{
static void Main(string[] args)
{
int i = 1;
object obj = i;//Boxing
Console.WriteLine(obj.ToString());
int j = (int)obj;//Unboxing
Console.WriteLine(i.ToString());
Console.Read();
}
}
}

Difference between String vs. String builder

Difference between String vs. String builder

String - Once the string object is created, its length and content cannot be modified. This method is slower.

String builder - Once the object is created, it can able to modify length and content. This method is faster.

Example on String vs. String builder

namespace stringvsstringbuilder
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("String Example");
string str = string.Empty;
DateTime start = DateTime.Now;
Console.WriteLine("Start time" + start.ToString());
for (long i = 0; i < 100000; i++)
{
str += i.ToString();
}
DateTime stop = DateTime.Now;
Console.WriteLine("Start time" + stop.ToString());

Console.WriteLine("StringBuilder Example");
StringBuilder builder = new StringBuilder();
start = DateTime.Now;
Console.WriteLine("Start time:" + start.ToString());
for (long i = 0; i < 100000; i++)
{
builder.Append(i.ToString());
}
stop = DateTime.Now;
Console.WriteLine("Stop time:" + stop.ToString());
Console.Read();
}
}
}

Tuesday, May 25, 2010

What are Objects

Objects: Object is an instance of a Class.
  • Object occupies normal variable memory.
  • Object is physical representation and we can destroy object memory.
  • Object having the lifetime, start time is Constructor and end time is Destructor.

What are Classes

Classes: Classes are like datatype on which object is created. It is like a template of an Object.
  • Class is a user defined data type.
  • Classes are like structures in C.
  • Class occupies static variables memory and it is a logical representation.
  • Memory is continuous until closing the application.
  • Classes doesn't have lifetime.

Script for exporting the results in HTML Format

Dim FSO, F, WR
Set FSO = CreateObject("Scripting.FileSystemObject")
IF(FSO.FileExists("C:\log.html"))Then
else
FSO.createTextFile("C:\log.html")
End If
html_logger = "C:\log.html"
environment.value("htmllogfile") = html_logger
set f = FSO.getfile("C:\log.html")
set wr = f.OpenAsTextStream(8,-2)
wr.writeline "<A Name="&chr(34)&"Head"&chr(34)&"><h1>Automation Status Summary Report </h1></A>"
wr.writeline <br><br><br><h5><u>Release</u></h5><br>"
wr.writeline "<h5><u>Build</u></h5><br>
wr.writeline "<h5><u> URL </U></h5><br><br>"
wr.writeline "<hr>"
wr.writeline <style type=text/css>"
wr.writeline "body {"

Saturday, May 22, 2010

Access Specifiers

Scope of the variables

  • Public -- Public access modifier can be set to a class / method / property to make it visible to any class / any assembly.
  • Private -- Private can be set to those fields which are within the class and they are used to hide from outside of the class.
  • Protected -- Protected is used for immediate child classes.
  • Internal -- Internal is used specifically for within the assembly.
  • Protected Internal -- It is used for within the assembly and assembly that inherits this assembly (Shared Assembly).
  • Static @ Modifier -- Static member belongs to the class and as such we don't need to create an object to access it.
  • Non-static member - Default, can be accessed only via an object of the class.
    Real time: WriteLine() is a static function in Console as we did not create an object that looks like Console to access it.

Destructors

  • It takes name of the class but has (~) operator.
  • No access specifier.
  • Decision to call the destructor is made by a program within C# called the garbage collector.
  • Cannot overload a destructor.

Constructors

  • Constructors are similar to methods.
  • They always take the name of the class but no return type.
  • Constructors cannot be called/ inherited can only be invoked by 'new' operator.
  • They allocate memory to all of class/member variables to keep default values in it.
  • Class has one single constructor & it is private and can never be instantiated.
  • Static Constructors -- Used for initializing only static members of a class..These are invoked very first time class is loaded in memory. No args. No Access Modifiers.
  • Non-Static Constructors -- Used for initializing static & non-static methods. Invoked every time a new object is created.

Example on Constructor:

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class add
{
public add(int a, int b)
{
int totsum;
totsum = a + b;
MessageBox.Show(totsum.ToString());
}
}

private void button1_Click(object sender, EventArgs e)
{
add emmu = new add(5, 3);
}
}

Differences between 'const' and 'read-only' in C#

ConstRead-Only
  • Can't be static.
  • Value is evaluated at compile time
  • Initialized at declaration only
  • Can be either instance-level or static.
  • Value is evaluated at run time.
  • Can be initialized in declaration or by code in
    the constructor.
Example on Const and Read-Only

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class constvalue
{
public const int conval = 10; //constant initialization
public readonly int tempvalue = conval; //read-only initialization
public constvalue(int rvalue)
{
tempvalue = tempvalue + rvalue; //read-only initialization in constructor
MessageBox.Show(tempvalue.ToString());
}
}

private void button1_Click(object sender, EventArgs e)
{
constvalue objconstvalue = new constvalue(100);
}

}

What is Data Abstraction:

Data Abstraction gives complete information about class; we can get information about data members, member functions, properties, events, collections, etc.

Data Encapsulation

Data Encapsulation is the process of combining data and functions into a single unit called class. By this method one cannot access the data directly. Data is accessible only through the functions present inside the class.

  • Data Encapsulation gave rise to the important concept of data hiding.
  • Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required
  • Encapsulation gives us maintainability, flexibility and extensibility to our code.
  • Encapsulation is the technique or process of making the fields in a class private and providing access to the fields using public methods.

Example on Data Encapsulation

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class emp
{
int sal, bonus, ts;
public void tsal()
{
sal = 5000;
bonus = 300;
ts = sal + bonus;
MessageBox.Show(ts.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
emp e1 = new emp();
e1.tsal();
}
}

You concentrate on public method tsal(), change this method to private. here comes encapsulation concept hiding methods from outside world.

DataBinding in ASP.Net

Friday, May 14, 2010

Connecting with .mdf database files

Working with File System Object 1

'Get the FireFox Version using VBScript
Dim verNum
Set objFSO = CreateObject("Scripting.FileSystemObject")
verNum= objFSO.GetFileVersion("C:\Program Files\Mozilla Firefox\firefox.exe")
MsgBox("Firefox Version"&verNum)

Thursday, May 13, 2010

File System Object Vs Folders

Size a Folder
Copy a Folder
Move a Folder
Delete a Folder
File Count in a folder
SubFolder Count in a Folder
Add a New Folder
Filenames in a folder
SubFoldernames in a Folder
'Syntax:
Copy
FolderObject.Copy(destination[, overwrite])
MoveFolder
FolderObject.MoveFolder(source, destination)
Delete a Folder
FolderObject.DeleteFolder(folderspec[, false])

'Size a Folder
Dim SourceFolder, oFSO
SourceFolder = "C:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.GetFolder(SourceFolder)
Msgbox "Size of the Folder "&oFSO.Size&" bytes" 'Display the size of the folder

'Copy a Folder
Dim SourceFolder, oFSO
SourceFolder = "C:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CopyFolder SourceFolder, "D:\"

'Move a Folder
Dim SourceFolder, DestinationFolder, oFSO
SourceFolder = "C:\IM"
DestinationFolder = "D:\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.MoveFolder SourceFolder, DestinationFolder

'Delete a Folder
Dim SourceFolder, oFSO
SourceFolder = "D:\IM"
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.DeleteFolder SourceFolder

'Get the File Count in a folder
Set FSO = CreateObject("Scripting.FileSystemObject")
FolderPath = "C:\Documents and Settings\"
Set TestFolder = FSO.GetFolder(FolderPath)
Set Files = TestFolder.Files
MsgBox("Number of Files in:"&vbCrLf&FolderPath&vbcrlf&Files.Count)

'Get the SubFolder Count in a Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TestFolder = FSO.GetFolder("C:\Documents and Settings")
Set SubFolders = TestFolder.SubFolders 'Returns the collection of subfolders
Msgbox(SubFolders.Count)

'Add a New Folder
Sub AddNewFolder(FolderPath, FolderName)
Dim FSO, f, fc,nf
Set FSO = CreateObject("Scripting.FileSystemObject")
Set f = FSO.GetFolder(FolderPath)
Set fc = f.SubFolders
If FolderName <> "" Then
Set nf = fc.Add(FolderName)
Else
Set nf = fc.Add("New Folder")
End If
End Sub
AddNewFolder "C:\",""

'Filenames in a Folder
Folderpath = "C:\MyFolder"
Dim FSO, oFolder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(FolderPath)
Files = oFolder.Files 'Gets the collection of files in a folder
For each file in Files
Msgbox File.Name
Next

'SubFoldernames in a Folder
Folderpath = "C:\MyFolder"
Dim FSO, oFolder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(FolderPath)
Folders = oFolder.SubFolders 'Gets the collection of subfolders in a folder
For each folder in Folders
Msgbox folder.Name
Next

File System Object Vs Drives

Total Number of Drives
Drive Information
Drive Name of a specified file path
Drive is Ready or Not

'Total Number of Drives in your System
Set FSO = CreateObject("Scripting.FileSystemObject")
Set DriveCollection= FSO.Drives
'A collection of drive objects
MsgBox("Total Number of Drives available in your system: "&DriveCollection.Count)

'Drive Information in your System
Set FSO = CreateObject("Scripting.FileSystemObject")
intDrive = 0
For Each Driver in FSO.Drives
Msgbox "Name of the Drive :"&Driver.DriveLetter
Select Case Driver.DriveType
Case 0: strDriveType = "Drive type cannot be determined"
Case 1: strDriveType = "Removable Drive"
Case 2: strDriveType = "Fixed Drive"
Case 3: strDriveType = "CD-ROM"
Case 4: strDriveType = "RAM Disk"
End Select
msgbox "Type of the Drive : "&strDriveType
If(Driver.DriveType = 2)Then
msgbox "Total Size of the Specified Drive: "&Driver.Totalsize
msgbox "Free Space available in the Drive: "&Driver.FreeSpace
msgbox "Available Space in the Drive: "&DriverAvailableSpace
msgbox "File System of the specified Drive: "&Driver.FileSystem
'Available File Systems are FAT, NTFS and CDFS
msgbox "Serial Number of the specified Drive: "&Driver.SerialNumber
msgbox "Share Name of the specified Drive: "&Driver.ShareName
msgbox "Volume Name of the specified Drive: "&Driver.VolumeName
End If
intDrive = intDrive + 1
Next

'Drive Name of a specified file path
DrvPath = "D:\debugnew.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set GetDriveInfo = fso.GetDriveName(drvPath)
Msgbox ("Name of the Drive :"&GetDriveInfo)

'Drive is Ready or Not
DrvPath = "D:\debugnew.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set GetDriveInfo = fso.GetDrive(fso.GetDriveName(drvPath))
If GetDriveInfo.IsReady Then
Msgbox "Drive is Ready"
Else
Msgbox "Drive is Not Ready"
End If

Tuesday, May 4, 2010

Comments

Comments

Comments in VBScript are declared so that code would be easier to understand and navigate. Comments can be placed anywhere within VBScript source code. In VBScript you can have only single line comments, no multi-line comments.

Example:
\'This is a single line comments
\'
'This is another single line comment

CoolTuts - JavaScript - JavaScript Comments

JavaScript single line Comments
Comments can be added JavaScript, to make the code more readable.
Single line comments start with //.

JavaScript multiple line Comments
Multi line comments start with /* and end with */.