Search This Blog

Thursday, December 5, 2013

AddHandler Visual Basic Example

VB.NET > Statements > AddHandler

AddHandler link an event with an event handler at run time.

Example:

1. Add event to an object

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Obj As New Class1
        ' Associate an event handler with an event
        AddHandler Obj.myEvent, AddressOf EvHandler
        Obj.Class1Event()
    End Sub

    Sub EvHandler()
        ' Handle the event
        MsgBox("EvHandler")
    End Sub

    Public Class Class1
        Public Event myEvent()
        Public Sub Class1Event()
            RaiseEvent myEvent()
        End Sub
    End Class

End Class

2. Handle events for dynamic runtime control





Enum type c# example

C# > Types > enum

Enum is s type that contains of a set of named constants called the enumerator list.
By default the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

Example:

A.

enum SomeMonths { January, February, March };
enum OtherMonths { April = 1, May }; //Enumerators can use initializers to override the default values

int v = (int)SomeMonths.January; // 0
v = (int)OtherMonths.May; // 2


B. String Enum







Long type C# Example

C# > Types > long

The long keyword is an integral type.
Range: –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Size: Signed 64-bit integer

Example:

long l = 21321321321;

long l1 = 21321321321L; // using suffix L

int i = 100L; // Error: Error 1 Cannot implicitly convert type 'long' to 'int'. An explicit conversion exists (are you missing a cast?)

int x = (int)100L; // OK








Wednesday, December 4, 2013

Serialization C#

C#  > Serialization  

Serialization  role is to save the state of an object. It converts an object into a stream of bytes. The reverse process is called deserialization.




DataContractAttribute C# example

C# > Serialization > DataContractAttribute

DataContract Attribute specifies that the type defines or implements a data contract and is serializable by a serializer.
EnumMember Attribute specifies that the field is an enumeration member and should be serialized.

Example:


namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        [DataContract]
        public enum Country
        {
            [EnumMember(Value = "USA")]
            USA,
            [EnumMember(Value = "DE")]
            Germany,
            [EnumMember(Value = "FR")]
            France,
            NotASerializableEnumeration
        }

        [DataContract (Name = "Customer")]
        public class Customer : IExtensibleDataObject
        {
            public Customer(string name,  Country cnt)
            {
                Name = name;
                Country = cnt;
            }
            private ExtensionDataObject extensionDataValue;
            public ExtensionDataObject ExtensionData
            {
                get { return extensionDataValue; }
                set { extensionDataValue = value; }
            }
            [DataMember]
            internal string Name;
            [DataMember]
            internal Country Country;
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Customer p = new Customer("Microsoft", Country.USA );
            FileStream fs = new FileStream(@"C:\1.xml", FileMode.Create);
            try
            {
                DataContractSerializer ser = new DataContractSerializer(typeof(Customer));
                ser.WriteObject(fs, p);
            }
            catch (SerializationException exc)
            {
                 MessageBox.Show(exc.Message.ToString());   
            }
            finally
            {
                fs.Close();
            }
        }
     }
}





Monday, December 2, 2013

Search for files in directories and subdirectories C#

C# > Files > Search

GetDirectories: gets the names of subdirectories in a specified directory.
GetFiles: returns the names of files in a specified directory.

Example: Search for files in directories and subdirectories

          List<string> sfiles = new List<string>();
         FileDirSearch(@"C:\\", sfiles, "*.xml");

      void FileDirSearch(string sDir, List<string> sfiles, string pattern)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, pattern))
                    {
                        sfiles.Add(f);
                    }
                    FileDirSearch(d, sfiles, pattern);
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show (exc.Message);
            }
        }






Control of Flow Keywords SQL Server

SQL Server > Control of Flow

Control of Flow keywords in SQL Server: