StatCounter

View My Stats

Friday, February 6, 2009

Name value collection in C#

Name value collection in C#


The name value collection is same like the HashTable.


We can set the Object value and the Key to that particuar Object.

But the HashTable dont allow theduplicate keyws.

But the NameValueCollection alloes duplicate Keys. as well as it's provide the fast

way of access to it.


Example HashTable:(It'a raise an error because of the duplicate keys "a"
________________________________________________________________________
Hashtable Ht = new Hashtable();
Ht.Add("a", "Object1");
Ht.Add("b", "Object2");
Ht.Add("a", "Object1 Clone");

Console.WriteLine(Ht["b"]);
Console.WriteLine(Ht["a"]);





Example For NameValueCollection:
________________________________
NameValueCollection col1=new NameValueCollection();
col1.Add("a","Today");
col1.Add("a","Yesterday");
col1.Add("a","Tomorrow");
col1.Add("b","Mango");
Console.WriteLine((col1.GetValues(col1.Keys[0])[0]).ToString());

This collection is based on the NameObjectCollectionBase class.
However, unlike the
NameObjectCollectionBase, this class stores multiple string values under
a single key.

Simple Observer Design pattern example in C#

Simple Observer Design pattern example in C#


The Reference URL is:
http://www.java2s.com/Code/CSharp/Design-Patterns/ObserverPatternDemo.htm
============================================================================
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleExample.Design_Pattren
{
public delegate void ChangedEventHandler(object sender,EventArgs e);
public class Subject
{
private string _data;
public event ChangedEventHandler Changed;

#region Default Constructor
public Subject()
{
}
#endregion


public string Data
{
get { return _data; }
set
{
_data = value;
this.OnChanged(EventArgs.Empty);

}
}

protected virtual void OnChanged(EventArgs e)
{
if (Changed != null)
this.Changed(this, e);
}

}

public class Observer
{
private Subject Subjectdata;
private string ObserverName;

public Subject ObservedObject
{
get { return Subjectdata; }
set { Subjectdata = value; }
}

private void DataChanged(object sender, EventArgs e)
{
Console.WriteLine("Notification "+ObserverName+" "+Subjectdata.Data);
}

public void Attach()
{
Subjectdata.Changed += new ChangedEventHandler(DataChanged);
}
public void Detach()
{
Subjectdata.Changed -= new ChangedEventHandler(DataChanged);
}

public Observer(string name)
{
this.ObserverName = name;
}
}

public class MainClass
{
public static void Main()
{
Subject sub = new Subject();
Observer observerA = new Observer("Observer A");
Observer observerB = new Observer("Observer B");
observerA.ObservedObject = sub;
observerB.ObservedObject = sub;
observerA.Attach();
observerB.Attach();
sub.Data = "1";
observerA.Detach();
//observerA.Detach();
sub.Data = "2";
sub.Data = "3";
Console.ReadKey();
}

}

}
===========================================================================

What is CAS(Code Access Security)

What is CAS(Code Access Security)


Code Access Security in managed code prevents code loaded from another server from performing
certain destructive actions. If your application calls out to unmanaged code loaded from another
server, you won't get that protection.

Aggregation,Association,Active Object,Passive object

Aggregation,Association,Active Object,Passive object


Aggregation:
____________

-One Object should be composed of more than 1 or 2 objects.

-An object composed of 2 or more other object.



For Example take a Car. Eventhrough the car does not having

the wheel The car called to become as Car.

Here the wheel object belngs to the group of car object.


->InAggregation the Object associated with on collection Objects.


->For Example:Purchase object associated with the Item Collection

Object.


Association:
_____________


Their will be a permanent relationship with 2 objects r called
as association.

For Example take a Humam body. If a heart of a human body get
diseased the human body will become body.


Active Object:
______________
An Object become Spontaneously invoke itself is alled as Active

Example: Clock...The time has changed continuously


Example: client

Passive object:
_______________

An Object rewuires an external resources or request itself to

activate itself.

Example: Calling Bell.

Some of other Objects:
_______________________

1.Monolithic Object
2.composite Object
(2.0)The composite object get's divided into 2 categories

they r
(2.1)homogeneous composite Object

(2.2)Heterogeneous composite object

Class,Inner Class(or)Nested class Example in C#

Class,Inner Class(or)Nested class Example in C#


using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleExample
{
public class OuterClass
{
public void OuterClassFunction()
{
Console.WriteLine("I am in Outer Class Function");
}
//Reinitialize the class

public InnerClass abc()
{
return new InnerClass();

}

public class InnerClass
{

public OuterClass OutC()
{
return new OuterClass();
}

public string PublicInner;
private string privateInner;

public InnerClass()
{

}

public void InnerClassFunction()
{
Console.WriteLine("I am in InnerClass");

}

void PrivateFunction_InnerClass()
{
Console.WriteLine("I am a Private function Of InnerClass");
}
}
}
}
________________________________________________________________________

For the Above example the Instance are named as abc(),OutC()


To Call an instance Methods from the main function:


static void Main(string[] args)
{
//Program Obj = new Program();
//Obj.ReadAssembly();
OuterClass.InnerClass Obj = new OuterClass.InnerClass();
Obj.OutC().OuterClassFunction();
Console.ReadLine();

}

__________________________________________________________________
Nested Class and Ordinary Class Explainations.



A nested class is declared in the same manner as a normal class declaration. The difference is that a nested class has access to all of the available modifiers.

The this keyword reference in the inner class only holds a reference to the inner class. Data members of the outer class are not accessible using the this reference in the inner class. If this is needed, pass a reference of the outer class into the constructor of the inner class.

static members of the outer class are available in the inner class irrespective of the accessibility level.

ISerializable implementation example

ISerializable implementation example


Introduction

Some times, we need to record data according to a given object into a file in order to either stock them or to send them via network, and such task are very needed especially in the asp or the distributed applications case. This kind of task is called serialization. There are several methods to do that, but never the less, I give one alternative among them as a part of this article. This method consists on implementing the ISerializable interface.

Let's consider a class who's called Person and here is its description:



Figure 1

The Person class code is as follow:

public class Person

{

private string _FirstName;

private string _LastName;

public string FirstName

{

get { return _FirstName; }

set { _FirstName = value; }

}

public string LastName

{

get { return _LastName; }

set { _LastName = value; }

}

public Person()

{

}

public Person(string FirstName, string LastName)

{

this.FirstName = FirstName;

this.LastName = LastName;

}
}

To serialize objects issued from this class, we should implement the ISerializable interface; this last one has a method ISerializable.GetObjectData that it should be implemented.

This is how to implement the ISerializable interface:

public class Person:ISerializable

{

private string _FirstName;

private string _LastName;

public string FirstName

{

get { return _FirstName; }

set { _FirstName = value; }

}

public string LastName

{

get { return _LastName; }

set { _LastName = value; }

}

public Person()

{

}

public Person(string FirstName, string LastName)

{

this.FirstName = FirstName;

this.LastName = LastName;

}

void ISerializable.GetObjectData(SerializationInfo oInfo, StreamingContext oContext)

{

oInfo.AddValue("FirstName", this.FirstName);

oInfo.AddValue("LastName", this.LastName);

}

}

The GetObjectData ( ) method hasn't to be marked as public otherwise an error indicates that the modifier public in not valid for this item will be generated.

Dynamic Fill Combobox values through javascript

Dynamic Fill Combobox values through javascript


Examople:

Call the fuction at the button click event

function DynamicComboAdd()
{
var Obj=document.getElementById('DropDownList1');
Obj.options.length=2;
Obj.options[0].text='hi';
Obj.options[0].value='hi';
Obj.options[1].text='hii';
Obj.options[1].value='hii';
return false;
}

Simple XML Text Reader Example in C# Console Appln

Simple XML Text Reader Example in C# Console Appln


using System;
using System.Xml;

namespace ReadXMLfromFile
{
///


/// Summary description for Class1.
///
class Class1
{
static void Main(string[] args)
{
XmlTextReader reader = new XmlTextReader ("books.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine (reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
Console.ReadLine();
}
}
}

How to hook the IIS

How to hook the IIS


To hook the iis in our system we need to monitor the w3wp.exe in that concern system.


if u hook this w3wp.exe file u can able to monitor what are all the operations are undergone in the

IIS while loading a particular page

Use of Private and Public

Use of Private and Public


In Normal .Net Keep sight on using private and public variab HFle (or) property or anything else

carefully.It will al so degrade the system performance.

By default all the public properties/variable/anythings are XML serialized.So whenever you want to use it globally declare it globally.(or) else declare it as as a private property.

Through this we can reduce the time to take for serialization.