Monday, November 10, 2014

C# Essentials


මොකක්ද මේ C# ?

ගොඩක් දෙනෙක් Java ගැන කියපු ගමන් අනිත් අය කියන්නෙ  කියන්නෙ "නෑ නෑ C# තමා හොද Java වලට වඩා"

තව කට්ටියක් C# ගැන කියපු ගමන් අනිත් අය කියන්නෙ කියන්නෙ "නෑ නෑ Java තමා හොද C#වලට වඩා"

:D :D :D

ගවේෂකනම් Java , C# දෙකටම කැමතියි :D

අපි Java Tutorial ගැන බලපු නිසා දැන් C# ගැනත් බලමු

C# කියන්නෙ Microsoft ලගෙ Language එකක් . .NET FRAMEWORK එක මත පදනම් වු Object Oriented වු තවත් පරිගනක භාෂාවක්.

 [ multi-paradigm programming language encompassing strong typingimperative,declarativefunctionalgenericobject-oriented (class-based), and component-oriented programming ]

අපි මෙහිදී C# වල සාමාන්‍යයෙන් දැනගෙන සිටීම අත්‍යාවශය යැයි සැලකෙන දේ පමණක් කතා කරනු ලබනවා.


.NET FRAMEWORK

.NET Framework පහත පරිදි Layers ගනන‍ාවකින් යුක්ත වේ.

.NET හි Execution වීමේ පටිපාටිය පහත පරිදි වේ





C# With SQLSERVER

මෙහිදී database එකකට Data Insert, Edit , Update සහ Delete වීම සම්බන්දව සලකාබලමු

පලමුව Windows Form Application එකක් Create කරගන්න



ඉන්පසු පහත සදහන් Designs ඒ ආකාරයටම ඔබද නිරිමාණය කරගන්න

Form1  මෙලෙසද (CRUD Opertaion සහ DataGrid එකක් Button ලෙස)



CRUD Opertaion මත Click කල විට තවත් Form එකක් open වීමට මෙම button double එක Click කර පහත පරිදි Code කරන්න

Form2 f2 = new Form2();
 f2.Show();
------------------------------------------------------------------------------------------------------------------------
ඊට අදාල Form2 පහත පරිදි වේ


-------------------------------------------------------------------------------------------------------------------------

මෙහිදී Add,View,Delete,Update යන button සදහා පොදුවේ database connections සාදනු ලබයිග
[ ඔබට එය කැමති පරිදි සිදු කල හැක --- Singleton Pattern , MVC ]

Database Connection ,Command සදහා

           SqlConnection cn = new SqlConnection();
            SqlCommand cm = new SqlCommand();
            //Connection 
            cn.ConnectionString = "<<>>Put Your Connection String";
            cm.Connection = cn;

ඉහත සදහන් code පොදු වේ

පහත කේත Add,View,Delete,Update වලට අදාපව බලමු

ADD

            cm.CommandText = "INSERT INTO test values(@a,@b,@c,@d)";
            cm.Parameters.AddWithValue("a",textBox1.Text);
            cm.Parameters.AddWithValue("b", textBox2.Text);
            cm.Parameters.AddWithValue("c", textBox3.Text);
            cm.Parameters.AddWithValue("d", textBox4.Text);
            try
            {
                cn.Open();
                cm.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                cn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Error ");
            
            }
            

VIEW

cm.CommandText = "SELECT * FROM test WHERE emp_id='"+textBox1.Text+"'";
            
            try
            {
                cn.Open();
                SqlDataReader ed=cm.ExecuteReader();
                while (ed.Read())
                {
                    textBox2.Text=ed["name"].ToString();
                    textBox3.Text=ed["address"].ToString();
                    textBox4.Text=ed["contact"].ToString();
                }

                MessageBox.Show("Done");
                cn.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Error ");

            }


EDIT

මෙහිදී වෙනස්වන හා අවශ්‍ය Command එක පමණක් සදහන් කර ඇත
එමෙන්ම textbox හි සදහන් values list එකකට convert කර ඇත

cm.CommandText = "UPDATE employee set emp_name='" + list[1] + "',emp_address='" + list[2] + "',emp_contact='" + list[3] + "' where emp_id='"+list[0]+"'";

DELETE


මෙහිදී වෙනස්වන හා අවශ්‍ය Command එක පමණක් සදහන් කර ඇත

cm.CommandText = "DELETE FROM employee where emp_id='" + textBox1.Text + "'";


-------------------------------------------------------------------------------------------------------------------------

මෙයට අදාල Database එක පහත පරිදි සකසා ගන්න



දැන් ඔබේ Employee Details ඇතුලත් කිරීමේ වැඩසටහන නිසියාකාරව ක්‍රියාකරන්නේදැයි බලන්න


Delegates

ඔයාලා දැනටමත් දන්නා ඇති Delegates වලින් Event Handle කරනවා කියලා. ඉතින් අපි බලමු කොහොමද Implementation එක කියලා

Delegates ගැන වැඩි විස්තර Microsoft ගෙන්ම අහගන්න :D  - http://msdn.microsoft.com/en-us/library/ms173171.aspx

දැන් පහත ආකාරයට Design එක කරගන්න Form3 සහ Form2



Form2 හි පහත ලෙස code කරන්න


ඉහත += මගින් එයට සිදු වීමට අව්‍යය දෑ Assign කරයි

Form3 හි පහත ලෙස code කරන්න


Delegate එක MyDelegate ලෙසදක. event එන listner ලෙසද ගනී

Button Click කරන විටදී listner event එක call කරනු ලබයි. එමගින් එයට සම්බන්ධිත Form2 හි listbox එකෙහි Values වැඩිවේ



ඔබට මෙම ආකාරයම Static keyword භාවිතයෙන් සිදු කල හැක
--------------------------------------------------------------------------------------------------------------------------
Special Key words in C#


  • New

New keyword is also used in polymorphism concept, but in the case of method overloading So what does overloading means, in simple words we can say procedure of hiding your base class through your derived class.
It is implemented as:
class A
    {
        public void show()
        {
            Console.WriteLine("Hello: Base Class!");
            Console.ReadLine();
        }
    }
 
    class B : A
    {
        public new void show()
        {
            Console.WriteLine("Hello: Derived Class!");
            Console.ReadLine();
        }
    }

  • Virtual

Virtual keyword is used for generating a virtual path for its derived classes on implementing method overriding. Virtual keyword is used within a set with override keyword. It is used as

class MyBaseClass
{
    // virtual auto-implemented property. Overrides can only 
    // provide specialized behavior if they implement get and set accessors. 
    public virtual string Name { get; set; }

    // ordinary virtual property with backing field 
    private int num;
    public virtual int Number
    {
        get { return num; }
        set { num = value; }
    }
}


class MyDerivedClass : MyBaseClass
{
    private string name;

   // Override auto-implemented property with ordinary property 
   // to provide specialized accessor behavior. 
    public override string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (value != String.Empty)
            {
                name = value;
            }
            else
            {
                name = "Unknown";
            }
        }
    }

}
  • Out
The out keyword causes arguments to be passed by reference. This is like the ref keyword, except that ref requires that the variable be initialized before it is passed. To use an outparameter, both the method definition and the calling method must explicitly use the out keyword. For example:

class OutExample
{
    static void Method(out int i)
    {
        i = 44;
    }
    static void Main()
    {
        int value;
        Method(out value);
        // value is now 44
    }
}



ගැටලු සදහා මෙහි පහතින් Comment කරන්න

No comments:

Post a Comment