Search This Blog

Monday, December 2, 2013

Rank DataTable with LINQ C# Example

C# > LINQ > Rank

Example: Using LINQ to implement RANK function with DataTable.

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

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public  DataTable RankDt(DataTable dt, string fld)
        {
            var rankDt = (from row in dt.AsEnumerable()
                            orderby row.Field<int>(fld) descending
                            select row).CopyToDataTable();

            rankDt.Columns.Add("Rank");
            int rank = 1;
            for (int i = 0; i < rankDt.Rows.Count - 1; i++)
            {
                rankDt.Rows[i]["Rank"] = rank;
                if (rankDt.Rows[i][fld].ToString() != rankDt.Rows[i + 1][fld].ToString())
                    rank++;
            }
            rankDt.Rows[rankDt.Rows.Count - 1]["Rank"] = rank;
            return rankDt;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Product");
            DataTable dt = ds.Tables[0];
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("quantity", typeof(Int32));
 
            DataRow dr = dt.NewRow();
            dr[0] = "Product 1" ;
            dr[1] = 500;
            dt.Rows.Add(dr);
            dr = dt.NewRow();

            dr[0] = "Product 2";
            dr[1] = 500;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "Product 3";
            dr[1] = 50;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = "Product 4";
            dr[1] = 100;
            dt.Rows.Add(dr);

            dataGridView1.DataSource = RankDt(dt, "quantity");
        }
     }
}

 





Thursday, November 28, 2013

Visual Basic Statements

VB.NET > Statements


Dim
End


RaiseEvent ReDim REM RemoveHandler
Resume Return Select...Case Set
Stop Structure Sub SyncLock
Then Throw Try...Catch...Finally Using
While...End While With...End With Yield




Generics and Arrays Example in C#

C# > Generics > Generics and Arrays

Example: Use a single generic method that takes an IList<T> input parameter and iterate through both a list of integers and an array of string.

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


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string[] str = { "0", "1", "2", "3", "4" };
            List<int> intL = new List<int>();
            for (int x = 0; x < 5; x++)
            {
                intL.Add(x);
            }
            ProcessList<string>(str);
            ProcessList<int>(intL);
        }
        static void ProcessList<T>(IList coll)
        {
            foreach (T item in coll)
            {
                MessageBox.Show(item.ToString());
            }
        }
    }
}





Wednesday, November 27, 2013

Form VB.NET

VB.NET > Form

Form

Is a representation of any window displayed in an application.

Type
  • standard
  • tool
  • borderless
  • floating
Example

Create dialog box in runtime

Dim form1 As New Form()

Dim btnYes As New Button()
Dim btnNo As New Button()
btnYes.Text = "Yes"
btnYes.Location = New Point(5, 5)
btnNo.Text = "No"
btnNo.Location = New Point(btnYes.Left, btnYes.Top + 30)

form1.Controls.Add(btnYes)
form1.Controls.Add(btnNo)

form1.Text = "Confirm Dialog Box"
form1.FormBorderStyle = FormBorderStyle.FixedDialog
form1.MaximizeBox = False
form1.MinimizeBox = False
form1.AcceptButton = btnYes
form1.CancelButton = btnNo
form1.StartPosition = FormStartPosition.CenterScreen

form1.ShowDialog()





Object-Oriented Programming VB.NET

VB.NET > Object-Oriented Programming VB.NET

Visual Basic .NET has been entirely rewritten to be fully object-oriented and everything in Visual Basic .NET can be treated as an object.

OOP language:
  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
Examplea




System Namespace VB.NET

VB.NET > System namespace

System namespace in VB.NET contains fundamental classes and base classes.

String






Tuesday, November 26, 2013

Create CDATA section XML C#

C# > XML > XmlDocument > CreateCDataSection

CreateCDataSection creates an XmlCDataSection.
CDATA is a section used to quote or escape blocks of text to keep that text from being interpreted as markup language.

Example:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<product ID='1'></ product>");
//Create a CData section
XmlCDataSection CData;
CData = doc.CreateCDataSection("Size is 10/55");

//Add the new node to the document
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
doc.Save("C:\\Products.xml");

Result:
  <![CDATA[Size is 10/55]]>
</product>





Monday, November 25, 2013

Short type C# Example

C# > Types > short

The short keyword is an integral type.
Range: -32768 to 32767

Size: Signed 16-bit integer

Example:

short k = 32767; // ok
k = 32768; // Error Constant value '32768' cannot be converted to a 'short'




Struct in C# Example

C# > Types > Struct

A struct type is a value type and is typically used for representing lightweight objects (color, zip, point).
  • cannot inherit from another struct or class
  • can be instantiated without using a new operator.
  • can implement an interface
  • can contain fields, constructors, methods, operators and events, but
  • if many members are required  you should make your type a class instead
Note:  Struct might be more efficient than a class in some scenarios. Using array of thousand of objects will be allocate more memory for referencing each object, so in this case, a struct would is more efficient and less expensive.






Example

        public struct strColor
        {
            public int r, g, b;
            public strColor(int p1, int p2, int p3)
            {
                r = p1;
                g = p2;
                b = p3;
            }
        }
       // Initialize using default and parameterized constructor
       strColor color1 = new strColor();
       strColor color2 = new strColor(10, 20, 30);
      // Declare object. It creates object without using the new  operator
       strColor color;
      //Initialize
       color.r = 10;
       color.g = 20;
       color.b = 30;