using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Data;
using System.Configuration;
using System.Collections;
public class IniFile
{
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filepath);
public IniFile()
{
}
// Write ini file
public void IniWriteValue(string Section, string Key, string Value, string inipath)
{
WritePrivateProfileString(Section, Key, Value, inipath);
}
// Read ini file
public string IniReadValue(string Section, string Key, string inipath)
{
int buffer;
buffer = 255; // Define length
StringBuilder temp = new StringBuilder(buffer);
GetPrivateProfileString(Section, Key, "", temp, buffer, inipath);
return temp.ToString();
}
}
Để có thể viết được một ứng dụng phần mềm, bạn phải biết cách truyền giá trị qua lại giữa các Form.
Tuy nhiên, khi ta muốn lấy giá trị từ nhiều Form khác, chắc chúng ta sẽ gặp rắc rối với những cách làm thông thường. Delegate sẽ giúp chúng ta giải quyết vấn đề.
Trước hết, tôi sẽ trình bày cho các bạn cách truyền giá trị theo cách thông thường. Khác...
1. Delegate là gì ?
Delegate là khái niệm trong C#, nó giống như con trỏ hàm của C++. Delegate là con trỏ trỏ vào các hàm có cùng đối(số lượng đối và kiểu đối giống nhau)
Tại sao phải dùng ?
- Delegate là cơ sở của Event do đó khi ta muốn sử dụng event ta phải sử dụng Delegate
Cách sử dụng
public delegate void LearningDelegate(string s);
class A
{
public void MethodA()
{
Console.WriteLine("Method A");
}
public void MethodB()
{
Console.WriteLine("Method B");
}
}
class Program
{
public static void Main()
{
A a = new A();
LearningDelegate deg;
deg = new LearningDeleage(a.MethodA);
deg();
deg = new LearningDeleage(a.MethodB);
deg();
}
}
2. Event là gì ?
Event là các sự kiện xảy ra khi chạy chương trình (sự kiện click của button, sự kiện giá trị của comboBox thay đổi,…)
Tại sao phải dùng Event ?
Dùng Event giúp chúng ta xử lý code lịnh hoạt và đơn giản hơn. Khi sử dụng Event thì chúng ta không cần quan tâm đến việc khai nào thì đặt hàm xử lý vì khi event phát sinh nó sẽ tự động gọi hàm xử lý ra để thực hiện.
Ví dụ : Khi bạn Add/Remove 1 item vào mảng thì chương trình hiện ra thông báo “Add” hoặc “Remove”. Với cách xử lý truyền thống khi Add hoặc Remove sẽ có 1 hàm để xử lý đoạn lệnh thông báo, nhưng với Event thì mỗi khi sự kiện Add/Remove xảy ra thì tự động chương trình sẽ gọi hàm xử lý. Điều này làm giảm công sức của coder
Cách sử dung : tạo ra 3 lớp
- Lớp 1 : khai báo đối tượng có event và các event của đối tượng đó
- Lớp 2 : đăng ký event của với chương trình
- Lớp 3 : chạy chương trình, tạo đối tượng có event và thực thi
namespace Practice_Console
{
public delegate void AddItemHandler(object sender, EventArgs e);
public delegate void RemoveItemHandler(object sender, EventArgs e);
class ListWithEventChanged
{
public event AddItemHandler AddItem;
public event RemoveItemHandler RemoveItem;
protected virtual void OnAddItem(EventArgs e)
{
if (AddItem != null)
AddItem(this, e);
}
protected virtual void OnRemoveItem(EventArgs e)
{
if (RemoveItem != null)
RemoveItem(this, e);
}
public void Add(string s)
{
Console.WriteLine(s);
OnAddItem(EventArgs.Empty);
}
public void Remove(string s)
{
Console.WriteLine(s);
OnRemoveItem(EventArgs.Empty);
}
}
class EventListener
{
public EventListener(ListWithEventChanged list)
{
ListWithEventChanged List = new ListWithEventChanged();
List = list;
List.AddItem += new AddItemHandler(List_AddItem);
List.RemoveItem += new RemoveItemHandler(List_RemoveItem);
}
private void List_AddItem(object sender, EventArgs e)
{
Console.WriteLine("Phat sinh su kien Add");
}
private void List_RemoveItem(object sender, EventArgs e)
{
Console.WriteLine("Phat sinh su kien Remove");
}
}
class Program
{
public static void Main(string[] args)
{
ListWithEventChanged list = new ListWithEventChanged();
EventListener listener = new EventListener(list);
list.Add("Add");
Console.ReadLine();
list.Remove("Remove");
Console.ReadLine();
}
}
}
Nguồn: http://dotnet.fibo.us/2009/08/15/delegate-va-event-trong-c/
Mặc dù đã đọc nhiều về Abstract class và Interface nhưng mình vẫn chưa rõ lắm về mục đích sử dụng của 2 thằng này và khi nào thì nên dùng.
Cả Abstract class và Interface đều là các lớp định nghĩa sẵn các method để các lớp khác kế thừa.
Abstract class : các method có thể chỉ khai báo hoặc có thể định nghĩa method đó – quy định cách thực thi của method để các method lớp dẫn xuất sẽ mặc định thực thi như vậy (diễn tả có vẻ hơi khó hiểu
) Khác...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
namespace Test
{
class Program
{
private static decimal ToDecimal(string Value)
{
if (string.IsNullOrEmpty(Value))
return 0;
else
{
try
{
//sử dụng dạng 1,123.45
return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint |
NumberStyles.AllowCurrencySymbol,
CultureInfo.CreateSpecificCulture("en-us"));
}
catch
{
try
{
//sử dụng dạng 1.123,45
return Decimal.Parse(Value.Replace(" ", ""), NumberStyles.AllowThousands |
NumberStyles.AllowDecimalPoint |
NumberStyles.AllowCurrencySymbol,
CultureInfo.CreateSpecificCulture("nl-NL"));
}
catch {
// còn nếu mà không được thì đó không phải là chuỗi số
return 0;
}
}
}
}
private static string fDecToStr(decimal dec)
{
return fDecToStr(dec, ",", 0, string.Empty);
}
private static string fDecToStr(decimal dec, string strDauHangTram)
{
return fDecToStr(dec, strDauHangTram, 0, string.Empty);
}
private static string fDecToStr(decimal dec, string strDauHangTram, int intDauThapPhan)
{
return fDecToStr(dec, strDauHangTram, intDauThapPhan , string.Empty);
}
private static string fDecToStr(decimal dec, string strDauHangTram, int intDauThapPhan , string dtrHauTo)
{
string strFormat;
strFormat = "#,###";
for (int i = 0; i < intDauThapPhan; i++)
{
if (strFormat.EndsWith("#,###"))
strFormat += ".";
strFormat += "#";
}
//sử dụng dạng 1,123
if (strDauHangTram.Equals(","))
return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("en-us"));
return dec.ToString(strFormat, CultureInfo.CreateSpecificCulture("nl-NL"));
}
private static string fIntToStr(int Value)
{
return fIntToStr(Value, ",");
}
private static string fIntToStr(int Value, string strDauHangTram)
{
//sử dụng dạng 1,123
if (strDauHangTram.Equals(","))
return Value.ToString("#,###.", CultureInfo.CreateSpecificCulture("en-us"));
return Value.ToString("#,###.", CultureInfo.CreateSpecificCulture("nl-NL"));
}
static void Main(string[] args)
{
string a;
a = "1115.45.4545,25201";
decimal dec;
dec = ToDecimal(a);
Console.WriteLine(dec.ToString("#,###", CultureInfo.CreateSpecificCulture("nl-NL")));
Console.WriteLine(dec.ToString("#,###", CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine("===============");
Console.WriteLine(fDecToStr(dec, ".", 3, string.Empty));
Console.WriteLine(fDecToStr(dec, ",", 5, string.Empty));
Console.WriteLine("===============");
}
}
}