Wednesday, March 30, 2011

Types of valid ENUMERATORS

In c# we generally go for the enumerators for the sake of having a better code readability and to achieve code standardization. Below are the various types of enums, from which only few are valid.


Type1:
----------------------------------------------------------------------
public enum enmPosition
{
Scroll=111,
dwn=12,
ug=11,
ch=10
};




Declaring a enum and then giving some elements for it and then assigning some numeric values for that. In this case its totally valid.

From above if you are going to access the enum values, then if we say: enmPosition.Scroll => this will return 111 as the value.



Type2:
-------------------------------------------------------------------

public enum enmPosition1 : int
{
Scroll = 111,
dwn = 12,
ug = 11,
ch = 10
};



Here though we are inheriting the enum from the int, the values can be accessed same as above, but the inheritance is mostly for the sake of the code clarity and redability.

This is also valid.



Type3:
------------------------------------------------------------------

public enum enmPosition2
{
Scroll,
dwn,
ug,
ch
};


In the above enum we can see that there are no user defined values attached or assigned to the enum elements(Scroll, dwn,ug,ch). But the enum has got the ability to assign the values to the elements.

In the below code we get the values a scroll=0; dwn=1;ug=2;ch=3. This is valid.



Type4:
---------------------------------------------------------------------

// Defining a string type enum
public enum enmStringType : string
{
India = "India is great",
XyxCtr = "This contry is not known"
}



In the above we declared a enum of type string, as the enums can return only the numeric types, this type of enum can't be used. So this is not valid.



Type4 -Extension:
-------------------------------------------------------------

In order to use enums which returns the will return the strings can be done in the following way:

first we need to create a custom class which inherits the System.Attributes.



public class StringValueAttribute : System.Attribute
{

private string _value;

public StringValueAttribute(string value)
{
_value = value;
}

public string Value
{ get { return _value; } }

}




In the above we are setting a parameterized constructor and a property for a private variable.



then change the enum to this


public enum Test : int
{
[StringValue("a")]
Xyz = 1,
[StringValue("b")]
Something = 2
}





now add this new method in your class, which is responsible for accessing the value attributes and getting the actual value from the enum.

public static string GetStringValue(Enum value)
{
string output = null;
Type type = value.GetType();

//Check first in our cached results...

if (_stringValues.ContainsKey(value))
output = (_stringValues[value] as StringValueAttribute).Value;
else
{
//Look for our 'StringValueAttribute'

//in the field's custom attributes

FieldInfo fi = type.GetField(value.ToString());
StringValueAttribute[] attrs =
fi.GetCustomAttributes(typeof(StringValueAttribute),
false) as StringValueAttribute[];
if (attrs.Length > 0)
{
_stringValues.Add(value, attrs[0]);
output = attrs[0].Value;
}
}

return output;
}



and for performance point of view, we are using the HashTable to store the data.
so add this at the starting of the class.


private static Hashtable _stringValues = new Hashtable();




Then when we try to access the enum, we get the value as well as the attribute (the string value).


GetData:
-------------------------------------------------------------
string tEnumValue=GetStringValue(Test.Something);

Sunday, March 27, 2011

WCF (Windows communication Foundation) tutorial starting

Hi, guys

Are you looking for any quick way for get hands on the WCF of the microsoft WCF!!!!!!
Dont worry I am here for such thing to happen.
This is the stating post of the WCF, and first I am going to give you a brief idea regarding the WCF.


What is WCF????
WCF stands for WINDOWS COMMUNICATION FOUNDATION, it is based in SOA (SERVICE ORIENTED ARCHITECTURE). WCF is totally a revolutionized SOA platform for easy cross platform communication. It has got a lot more features than webservices.

DEF: WCF is a runtime environment for developing, building, configuring and hosting the network based SOA application. It is a package or a combination of webservices, remoting, MSMQ, com+.






There are lot of terminologies to come across in WCF, which will be covered in the next posts...........