Search This Blog

Tuesday, July 16, 2013

Events C#


Events in C# are based on the delegate model.
To declare an event in a class the delegate type for the event must be declared.

Events use the publisher-subscriber model.

A publisher is an object that contains the definition of the event and the delegate.
A subscriber is an object that accepts the event and provides an event handler.




Example:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public class Event
        {
            private int value;
            public delegate void EventHandler(int v);
            public event EventHandler ValueChanged;

            protected virtual void OnValueChanged()
            {
                if (ValueChanged != null)
                {
                    ValueChanged(value);
                }
            }
            public Event()
            {
            }
            public void SetValue(int n)
            {
                if (value != n)
                {
                     value = n;
                    OnValueChanged(); // raise event
                }
            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Event ev = new Event();
            ev.ValueChanged += ev_ValueChanged;
            ev.SetValue(2);
            ev.SetValue(3);
        }
        void ev_ValueChanged(int v)
        {
           MessageBox.Show("Event value : " + v.ToString()); 
        }
     }
}

 




Logger class Delegate Example

C# > Delegate

Logger class Delegate Example

Example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public class LoggerClass
        {
            public delegate void LogHandler(string message); // Declare a delegate
            public void Log(LogHandler vLogHandler)
            {
                vLogHandler("Log begin");
                // do stuff ...
                vLogHandler("Log end");
            }
        }
        static void Logger(string s)
        {
            MessageBox.Show(s);
        }
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            LoggerClass mClass = new LoggerClass();
            LoggerClass.LogHandler mLogger = new LoggerClass.LogHandler(Logger);  // create an instance of the delegate and pointing to the logging function.
            mClass.Log(mLogger); // delegate will be passed to the Log() function.
        }
    }
}




Square brackets ([]) operator C#

C# > Operators > [] Operator 

Square brackets ([]) operator is used for arrays, indexers, and attributes.

Examples
 //arrays
  int[] mList;
  mList = new int[200];

 //indexers
  System.Collections.Hashtable h = new System.Collections.Hashtable();
  h["e1"] = 1;
      
  //attributes
  [System.Serializable]
      









Monday, July 15, 2013

Abstract classes, MustInherit, MustOverride , Overrides VB NET

VB.NET > OOP > Abstract Class

Abstract classes specify members that must be implemented in the inheriting classes. Any class can inherit only from one abstract class. You we cannot create objects of an abstract class.

MustInherit
Specifies that a class can be used only as a base class and that you cannot create an object directly from it.

MustOverride  
Specifies that a property or procedure is not implemented in this class and must be overridden in a derived class before it can be used.

Overrides
Specifies that a property or procedure overrides an identically named property or procedure inherited from a base class.




Example:

Public Class Form1
  Public MustInherit Class Employee

    Private mFirstName As String
    Private mLastName As String

    Public Sub New(ByVal FirstName, ByVal LastName)
      mFirstName = FirstName
      mLastName = LastName
    End Sub

    Public Property FirstName() As String
      Get
        Return mFirstName
      End Get
      Set(ByVal value As String)
        mFirstName = value
      End Set
    End Property

    Public Property LastName() As String
      Get
        Return mLastName
      End Get
      Set(ByVal value As String)
        mLastName = value
      End Set
    End Property

    Public MustOverride Function Income() As Double
  End Class

  Public Class Teacher
    Inherits Employee

    Private mSalary As Double

    Public Sub New(ByVal FirstName, ByVal Lastname, ByVal salary)
      MyBase.New(FirstName, Lastname)
      mSalary = salary
    End Sub

    Public Property Salary() As String
      Get
        Return mSalary
      End Get
      Set(ByVal value As String)
        mSalary = value
      End Set
    End Property

    Public Overrides Function Income() As Double
      Return Salary() * 1.2
    End Function
  End Class

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim mTeacher As New Teacher("John", "Smith", 100)
    Dim sal = mTeacher.Income ' 120
  End Sub

End Class





Friday, July 12, 2013

Export RadGridView to Bitmap full Size

Telerik > Windows Forms > Export RadGridView to Bitmap

Sometimes is useful to export Telerik Rad Grid to image to its full size without regard of scroll bars.




Example:

Private Function GetImage(grd As Telerik.WinControls.UI.RadGridView)
        grd.Visible = False
        grd.Dock = DockStyle.None
        Dim w = 0
        For Each col In grd.Columns
            w = w + col.Width
        Next
        grd.Width = w
        If grd.Rows.Count > 0 Then
            grd.Height = (grd.Rows.Count * grd.Rows(0).Height)
        Else
            grd.Height = 150
        End If

        Dim bitmap As New Bitmap(grd.Bounds.Width, grd.Bounds.Height)
        grd.DrawToBitmap(bitmap, grd.Bounds)
        grd.Dock = DockStyle.Fill
        grd.Visible = True

        Return bitmap
End Function




Thursday, June 27, 2013

Alter SQL Server Examples

SQL Server > DDL > Alter

Alter Table

Modifies a table definition by altering, adding, or dropping columns and constraints.

Examples:


  • Add new column to a table

    ALTER TABLE dbo.TABLE_NAME ADD FIELD_NAME nvarchar(MAX) NULL




ALTER LOGIN

Changes the properties of a SQL Server login account. Examples:    1.Change user password       ALTER LOGIN sa WITH PASSWORD = 'your_password';    2.Enable user       ALTER LOGIN sa ENABLE;




CREATE TABLE SQL Server

SQL Server SQL Statements > CREATE TABLE

CREATE TABLE statement creates a table in a database.

Syntax

CREATE TABLE table_name
(
   column_name data_type(size),
   ...
);

Example

CREATE TABLE [dbo].[Table1](
       [ID] [bigint] IDENTITY(1,1) NOT NULL,
       [Name] [nvarchar](50) NOT NULL,
       [Password] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_users1] PRIMARY KEY CLUSTERED
(
       [ID] ASC
)
) ON [PRIMARY]
GO