By GokiSoft.com| 14:07 02/10/2021|
C Sharp

[Video] Hướng dẫn xây dựng khung dự án (namespace) + Tìm hiểu Exception (Try .. Catch .. Finally) - Lập trình C# C Sharp

Link Video Bài Giảng



Xây dựng 1 khung dự án quản lý bán hàng:
Phân tích module chức:
	- Quản lý sản phẩm
	- Quản lý phản hồi
	- Quản lý tin tức
	- Quản lý đơn hàng
	- Quản lý mail
	- Báo cáo


	- CSDL -> Tables <-> Class Object (ORM) -> Insert/Update/Delete/Select -> DB
	- Thư viện chung cho cả dự án
	- Cấu hình chung cho dự

=================================================
Xây dựng khung & namespace -> tổ chức như thế nào??? -> Modules.

Ví dụ:
	- Xây dựng Exception đặt tên là: NegativeNumberException, RectangleException -> Kế thừa từ class Exception
	- Xay dung class object: Rectangle -> gom 3 fields: a, b, c -> 3 canh cua 1 tam giac
		- Tao ra ham tao day du doi so: Rectangle(a, b, c) -> Check du lieu dau vao cho Ham tao nay
			- a, b, c < 0 -> NegativeNumberException
			- Neu tong 2 canh < canh con lai ->  RectangleException
	- Test: Tao 1 object Rectangle
		- Yeu cau canh cua tam giac > 0 -> msg
		- Khong thoa man dieu kien 3 canh cua 1 tam giac
		- Khoi tao doi tuong Rectangle thanh cong.


Khung dự án -> Phát triển theo yêu cầu trên



Cách sử dụng namespace và alias cho namespace


using System;
using Models;
using Lesson05.Modules.Product;
using News = Lesson05.Modules.News;
using Order = Lesson05.Modules.Order;

namespace Lesson05
{
    class Program
    {
        static void Main(string[] args)
        {
            Test01();
        }
        static void Test01()
        {
            Circle circle1 = new Circle();
            Circle.Radius = 12.6;

            Circle circle2 = new Circle();
            Circle.Radius = 1.6;

            Console.WriteLine("Chu vi 1: " + circle1.TinhChuVi());
            Console.WriteLine("Chu vi 2: " + circle2.TinhChuVi());

            //Vi du: cach goi class object tu namespace.
            ProductController controller = new ProductController();

            //TH nao thi su dung Alias cho namespace
            News.Config config = new News.Config();
            config.Display();

            Order.Config config2 = new Order.Config();
            config2.showMsg("OKOK");
        }
    }
}


Tìm hiểu exception



static void Test02()
        {
            //Exception
            int x, y;
            Console.WriteLine("Nhap X: ");
            x = int.Parse(Console.ReadLine());

            Console.WriteLine("Nhap Y: ");
            y = int.Parse(Console.ReadLine());

            //C1: Logic -> Nen fix theo cach nay
            if(y == 0)
            {
                Console.WriteLine("Error: Devided by zero!!!");
                //throw new Exception();
            } else
            {
                double s = x / y;
                Console.WriteLine("s = {0}", s);
            }
            //C2: Try .. Catch ..
            try
            {
                double s = x / y;
                Console.WriteLine("s = {0}", s);
            } catch(Exception ex)
            {
                Console.WriteLine("Error: Devided by zero!!!");
            } finally
            {
                Console.WriteLine("Noi giai phong tai nguyen ...");
            }

            int[] t = { 1, 4, 2, 6, 10 };
            //Length: 5, Index: 0 -> 4
            Console.WriteLine("t[{0}] = {1}", 0, t[0]);

            //Console.WriteLine("t[{0}] = {5}", 0, t[5]); -> Fix logic.
        }

Ứng dụng exception trong bài toán -> Khỏi tạo đối tượng tam giác gồm 3 canh a, b, c.



static void Test03()
        {
            try
            {
                Rectangle rectangle = new Rectangle(5, 6, 7);
                Console.WriteLine("Khoi tao doi tuong Rectangle thanh cong.");
            } catch(NegativeNumberException ex)
            {
                Console.WriteLine("Yeu cau canh cua tam giac > 0");
            } catch(RectangleException ex)
            {
                Console.WriteLine("Khong thoa man dieu kien 3 canh cua 1 tam giac");
            }
        }



#RectangleException.cs


using System;
namespace Lesson05.Exc
{
    public class RectangleException : Exception
    {
        public RectangleException()
        {
        }
    }
}


#Rectangle.cs


using System;
namespace Lesson05.Exc
{
    public class Rectangle
    {
        public double A { get; set; }
        public double B { get; set; }
        public double C { get; set; }

        public Rectangle(double a, double b, double c)
        {
            if(a <= 0 || b <= 0 || c <= 0)
            {
                throw new NegativeNumberException();
            }
            if((a + b < c) || (a + c < b) || (b + c < a))
            {
                throw new RectangleException();
            }
            A = a;
            B = b;
            C = c;
        }
    }
}


#NegativeNumberException.cs


using System;
namespace Lesson05.Exc
{
    public class NegativeNumberException : Exception
    {
        public NegativeNumberException()
        {
        }
    }
}

Souce Code Full -> Main



using System;
using Models;
using Lesson05.Modules.Product;
using News = Lesson05.Modules.News;
using Order = Lesson05.Modules.Order;
using Lesson05.Exc;

namespace Lesson05
{
    class Program
    {
        static void Main(string[] args)
        {
            //Test01();
            //Test02();
            Test03();
        }

        static void Test03()
        {
            try
            {
                Rectangle rectangle = new Rectangle(5, 6, 7);
                Console.WriteLine("Khoi tao doi tuong Rectangle thanh cong.");
            } catch(NegativeNumberException ex)
            {
                Console.WriteLine("Yeu cau canh cua tam giac > 0");
            } catch(RectangleException ex)
            {
                Console.WriteLine("Khong thoa man dieu kien 3 canh cua 1 tam giac");
            }
        }

        static void Test02()
        {
            //Exception
            int x, y;
            Console.WriteLine("Nhap X: ");
            x = int.Parse(Console.ReadLine());

            Console.WriteLine("Nhap Y: ");
            y = int.Parse(Console.ReadLine());

            //C1: Logic -> Nen fix theo cach nay
            if(y == 0)
            {
                Console.WriteLine("Error: Devided by zero!!!");
                //throw new Exception();
            } else
            {
                double s = x / y;
                Console.WriteLine("s = {0}", s);
            }
            //C2: Try .. Catch ..
            try
            {
                double s = x / y;
                Console.WriteLine("s = {0}", s);
            } catch(Exception ex)
            {
                Console.WriteLine("Error: Devided by zero!!!");
            } finally
            {
                Console.WriteLine("Noi giai phong tai nguyen ...");
            }

            int[] t = { 1, 4, 2, 6, 10 };
            //Length: 5, Index: 0 -> 4
            Console.WriteLine("t[{0}] = {1}", 0, t[0]);

            //Console.WriteLine("t[{0}] = {5}", 0, t[5]); -> Fix logic.
        }

        static void Test01()
        {
            Circle circle1 = new Circle();
            Circle.Radius = 12.6;

            Circle circle2 = new Circle();
            Circle.Radius = 1.6;

            Console.WriteLine("Chu vi 1: " + circle1.TinhChuVi());
            Console.WriteLine("Chu vi 2: " + circle2.TinhChuVi());

            //Vi du: cach goi class object tu namespace.
            ProductController controller = new ProductController();

            //TH nao thi su dung Alias cho namespace
            News.Config config = new News.Config();
            config.Display();

            Order.Config config2 = new Order.Config();
            config2.showMsg("OKOK");
        }
    }
}





Tags:

Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)