ba

Friday, July 16, 2010

การใช้งาน เมธอด

การใช้งาน เมธอด
ในการสร้างเมธอด เมธอดจะต้องอยู่ในคลาสเสมอ มีรูปแบบดังนี้[modifier] return_type method_name([parameter]){//คำสั่ง...........}
modifier ชื่อการเข้าถึงเมธอดนี้ เช่น public,private,protectedreturn_type ถ้าเมธอดส่งค่ากลับไปที่จุดเรียกเมธอดก็ต้องใส่ชนิดของค่าที่จะส่งกลับด้วยแต่ถ้าเมธอดนี้ ไม่ส่งค่ากลับก็ใส่ void แต่ถ้ามีการส่งค่ากลับจะต้องมี keywork คำว่า return ด้วยmethod_name เป็นชื่อเมธอดเช่น Add,GetValueparameter พารามิเตอร์นี้จะมีหรือไม่มีก็ได้ถ้าต้องการรับค่าเข้ามาก็ใส่พารามิเตอร์นี้ด้วยแต่ถ้าไม่ต้องการ รับค่าเข้ามาก็ไม่ต้องใส่ตัวอย่างการสร้างเมธอด
public void SetValue(int value){}ในเมธอดนี้มีชื่อว่า SetValue โดยรับค่า value เข้ามาและในเมธอดนี้ไม่มีการส่งค่ากลับ
ตัวอย่างการใช้งานเมธอด เปิด visual studio 2008 หรือ visual studio 2005 ขึ้นมาไปที่ File->new->Project->Visual c#->Console Application
ตัวอย่าง
using System;public class TestMethod{ public double AddValue(double x,double y) //สร้างเมธอด AddValue รับค่าเข้ามา 2 ตัวและส่งค่ากับเป็นชนิด double { return x + y; } public double SubstractValue(double x, double y) //สร้างเมธอด SubstractValue รับค่าเข้ามา 2 ตัวและส่งค่ากับเป็นชนิด double { return x - y; } public static void Main() { TestMethod a = new TestMethod(); Console.WriteLine("Call AddValue(2,4) : "+a.AddValue(2,4));// เรียกใช้เมธอด a.AddValue(2,4); เพื่อส่งตัวเลข 2 ไปที่ ตัวแปร x และ 4 ไปที่ตัวแปร y Console.WriteLine("Call SubstractValue(10,5) : " + a.SubstractValue(10, 5)); // เรียกใช้เมธอด a.SubstractValue(10,5); เพื่อส่งตัวเลข 10 ไปที่ ตัวแปร x และ 5 ไปที่ตัวแปร y Console.ReadLine(); }}
ผลลัพธ์จะได้Call AddValue(2,4) : 6Call SubstractValue(10,5) : 5
ตัวอย่างการสร้างเมธอดเพื่อแสดง Date
using System;public class TestMethod{ public string ShowDate() { return DateTime.Now.ToShortDateString().ToString(); //แสดงวันที่ปัจจุบัน } public static void Main() { TestMethod a = new TestMethod(); Console.WriteLine("Date : " + a.ShowDate()); Console.ReadLine(); }}
ผลลัพธ์จะแสดงวันที่ปัจจุบันออกมาคือDate : 22/12/2550 (เนื่องจากผมเขียนในวันที่นี้ก็จะแสดงวันที่นี้ออกมานะครับ)
ตัวอย่างการสร้างเมธอดแบบสามารถรับค่าเข้ามาแล้วกำหนดค่าและส่งกลับ
using System;public class Employee{ private string name; private int age; private string id; public Employee(string name, int age)//เป็น Constructor รับค่าเข้ามา 2 ค่า { this.id = System.Guid.NewGuid().ToString();//แสดง Guid this.name = name; this.age = age; } public void SetName(string name)//กำหนด name ใหม่ { this.name = name; } public string GetName()//ส่งค่า name กลับไปที่จุดเรียกใช้เมธอด { return name; } public void SetAge(int age)//กำหนด age ใหม่ { this.age = age; } public int GetAge()//ส่งค่า age กลับไปที่จุดเรียกใช้เมธอด { return age; } public string Id()//ส่ง id กลับไปที่จุดเรียกใช้เมธอด { return id; } }public class TestMethod{ public static void Main() { Employee emp1 = new Employee("John", 40);//ส่ง John และ 40 ไปที่ Constructor Console.WriteLine("#Id {0} ", emp1.Id()); Console.WriteLine("Name {0} ", emp1.GetName()); Console.WriteLine("Age {0} ", emp1.GetAge()); Console.WriteLine("Change name and age"); emp1.SetName("Adam");//กำหนดค่า name ใหม่ emp1.SetAge(42);//กำหนดค่า age ใหม่ Console.WriteLine("Name {0} ", emp1.GetName()); Console.WriteLine("Age {0} ", emp1.GetAge()); Console.WriteLine("\n\n\n"); Employee emp2 = new Employee("Jane", 30);//ส่ง Jane และ 30 ไปที่ Constructor Console.WriteLine("#Id {0} ", emp2.Id()); Console.WriteLine("Name {0} ", emp2.GetName()); Console.WriteLine("Age {0} ", emp2.GetAge()); Console.ReadLine(); }}ผลลัพธ์แสดงดังรูป
ตัวอย่าง
using System;public class Point{ private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public int GetXValue() { return x; } public int GetYValue() { return y; }}public class TestMethod{ static void Main() { Point p = new Point(3, 4); Console.WriteLine("x = {0}", p.GetXValue()); Console.WriteLine("y = {0}", p.GetYValue()); Console.ReadLine(); }}
ผลลัพธ์x = 3y = 4
การใช้งาน Nested MethodNested method เป็นเมธอดที่เรียกเมธอดอีกที
ตัวอย่าง
using System;public class TestMethod{ private int n = 1; public void MethodOne() { Console.WriteLine("This is methodone"); Console.WriteLine("Value is " + n); Console.WriteLine("\n"); MethodTwo(); // เรียกใช้เมธอด MethodTwo() } public void MethodTwo() { n++; Console.WriteLine("This is methodtwo"); Console.WriteLine("Value is " + n); Console.WriteLine("\n"); MethodThree();//เรียกใช้ MethodThree() } public void MethodThree() { n++; Console.WriteLine("This is methodthree"); Console.WriteLine("Value is " + n); } public static void Main() { TestMethod a = new TestMethod(); a.MethodOne(); Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป
การใช้งาน Method recursionmethod recursion เป็นเมธอดที่เรียกตัวเองเช่นต้องการหา factorial ของ 5! ซึ่งมีรูปแบบทางคณิตศาสตร์ดังนี้5! = 5x4x3x2x1 สังเกตุว่าในกระบวนการนี้จะเป็นการทำซ้ำ คือ n(n-1)(n-2)(n-3),...(1) หมายความว่าตัวมันเองจะคูณกับตัวมันเองที่ลดค่าที่ละหนึ่งไปเรี่อยๆถึง 1ซึ่งแนวคิดนี้เราสามารถนำมาใช้กับ method recursion ได้
ตัวอย่าง การเขียนโปรแกรม recursion
using System;public class TestMethodReCursion{ public int Fac(int n) { int result; if (n == 1 n == 0) //ถ้า n มีค่าเท่ากับ 1 หรือ 0 ก็ใช้แสดง 1 เนื่องจากว่า 1! หรือ 0! = 0 { result = 1; } else { result = n * Fac(n - 1); //ถ้า n มีค่าอื่นๆที่ไม่ใช้ 1 ก็ให้ นำ n คูณกับ ค่าของ n-1 ไปเรื่อยๆ } return result; } public static void Main() { TestMethodReCursion a = new TestMethodReCursion(); Console.WriteLine("5! = {0}",a.Fac(5)); Console.WriteLine("3! = {0}", a.Fac(3)); Console.WriteLine("10! = {0}", a.Fac(10)); Console.ReadLine(); }}
ผลลัพธ์จะได้ดังรูป
การส่งค่า Parameter ต่างๆ
ปกติแล้วการส่งค่า parameter ต่างๆของเมธอดจะเป็น Passed by Value เสมอ แต่ถ้าเราต้องการให้ส่งค่าแบบอื่นๆก็ใส่ keyword ลงไปเช่น ref เป็นการส่งค่าแบบ reference type จะต้องกำหนดค่าเริ่มต้นให้กับตัวแปรที่ต้องการส่งด้วยparams เป็นการส่งค่า แบบ array หรือสามารถส่งได้หลายๆค่าout เป็นการส่งค่าแบบ out จะคล้ายๆกับ ref แต่ไม่ต้องกำหนดค่าเริ่มต้นให้กับตัวแปรที่ต้องการส่ง
ตัวอย่างการส่งค่าแบบ value
using System;public class TestValueParameter{ public int AddMethod(int i,int j) { return i + j; } public static void Main() { TestValueParameter a = new TestValueParameter(); Console.WriteLine(a.AddMethod(3, 4)); Console.ReadLine(); }}ผลลัพธ์จะได้ 7
ตัวอย่างการส่งค่าแบบ Reference
using System;public class TestRefParameter{ public void TestRef(int i,ref int j) { i = 88888; j = 99999; } public static void Main() { int i = 1; int j = 2; TestRefParameter a = new TestRefParameter(); Console.WriteLine("Before call method TestRef i ={0} , j = {1}", i, j); a.TestRef(i, ref j);//เรียกใช้งาน เมธอด TestRef เพื่อส่งค่า i ที่เป็นค่า value และ ค่า j ที่เป็น reference ไป Console.WriteLine("\n"); Console.WriteLine("After call method TestRef i = {0} , j ={1}", i, j); Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป
คำอธิบายในตัวอย่างนี้ ตัวแปร i จะเป็นการส่งค่าแบบ ปกติคือ แบบ Value ดังนั้นค่าของตัวแปร i จะไม่เปลื่ยนแปลงหลังจากที่เรียกใช้เมธอดTestRef แต่ ตัวแปร j ซึ่งเป็นการส่งค่าแบบ reference ( สังเกต มี ref นำหน้า ) หลังจากที่เรียกใช้เมธอด TestRef ค่าของตัวแปร j จะเปลื่ยนไปเนื่องจากว่าเป็นการส่งแบบ reference
การส่งค่าแบบ out
ในการส่งค่าแบบนี้ จะต้องมี keyword out นำหน้าตัวแปรที่จะส่งด้วย และไม่ต้องกำหนดค่าเริ่มต้นของตัวแปรที่จะส่ง
ตัวอย่าง
using System;public class TestOutParameter{ public void OutMethod(out int value) { value = 20; } public static void Main() { int value; //ไม่ต้องกำหนดค่าเริ่มต้นในกรณีที่ต้องการส่งแบบ out TestOutParameter a = new TestOutParameter(); a.OutMethod(out value); Console.WriteLine("After call OutMethod : "+value); Console.ReadLine(); }}ผลลัพธ์จะได้After call OutMethod : 20 คำอธิบายในตัวอย่างนี้เป็นการส่งแบบ out ตัวแปร value ที่อยู่ในเมธอด Main()ไม่ต้องกำหนดค่าเริ่มต้นใดๆ ในเมธอด OutMethod จะกำหนดค่าให้กับ ตัวแปล value เอง
การส่งค่าแบบ paramsในการส่งค่าแบบนี้ จะใช้ keyword params นำหน้าตัวแปรของ parameterที่รับค่าเข้ามา
ตัวอย่าง
using System;public class TestParamsMethod{ public void TestParams(params int[] a)//กำหนดเมธอดรับค่าแบบ params { Console.WriteLine("total number is {0}", a.Length); for (int i = 0; i < a.Length; i++) Console.WriteLine("member is {0}",a[i]); } public static void Main() { TestParamsMethod test = new TestParamsMethod(); int[] a = { 1, 3, 5, 7, 9, 8, 5, 6, 99 }; int[] b = { 1, 2 }; int c = 100; test.TestParams(a); Console.WriteLine("\n"); test.TestParams(b); Console.WriteLine("\n"); test.TestParams(c); Console.ReadLine(); }}
ผลลัพธ์จะได้ดังรูป
คำอธิบาย ในตัวอย่างนี้ มีเมธอด TestParams ซึ่งกำหนดให้ parameter รับค่าแบบ params เข้ามาแล้วแสดงผล ในเมธอด Main()จะมีตัวแปร array 2 ตัวคือ a และ b ซึ่งแต่ละตัวจะมีสามาชิกไม่เท่ากัน และ ตัวแปร c ซึ่งมีสมาชิกหนึ่งตัว แล้วมีการเรียกใช้งาน เมธอด TestParams สังเกตุว่าตอนเรียกใช้งานเมธอดที่รับค่าแบบ params เราไม่ต้องกำหนด keyword params ในตอนที่เรียกใช้ เราสามารถส่งค่าแบบอาร์เรย์ที่มีสมาชิกกี่ตัวก็ได้
การใช้งานเมธอด Overloadเมธอด overload คือเมธอดที่มีชื่อเหมือนกันแต่มีการรับค่า parameter ต่างกัน โดยค่า parameter ต่างกันได่แก่ ชื่อของ parameter,ชนิดของ parameter และ จำนวน parameter ที่รับเข้ามาแต่สำหรับ return_type ของเมธอดถ้าต่างกันไม่ถือว่าเป็น overload เช่นpublic void Test(){}public void Test(int a){}public void Test(double a){}public void Test(int a,int b){}
ตัวอย่างการใช้งาน method overload
using System;public class TestOverload{ public void AddValue() { Console.WriteLine("Call AddValue() : {0}", (3 + 4)); } public void AddValue(int x, int y) { Console.WriteLine("Call AddValue(int x,int y) : {0}", x + y); } public void AddValue(double x, double y) { Console.WriteLine("Call AddValue(double x,double y) : {0}", x + y); } public static void Main() { TestOverload a = new TestOverload(); a.AddValue(); a.AddValue(10, 20); a.AddValue(5.5, 6.7); Console.ReadLine(); }}ผลลัพธ์แสดงดังรูป
ในตัวอย่างนี้มีการกำหนดเมธอด AddValue ที่เป็น overload กันคือเมธอดแรกไม่มีค่า parameter ใดๆ เมธอดที่สอง รับค่า parameter ที่เป็น int เมธอด ที่สาม รับค่า parameter ที่เป็น double
การใช้งาน Propertiesproperties คือเมธอดที่สามารถกำหนดค่าและreturn ค่าได้เลยในตัวเอง สังเกตว่าถ้าเราสร้างเมธอดที่กำหนดค่าและแสดงค่าเราต้องต้องสร้างเมธอดสองเมธอด้วยกันคือprivate int number;
public void SetVAlue(int value){ number = value;}
public int GetValue(){return number;}
แต่ใน properties เราสามารถกำหนดเพียงแค่ 1 properties ได้ดังนี้
private int number;
public int Number{
get{ return number;}
set{number = value;}
}
ตัวอย่างการใช้งาน properties
using System;public class TestProperties{ private int number; public TestProperties() { this.number = 0; } public TestProperties(int number) { this.number = number; } public int Number //สร้าง Properties ที่ชื่่อ Number { get { return number; } set { number = value; } }}public class TestMainClass{ public static void Main() { TestProperties t = new TestProperties(); Console.WriteLine(t.Number); //แสดงค่าของ Properties t.Number = 9999; //กำหนดค่าของ Properties Console.WriteLine(t.Number); //แสดงค่าของ Properties Console.WriteLine(); TestProperties u = new TestProperties(100); Console.WriteLine(u.Number); u.Number = 100000; Console.WriteLine(u.Number); Console.ReadLine(); }}
ผลลัพธ์จะได้09999
100100000
การใช้งาน static methodstatic method คือเมธอดที่กำหนด keyword static นำหน้าโดยสิ่งสำคัญเกี่ยวกับ static method คือ
static method จะไม่สามารถใช้ this ใน static method ได้static method สามารถเรียนผ่านเมธอดที่เป็น static ด้วยกันได้static method จะใช้กับตัวแปรที่เป็น static เท่านั้นstatic method ไม่สามารถใช้กับ instance ของคลาสที่สร้างโดยใช้ new
รูปแบบเช่นpublic static void Test(){}
ตัวอย่างการใช้งาน static method
using System;public class TestStaticMethod{ private static int value; private int number; //ตัวแปรนี้ ไม่สามารถใช้กับเมธอด ShowValue ได้เนื่องจากว่าเมธอด ShowValue กำหนดเป็น static public static void ShowValue() //static method { value = 10; Console.WriteLine("value is {0}", value); } public static void Main() { ShowValue(); //สามารถเรียกใช้งาน ShowValue() ได้เลยเนื่องจากว่า Main() เป็น static เมธอดอยู่แล้วสามารถเรียกใช้ static เมธอดตัวอื่นๆได้ Console.ReadLine(); }}
ผลลัพธ์จะได้value is 10
การใช้งาน Static properties
static properties จะคล้ายๆกับ static method คือสามารถเรียกใช้ตัวแปรที่เป็น static ด้วยกันได้
ตัวอย่าง
using System;public class TestStaticProperties{ private static int number; public TestStaticProperties() { } public static int Number { get { return number; } set { number = value; } } public static void Main() { TestStaticProperties.Number = 3456; //กำหนดค่าให้กับ static properties Console.WriteLine(TestStaticProperties.Number); //แสดงค่าของ static properties Console.ReadLine(); }}
ผลลัพธ์จะได้3456

การใช้งาน Enum และ Struct

การใช้งาน Enumeration,Struct
การใช้งาน Enum
enumeration จะมีชนิดเป็น integer type ซึ่ง เราสามารถกำหนดกลุ่มของข้อมูลได้ (User-defined)
ในการสร้าง enumeration เราจะใช้ keyword คำว่า enum ดังนี้

public enum Colors{ White, Black, Red, Green, Blue}

ในตัวอย่างนี้สร้าง enum ที่ชื่อว่า Colors โดยมีสมาชิกอยู่ 5 สมาชิกคือ White,Black,Red,Green,Blueโดยสมาชิกต่างๆของ enum ถ้าเราไม่ได้กำหนดค่าเริ่มต้นจะมีค่าเริ่มต้นจาก 0 ดังนี้white = 0Black =1Red =2Green=3Blue=4
ในการเรียกใช้งาน enum
Colors c = Colors.Green; //สร้างตัวแปร c ขึ้นมาเพื่อเรียกใช้สมาชิกที่ชื่อ Green
ตัวอย่าง enum
using System;public enum Colors{ White, Black, Red, Green, Blue}public class TestEnum{ public static void Main() { Colors c = Colors.Blue; Console.WriteLine(c); Console.ReadLine(); }}

ผลลัพธ์จะแสดง Blue
จากตัวอย่างนี้ถ้าต้องการให้แสดงเป็น integer type เราก็เขียนได้ดังนี้
Console.WriteLine((int)c);

ผลลัพธ์จะได้ 4
เราสามารถกำหนดค่าให้กับสมาชิกของ enum ได้ดังนี้


public enum Colors{ Red = 10, Green =20, Blue=30}

ตัวอย่าง
using System;public enum Colors{ Red = 10, Green =20, Blue=30}public class TestEnum{ public static void Main() { Colors c = Colors.Green; Console.WriteLine((int)c); Console.ReadLine(); }}

ผลลัพธ์จะได้ 20
นอกจากนี้สามารถเขียนรับค่า value มาจาก enum ได้อีกแบบคือ
Colors c = (Colors)Enum.Parse(typeof(Colors), "Blue", false);
ตัวอย่าง
using System;public enum Colors{ Red = 10, Green =20, Blue=30}public class TestEnum{ public static void Main() { Colors c = (Colors)Enum.Parse(typeof(Colors), "Blue", false); Console.WriteLine((int)c); Console.ReadLine(); }}

ผลลัพธ์จะได้ 30
การใช้งาน switch กับ enum
using System;public enum Colors{ Red = 10, Green =20, Blue=30}public class TestEnum{ public static void Main() { CallEnums(Colors.Red); Console.ReadLine(); } public static void CallEnums(Colors c) { switch (c) { case Colors.Red: Console.WriteLine(" Red Color "); break; case Colors.Green: Console.WriteLine("Green Color"); break; case Colors.Blue: Console.WriteLine("Blue Color"); break; default: Console.WriteLine("invalid input"); break; } }}

ผลลัพธ์จะได้ Red Color

ตัวอย่างการใช้สมาชิกของ enum คำนวนค่า
using System;public enum TestValue{ a = 10, b= 30, c= a+b}public class TestEnum{ public static void Main() { TestValue v = TestValue.c; Console.WriteLine((int)v); Console.ReadLine(); }}

ผลลัพธ์จะได้ 40
การวนลูปเพื่อแสดงสมาชิกของ enum
using System;public enum Colors{ Red,Green,Blue,White,Black}public class TestEnum{ public static void Main() { Colors c; for (c = Colors.Red; c <= Colors.Black; c++) //วนลูปเพื่อแสดงสมาชิกแต่ละตัวของ Colors { Console.WriteLine("value is {0} ", c); } Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป

การใช้งาน GetValues() ใน enum
using System;public enum Weeks{ Sun,Mon,Tue,Wed,Thr,Fri,Sat}public class TestEnum{ public static void Main() { Weeks w = Weeks.Sun; foreach (int i in Enum.GetValues(w.GetType())) //วนลูปเพื่อแสดงค่าสมาชิกแต่ละตัว { Console.WriteLine("value is {0}", Enum.GetName(w.GetType(), i)); //แสดงชื่อของสมาชิกแต่ละตัว } Console.ReadLine(); }}

ตัวอย่าง การใช้ GetNames()
using System;public enum Weeks{ Sun,Mon,Tue,Wed,Thr,Fri,Sat}public class TestEnum{ public static void Main() { Weeks w = Weeks.Sun; foreach (string str in Enum.GetNames(w.GetType())) { Console.WriteLine("value is {0}",str); } Console.ReadLine(); }}
ผลลัพธ์แสดงดังรูป
การเปรียบเทียบค่าของสมาชิกใน enum
using System;public enum AsciiCode{ A = 65, B =66, C=67, D =68}public class TestEnum{ public static void Main() { AsciiCode a = AsciiCode.C; AsciiCode b = AsciiCode.B; if (a <> {1}", Convert.ToInt32(a),Convert.ToInt32(b)); } Console.ReadLine(); }}

ผลลัพธ์ จะได้ 67 > 66
การตรวจสอบว่ามีสมาชิกตามที่เรากำหนดหรือไม่
using System;public enum Colors{ Red,Green,Blue}public class TestEnum{ public static void Main() { if (Enum.IsDefined(typeof(Colors), "White")) //ใช้ IsDefined ตรวจสอบสมาชิกของ enum { Console.WriteLine("Valid member"); } else { Console.WriteLine("Invalid member"); } Console.ReadLine(); }}

ผลลัพธ์จะได้ Invalid member เนื่องจาก enum ที่ชื่อ Colors ไม่มีสมาชิก White

การใช้งาน Struct
struct จะคล้ายๆกับ class ใช้สร้างกลุ่มข้อมูลต่างๆ เช่น เมธอด,properties,member variable
ข้อแตกต่างระหว่าง class กับ struct1. Struct เป็น Value Type ส่วนคลาสเป็น Reference type2. Struct ไม่สามารถใช้ Interitance 3. สมาชิกของStruct ไม่สามารถระบุเป็น abstract,virtual4. Default Contructor จะถูกสร้างขึ้นมาทุกครั้งเมื่อสร้าง struct และ ค่า default Constructor จะไม่สามารถเปลี่ยนแปลงได้5. Struct เราสามารถกำหนด Constructor ได้แต่ไม่สามารถกำหนด Destructor
รูปแบบ struct
struct struct_name{//member ต่างๆ}
struct เป็น keyword จะต้องประกาศทุกครั้งเมื่อสร้าง structstruct_name เป็นชื่อของ struct
ตัวอย่าง
using System;public struct TestColor{ public string mycolor; public TestColor(string color) { mycolor = color; } public string Color { get { return mycolor; } set { mycolor = value; } }}public class TestStruct{ public static void Main() { TestColor c = new TestColor("Green"); Console.WriteLine(c.Color); c.Color = "Blue"; Console.WriteLine(c.Color); Console.ReadLine(); }}


ตัวอย่าง
using System;public struct TestColor{ public string mycolor;}public class TestStruct{ public static void Main() { TestColor c; c.mycolor = "White"; Console.WriteLine(c.mycolor); Console.ReadLine(); }}

การใช้งานเมธอด ใน struct

using System;public struct Rectangles{ public double width; public double height; public Rectangles(double width, double height) { this.width = width; this.height = height; } public double Size() { return width * height; }}public class TestStruct{ public static void Main() { Rectangles r = new Rectangles(4.4, 6.6); Console.WriteLine(r.Size()); Console.ReadLine(); }}

การใช้งาน Generic struct

using System;using System.Collections.Generic;struct TestGeneric{ public T value1, value2; public TestGeneric(T value1, T value2) { this.value1 = value1; this.value2 = value2; } public T Value1 { get { return value1; } set { value1 = value; } } public T Value2 { get { return value2; } set { value2 = value; } } }public class TestStruct{ static void Main() { TestGeneric a = new TestGeneric(4, 8); Console.WriteLine(a.Value1 + " " + a.Value2); a.Value1 = 10; a.Value2 = 20; Console.WriteLine(a.Value1 + " " + a.Value2); Console.ReadLine(); }}
ผลลัพธ์4 810 20

การเขียน c#.net เพื่อติดต่อกับ LDAP

ในตัวอย่างนี้ผมทดสอบ ดึง username,password มาจาก Window Server ในส่วนของ OU นะครับซึ่งใน OU (Organization Unit ) นี้ผมได้เก็บชื่อ user ต่างๆมากมายรวมถึงเก็บ สิทธิ์ในการ login เข้าเข้าสู่ window serverผมได้สร้าง web form ขึ้นมา หน้านึง ที่มีหน้าตาดังรูป
จากนั้น ก็คลิกที่ปุ่ม Submit แล้วเขียนโค้ดดังนี้(ก่อนที่จะใช้งาน DirectoryEntry ให้ไป add reference library ที่ชื่อ System.DirectoryServices ก่อน จากนั้นก็เรียกใช้ using System.DirectoryServices;และในการติดต่อ ldap สิ่งแรกคือจะต้องรู้ LDAP Server ก่อนนะครับ )
ตัวอย่างโค้ด protected void Button1_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
string path = "192.x.x.x"; //ip ของ window server ที่จะติดต่อ
DirectoryEntry direct_entry = new DirectoryEntry();
direct_entry.Path = path;
direct_entry.Username = username;
direct_entry.Password = password;
if (IsCheckUserExist(username)) //ถ้ามี user อยู่ในระบบจริงก็ให้ไปที่หน้า hellomember.aspx
{
Response.Redirect("hellomember.aspx");
}
else
{
Response.Write("กรุณาใส่ username,password ให้ถูกต้อง");
}
}
public bool IsCheckUserExist(string username)
{
DirectoryEntry direct_entry = GetDirectoryEntry();
DirectorySearcher direct_search = new DirectorySearcher();
direct_search.SearchRoot = direct_entry;
direct_search.Filter = "(&(ObjectClass=user)(SAMAccountName=" + username + "))"; //ตรวจสอบเฉพาะ username ที่ใส่เข้ามาทาง textbox
SearchResultCollection result_col = direct_search.FindAll();
return result_col.Count > 0; //ถ้ามี username ชื่อนี้อยู่ในระบบ server จริงก็ให้ ส่งค่า true ไปที่ event ที่ชื่อ Button1_Click
}
public DirectoryEntry GetDirectoryEntry()
{ DirectoryEntry direct_entry = new DirectoryEntry("LDAP://192.x.x.x",txtUsername.Text,txtPassword.Text);//กำหนด LDAP Server , username,password
direct_entry.AuthenticationType = AuthenticationTypes.Secure;
return direct_entry;
}
นี้เป็นตัวอย่างโค้ดที่ผมเขียนขึ้นเพื่อติดต่อ LDAP ผมเขียนใน Website นะครับถ้าจะทำเป็น web service ก็เขียนคล้ายกับตัวอย่างนี้ลองเอาไปประยุกต์ใช้ดูครับ
โดยสิ่งสำคัญหลักๆใน ตัวอย่างนี้คือ ให้ผู้ใช้ สามารถ login ผ่านหน้า web site ได้ โดยผู้ใช้งานจะต้องมี username,password อยู่ใน ส่วนของ OU ใน Window server ก่อน
แล้วพอกดปุ่ม Submit ทางโปรแกรมก็จะติดต่อ เพื่อตรวจสอบ username,password ผ่าน protocal : LDAP