Saturday, October 27, 2007

Using Objects of VB.NET & C#

Using Objects

In vb.net & c# language Using Objects are differ. Code of Using Objects of vb.net & c# language.

____________________________________________________________________

VB.NET

Dim hero As SuperHero = New SuperHero
' or
Dim hero As New SuperHero

With hero
.Name = "SpamMan"
.PowerLevel = 3
End With

hero.Defend("Laura Jones")
hero.Rest() ' Calling Shared method
' or
SuperHero.Rest()

Dim hero2 As SuperHero = hero ' Both reference the same object
hero2.Name = "WormWoman"
Console.WriteLine(hero.Name) ' Prints WormWoman

hero = Nothing ' Free the object

If hero Is Nothing Then _
hero = New SuperHero

Dim obj As Object = New SuperHero
If TypeOf obj Is SuperHero Then _
Console.WriteLine("Is a SuperHero object.")

' Mark object for quick disposal
Using reader As StreamReader = File.OpenText("test.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
Console.WriteLine(line)
line = reader.ReadLine()
End While
End Using

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

C#

SuperHero hero = new SuperHero();


// No "With" construct
hero.Name = "SpamMan";
hero.PowerLevel = 3;

hero.Defend("Laura Jones");
SuperHero.Rest(); // Calling static method



SuperHero hero2 = hero; // Both reference the same object
hero2.Name = "WormWoman";
Console.WriteLine(hero.Name); // Prints WormWoman

hero = null ; // Free the object

if (hero == null)
hero = new SuperHero();

Object obj = new SuperHero();
if (obj is SuperHero)
Console.WriteLine("Is a SuperHero object.");

// Mark object for quick disposal
using (StreamReader reader = File.OpenText("test.txt")) {
string line;
while ((line = reader.ReadLine()) != null)
Console.WriteLine(line);
}

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

Note: This code applicable for both desktop and web application.

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

No comments: