ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
News
Php Tips
Android Programming
Design Pattern By PHP
C# using Linq น่าใช้จริงๆ
C# Tips & Technique
C# Design Pattern
Linux Quick Tips
Java & JavaScript Tips
Database & SQL
ZengCode Framework Guide
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     C# มาลองใช้ Class Dictionary กันครับ  (2010-03-02)

 Dictionary/StringDictionary  เป็น Collection ที่ใช้ในการรวบรวมข้อมูลของ name/value ที่มีความใกล้เคียงกัน ( โดย name/value จะคือค่า key และ item ของออบเจกต์ ) โดยออบเจกต์นี้จะคล้ายกับการใช้งาน Array เพียงแต่มีการใช้งานที่ง่าย และไม่มีฟังก์ชั่นซับซ้อนเท่า Array แต่ก็ใช้งานได้ในวงจำกัด

อันแรกนี่ Bean Class นะครับ

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

namespace TestProgram
{
    class Student
    {
        public string id { get; set; }
        public string name { get; set; }
    }
}

 

อันนี้เป็น การใช้งาน Dictionary กับ Bean Class นะครับ มีการวนลูป และการระบุโดย Key เอาไปประยุกต์ใช้งานกันนะครับ

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

namespace TestProgram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Dictionary<String, Student> myStudent = new Dictionary<String, Student>();
            for (int i = 1; i <= 3; i++)
            {
                Student st = new Student();
                st.id = "ID"+i;
                st.name = "Name_" + i;
                myStudent.Add("Student" + i, st);
            }

            //Loop over Dictionary with foreach
            foreach (var student in myStudent)
            {
                MessageBox.Show("Key => " + student.Key + " ,Student ID => " + student.Value.id + " ,Student Name => " + student.Value.name);
            }
            
            //Get from the specified key name
            Student std = new Student();       

     if (dict.ContainsKey("Student1"))
     {
        std = myStudent["Student1"];
        MessageBox.Show(std.id+" "+std.name);

           }


        }
    }
}

น่าใช้ไหมครับ สมกับคำว่า Dictionary one of the most interesting subjects in the programming world.

 

 


Comment
Name
Comment
Security CodeCAPTCHA Image

easy tracking
avis car rental discount code

This page took 0.160482 seconds to load.