Wednesday, September 14, 2011

Explicit tutorial c#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Eexplicit
{
    interface IBike
    {
        void Color();
    }

    interface ICar
    {
        void Color();
    }


    class AutoMobileColor :  IBike,ICar
    {
        public void Color()
        {
            Console.WriteLine("Default Automobile color");
        }

        #region IBike Members

        void IBike.Color()
        {
            Console.WriteLine("Bike Interface");
        }

        #endregion

        #region ICar Members

        void ICar.Color()
        {
            Console.WriteLine("Car interface");
        }

        #endregion
    }

    class Program
    {
        static void Main(string[] args)
        {
            AutoMobileColor col = new AutoMobileColor();
            col.Color();
            IBike ibike = (IBike)col;
            ibike.Color();
            ICar icar = (ICar)col;
            icar.Color();
            Console.Read();
        }
    }
}

Friday, August 19, 2011

?? operator in c# || null-coalescing

Hi,
This little comment on ?? operator in c# will give an idea how to use it.


You cannot assign NULL to any value type in c#
Is there any way to assign null to Int.The answer is NO.
For example:
           int x = NULL; //Error as you cannot assign null to a non-nullable type
instead you can use like this,
           int? x = NULL; //Runs Without error


What happens if x is assign to a variable of type int?
          int y = x; //Error
          int y = (int)x; //Error
Even if i cast it to int it wont allow to set the null to y,


Now the ?? comes into he picture.


        int y = x  ??  5;
Here,the value for x is checked (if x is null then it will assign 5 to y);
        now y will contain 5;
Suppose if i want to set the value of x you can do it ,Just simply assign x = 90;
and now,
        int y = x  ??  5;
Here the value for x is not null so the value of x is assigned to the y .
        now y will contain 90;



Tuesday, August 2, 2011

Sizeof() in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataTypes
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine();
Console.WriteLine("Size of Int " + sizeof(int));
Console.WriteLine("Size of Float " + sizeof(float));
Console.WriteLine("Size of Double " + sizeof(double));
Console.WriteLine("Size of Decimal " + sizeof(decimal));
Console.WriteLine("Size of Char " + sizeof(char));
//Console.WriteLine("Size of String " + sizeof(string)); -- can't do this because string is of unknown size
}
}
}

Sunday, July 24, 2011

Reverse the given number

Hi,
Here is the logic to reverse the given number
public
{


temp = num;

{
rev = rev * 10 + temp % 10;
temp = temp/10;
}

}
int ReverseNumber(int num)int temp = 0;int rev = 0;while (temp != 0)return rev;

Monday, July 4, 2011

adding a multiline textbox in winforms


Hi
1. Here is the code to add a multiline textbox which accepts return,tab.
2. First drag and drop a textbox from the toolbox.

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

namespace mltext
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            textBox1.Multiline = true;

            //// setting height and width of textbox.
            textBox1.Width = 150;
            textBox1.Height = 80;
           
            //// Add vertical scroll bars to the TextBox control.
            textBox1.ScrollBars = ScrollBars.Vertical;

            //// Allow the RETURN key to be entered in the TextBox control.
            textBox1.AcceptsReturn = true;

            //// Allow the TAB key to be entered in the TextBox control.
            textBox1.AcceptsTab = true;

            //// Set WordWrap to true to allow text to wrap to the next line.
            textBox1.WordWrap = true;
        }

    }
}

Saturday, June 18, 2011

Enum usage in switch case

Below is the code to use instance of a class to be used in switch case


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

namespace EnumFormApplication
{
    public partial class Form1 : Form
    {
        enum date
        {
            sat,
            sun,
            mon,
            tue,
            wed,
            thu,
            fri,
        };

        public Form1()
        {
            InitializeComponent();

            comboBox1.Items.Add(date.sun);
            comboBox1.Items.Add(date.mon);
            comboBox1.Items.Add(date.tue);
            comboBox1.Items.Add(date.wed);
            comboBox1.Items.Add(date.thu);
            comboBox1.Items.Add(date.fri);
            comboBox1.Items.Add(date.sat);

            comboBox1.SelectedIndex = 1;
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

        bool IsHoliday(date date)
        {
            switch (date)
            {
                case date.sun:
                case date.sat:
                    {
                        return true;
                    }

                default:
                    {
                        return false;
                    }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            date obj = (date)comboBox1.SelectedItem;

            if (IsHoliday(obj))
            {
                textBox1.Text = "holiday";
            }
            else
            {
                textBox1.Text = "workingday";
            }
        }
    }
}