Search This Blog

Tuesday, August 12, 2014

Typeof C# example

C# > Keywords > typeof

Obtains the System.Type object for a type.


Example:



Type type = typeof(T);







Wednesday, July 30, 2014

SerialPort Class Example

C# > IO  > Ports > SerialPort

SerialPort controls a serial port file resource. 

Example

1. Gets all serial port names for the current computer.

List<String> serialPorts = new List<String>();
foreach (String portName in System.IO.Ports.SerialPort.GetPortNames())
{
  serialPorts.Add(portName);

}





System.IO.Ports Namespace C#

C# > IO  > Ports

System.IO.Ports contains classes for controlling serial port.






Thursday, July 24, 2014

CAST SQL Server Example

SQL Server > Built-in Functions > CAST

CAST converts an expression of one data type to another.


CAST ( expression AS data_type )

Examples







Wednesday, July 16, 2014

ROWCOUNT SQL Server Example

SQL Server > Built-In Functions > @@ROWCOUNT

Returns the number of rows affected by the last statement.

Example:

UPDATE Person
SET Age = 31
WHERE Id = 1
IF @@ROWCOUNT = 0
    PRINT 'No rows were updated';




Tuesday, June 24, 2014

Public keyword C# Example

C# > Keywords > public

Public is the most permissive access level. 

It is no restrictions on accessing public members.






Example


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 WindowsFormsApplication12
{
    public partial class Form1 : Form
    {
        public class Person
        {
            public string Name;
            public int Age;
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Person p = new Person();
            p.Name = "John";
            p.Age = 35;
        }
    }
}




Wednesday, June 18, 2014

Implement Strings Enum in C#

C# > Types > Enum > String Enum


Implement Strings Enum in C#









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

namespace EnumString
{
    public partial class Form1 : Form
    {
        public enum Cars
        {
            [Description("Audi")] Audi,   
            [Description("Bmw")] Bmw,   
            [Description("Land Rover")] LandRover
        }
        public static string GetCarDescription(Enum car)
        {
            FieldInfo fi = car.GetType().GetField(car.ToString());
            DescriptionAttribute[] da = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (da != null && da.Length > 0)       
                return da[0].Description;
            else      
                return da.ToString();
        }
        public static IEnumerable EnumToList()
        {   
            Type type = typeof(T);   
            Array vals = Enum.GetValues(type);   
            List valList = new List(vals.Length);   
            foreach (int val in vals)   
            {
                valList.Add((T)Enum.Parse(type, val.ToString()));   
            }
            return valList;
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Cars car in EnumToList<Cars>())
            {
                comboBox1.Items.Add(GetCarDescription(car));
            }
        }
    }
}